n <- 10
x <- c(0:n)
p <- 0.5
pmf <- dbinom(x, size=n, prob=p)
x
##  [1]  0  1  2  3  4  5  6  7  8  9 10
str(pmf)
##  num [1:11] 0.000977 0.009766 0.043945 0.117188 0.205078 ...
plot(x, dbinom(x, size=n, prob=p), main="Probability mass function for Bin(10,0.5)")

pbinom(5, size=n, prob=p)
## [1] 0.6230469
plot(x, dbinom(x, size=n, prob=p), main="Cumulative distribution for Bin(10,0.5)", type="S")

plot(x, pbinom(x, size=n, prob=p), main="Cumulative distribution for Bin(10,0.5)", type="S")

draws <- rbinom(25, size=n, prob=p)
str(draws)
##  int [1:25] 6 4 3 4 6 7 4 3 2 3 ...
hist(draws, main="Random draws from Bin(0,0.5)")

hist(draws, main="Random draws from Bin(0,0.5)", probability="TRUE", xlim=c(0,n))

hist(draws, main="Random draws from Bin(0,0.5)", probability="TRUE", xlim=c(0,n), ylim=c(0, 0.35))
points(x, dbinom(x, size=n, prob=p), pch=20, col="red", cex=2)

data <- c(9,7,8,8,11,15,11,13,8,12,12,11,11,9,12,8,12,11,12,10,9,12,11,9,7)
hist(draws, main="Random draws from Bin(10,0.5)", probability="TRUE", xlim=c(0,n), ylim=c(0, 0.35))
points(x, dbinom(x, size=n, prob=p), pch=20, col="blue", cex=2)
hist(data, probability=TRUE, col="blue", add=TRUE)
hist(data, probability = TRUE, col="blue",add=TRUE)
points(data,dbinom(data,size=25,prob=p),pch=20, col="red")

?sample
## starting httpd help server ... done
sample.int(n, size = n, replace = FALSE, prob = NULL,
           useHash = (n > 1e+07 && !replace && is.null(prob) && size <= n/2))
##  [1]  9  1 10  5  7  4  6  8  3  2
data_small <- sample(x, 10, replace = FALSE, prob = NULL)
plot(data_small, dbinom(data_small, size=10, prob=p), pch=20, col="yellow", cex=2)

binomial_test <- read.csv("C:/Users/HP/Documents/OKLAHOMA STATE UNIVERSITY/FALL 2023 COURSES/COURSEWORK/PBIO 5110 ECOLOGICAL MODELING/WEEK 2/binomial_test.csv", header = TRUE, na="")
summary (binomial_test)
##     Results    
##  Min.   :0.00  
##  1st Qu.:2.00  
##  Median :3.00  
##  Mean   :2.68  
##  3rd Qu.:3.00  
##  Max.   :5.00
mean <- mean(binomial_test$Results)
n <- length(binomial_test$Results)
p <- mean/n
range (binomial_test$Results)
## [1] 0 5
hist(binomial_test$Results, main="Binomial Test Density", probability = TRUE,
     xlim = c(0,5))
points(x, dbinom(x, size=n, prob=p), pch=20, col="red",
       cex=2)

var(binomial_test$Results)
## [1] 1.393333
var <- n*p*(1-p)
np <- n*p*(1-p)
np
## [1] 2.392704

Poisson Distribution

x<- c(0:20)
lambda = 3
plot(x, dpois(x, lambda), main = "Probability mass function for Po(3)",
     pch=20)

plot(x, ppois(x, lambda), main = "Cumulative density function for Po(3)",
     pch=20, type="s")

x<- c(0:20)
lambda <- 12
draws <- rpois(999, lambda=lambda)
hist(draws, main = "Random draws from Po(12)", breaks=20,
     probability=TRUE)
points(x, dpois(x, lambda=lambda), col="red", pch=20)

lambda <- c(1, 5, 10)
x <- c(0:20)
color_vector <- c("black", "blue", "green")
plot(x, dpois(x, lambda = lambda[1]), main = "Poisson processes",
      pch = 20, col = color_vector[1], type = "l")
  abline(v = mean(rpois(9999, lambda[1])), col = color_vector[1],
      lwd = 2)
  var(rpois(9999, lambda = lambda[1]))
## [1] 1.022591
for (i in 2:length(lambda)) {
  points(x, dpois(x, lambda[i]), main = "Poisson processes", pch = 20, col = color_vector[i])
abline(v = mean(rpois(9999, lambda[i])), col = color_vector[i], lwd = 2)
print(var(rpois(9999, lambda = lambda[i])))
}

## [1] 5.022742
## [1] 9.971619

#Continous functions

mu <- 0
sigma <- 1
curve(dnorm(x,mean=mu,sd=sigma),from= -5,to= 5, ylim=c(0,1))
curve(pnorm(x,mean=mu,sd=sigma), from= -5, to= 5, add=TRUE,
      col="blue")
title(expression("PDF and "* phantom("CDF ")* "for N(0,1)"),
      col.main="black")
title(expression(phantom("PDF and ")* "CDF "*phantom(" f or N(0,1")),
      col.main="blue")

gamma distribution

sh <- 1
sc <- 2
curve(dgamma(x, shape=sh, scale=sc), from=0.001, to= 20,
      ylim=c(0,1))
curve(pgamma(x,shape=sh, scale=sc), from=0.001, to=20,
      add=TRUE, col="blue")
title(expression("PDF and " * phantom("CDF ")*" for Gamma(1,2)"),
      col.main="black")
title(expression(phantom("PDF and ")*"CDF " * phantom(" for Gamma(1,2")),
      col.main="blue")

sh <- 9
sc <- 0.5
curve(dgamma(x, shape=sh, scale=sc), from=0.001, to= 20,
      ylim=c(0,1))
curve(pgamma(x,shape=sh, scale=sc), from=0.001, to=20,
      add=TRUE, col="blue")
title(expression("PDF and " * phantom("CDF ")*" for Gamma(9,0.5)"),
      col.main="black")
title(expression(phantom("PDF and ")*"CDF " * phantom(" for Gamma(9,0.5")),
      col.main="blue")

sh <- 7.5
sc <- 2
draws <- rgamma(9999, shape=sh, scale=sc)
hist(draws)
abline(v=mean(draws),lwd=2)
abline(v=var(draws), lty=2,lwd=2)

mean(draws)/sc
## [1] 7.460401
mean(draws)*sc
## [1] 29.8416

#beta distribution

alpha <- 0.5
beta <- 0.5
curve(dbeta(x, shape1 = alpha, shape2=beta), from=0, to=1,
      ylim=c(0,4))
curve(pbeta(x, shape1=alpha, shape2=beta), from=0.001,to=20,
      add=TRUE, col="blue")
title(expression("PDF and "* phantom("CDF ") *"for Beta(0.5,0.5)"),
      col.main="black")
title(expression(phantom("PDF and ")* "CDF " *phantom("for Beta(0.5,0.5)")),
      col.main="blue")

alpha <- 2
beta <- 5
curve(dbeta(x, shape1 = alpha, shape2=beta), from=0, to=1,
      ylim=c(0,4))
curve(pbeta(x, shape1=alpha, shape2=beta), from=0.001,to=20,
      add=TRUE, col="blue")
title(expression("PDF and "* phantom("CDF ") *"for Beta(2,5)"),
      col.main="black")
title(expression(phantom("PDF and ")* "CDF " *phantom("for Beta(2,5)")),
      col.main="blue")

x <- c(1:10)
x <- c(1:20)
curve(dexp(x,rate = 1,log = FALSE), from = 0, to=10, col="2", lwd=2)
curve(pexp(x,rate = 1,log = FALSE), from = 0, to=10, col="3",lwd=2, add=TRUE)
curve(rexp(x,rate = 1), from = 0, to=10,col="4",lwd=2, add = TRUE)
curve(dexp(x,rate = 10,log = FALSE), from = 0, to=10, col="8",lwd=2,add=TRUE)
curve(pexp(x,rate = 10,log = FALSE), from = 0, to=10, col="6",lwd=2,
      add=TRUE)
title(main = "Impact of Rate Parameter on Density function")
legend(x="topright",
       legend= c("dexp rate=1", "pexp rate=1", "rexp rate=1", "dexp rate=10",
                 "pexp rate=10"),
       col=c("2","3","4","8","6"),
       lwd=1)

R Markdown