# Question 1 Create an R function to calculate factorials without the built-in function factorial().
M.factorial <- function(n) { ## name my function to calculate factorials as "M.factorial"
## argument: n
## if n is negative, the factorial is not calculate and the message "You should use a non-negative integer" is displayed
if(n < 0){
print("You should use a non-negative integer")
}
## if n is 0, the factorial is 1 (0! = 1)
if(n == 0)
1
## if n is a positive integer, the factorial is calculated
if(n > 0) {
prod(n : 1) ## multiply all numbers from 1 to n together (n!)
}
}
# Question 2 Create an R function to calculate the number of possible combinations that can be obtained by taking a subset of elements from a larger set by using my function.
M.choose <- function(n, k) { ## name my function to calculate the number of possible combinations as "M.choose"
## aeguments: n, k (choose k elements from n)
M.factorial(n) / (M.factorial(k) * M.factorial(n - k)) ## divide n's factorial by the product of k's fuctorial and a fuctorial of a difference between n and k
}
# Question 3 Create an R function to calculate the probability mass of binomial distributions by using my function.
M.dbinom <- function(x, n, p) { ## name my function to calculate the probability mass of binominal distributions as "M.dbinom"
M.choose(n, x) * p ^ x * (1 - p) ^ (n - x) ## multiply the result of calculation by M.choose(n, x), p to the power x and (1-p) to the power (n-x) together
}
Display graphically the PMF of binomial distributions for n = 2, 5, 10, and 50 by using my bionominal PMF. p can be chosen freely but at least two different values for p should be used.
## prepare two two variables to save the result of calculation
data2 <- matrix(NA, ncol = 4)
data3 <- matrix(NA, ncol = 4)
for(p in c(0.5, 0.1)) { ## loop for p = 0.5 and p = 0.1
for(i in c(2, 5, 10, 50)) { ## loop for n = 2, 5, 10, 50
for(j in 0 : i) { ## loop for x = 0 ~ n
data2 <- c(j, i, p, M.dbinom(j, i, p)) ## save the result of culculation in data2
data3 <- rbind(data3, data2) ## save the data of data2 in data3
}
}
}
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
## Warning in rbind(data3, data2): number of columns of result is not a
## multiple of vector length (arg 2)
colnames(data3) <- c("x", "n", "p", "prob.mass") ## name the colum
## check the data
head(data3) ## first six data
## x n p prob.mass
## NA NA NA NA
## data2 0 2 0.5 0.00000
## data2 1 2 0.5 0.50000
## data2 2 2 0.5 2.00000
## data2 0 5 0.5 0.00000
## data2 1 5 0.5 0.15625
tail(data3) ## last six data
## x n p prob.mass
## data2 45 50 0.1 1.251107e-39
## data2 46 50 0.1 1.510998e-41
## data2 47 50 0.1 1.428840e-43
## data2 48 50 0.1 9.922500e-46
## data2 49 50 0.1 4.500000e-48
## data2 50 50 0.1 5.000000e+01
I couldn’t write R commands after this, so I couldn’t get the graph of the PMF
install.packages(“knitr”, dependencies = TRUE) library(“knitr”) knit2html(“pre_1.Rmd”, output = “pre_1.html”)