In the first Exercise I create a R function which will calculate factorial of given number.
my_fact <- function(n){
result <- 1
{if(n == 0){
return(1)}
if(n >= 1) {
for(i in 1:n){
result <- result*i}
return(result)
}
}
}
Second, I create a R-function, which calculates the number of possible combinations by taking a subset of elements from a larger set.
my_c3 <- function(n, k) {
result <- my_fact(n) / (my_fact(k) * my_fact(n - k)) ## i.e n! = n!/k!(n-k)!
return(result)
}
I create a R function to calculate the probability mass of binomial distributions.
my_b3 <- function (x, n, p) {
result <- my_c3(n, x) * p^x * (1-p)^(n-x)
return(result)
}
I display graphically the PMF of binomial distributions for n {2,5,10 and 50}
graph <- function(n, p){
x <- my_b3(0:n, n, p)
return(barplot(x,names.arg=0:n))
}
graph(2, 0.4)
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n >= 1) {: the condition has length > 1 and only the first
## element will be used
## Warning in 1:n: numerical expression has 3 elements: only the first used
graph(2, 0.7)
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n >= 1) {: the condition has length > 1 and only the first
## element will be used
## Warning in 1:n: numerical expression has 3 elements: only the first used
graph(5, 0.4)
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n >= 1) {: the condition has length > 1 and only the first
## element will be used
## Warning in 1:n: numerical expression has 6 elements: only the first used
graph(5, 0.7)
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n >= 1) {: the condition has length > 1 and only the first
## element will be used
## Warning in 1:n: numerical expression has 6 elements: only the first used
graph(10, 0.4)
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n >= 1) {: the condition has length > 1 and only the first
## element will be used
## Warning in 1:n: numerical expression has 11 elements: only the first used
graph(10, 0.7)
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n >= 1) {: the condition has length > 1 and only the first
## element will be used
## Warning in 1:n: numerical expression has 11 elements: only the first used
graph(50, 0.4)
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n >= 1) {: the condition has length > 1 and only the first
## element will be used
## Warning in 1:n: numerical expression has 51 elements: only the first used
graph(50, 0.7)
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n == 0) {: the condition has length > 1 and only the first
## element will be used
## Warning in if (n >= 1) {: the condition has length > 1 and only the first
## element will be used
## Warning in 1:n: numerical expression has 51 elements: only the first used