* 1.* 
                          
                        

In order to create factorial I create a function of n that corresponds to the requirements of the factorial: if n=0 (0!) the result will be 1; if n>0 (n!) => n(n-1)(n-2)…1. If the number is below 0, there will be an error as such factorial is undefined

my_f <- function(n) {
  
        if(n == 0) {
          return(1)
        } 
        if (n > 0) { 
          return(n*my_f(n-1))
        } 
        if (n < 0) { 
          return("Error")
        }
}

Below I check the factorial by calculating some examples and by using the built-in command factorial() for comparison

my_f(0)
## [1] 1
my_f(-5)
## [1] "Error"
my_f(5)
## [1] 120
my_f(1)
## [1] 1
factorial(5)
## [1] 120
                            * 2.*

In order to create combination function I follow the same strategy as above: I specify a function that has k elements taken from n. Using the factorial, my_f, I can generate a combination, my_c: n!/k!(n-k)!. If I set k bigger than n I will get an error given that I cannot divide by 0

my_c <- function(n, k) {
  
  return(my_f(n)/(my_f(k)*my_f(n-k)))
  
}

I make some checks by trying the created function - my_c - and also by comparing the results with the built-in command “choose()”. When I set k>n it gives me an error

my_c(5, 2)
## [1] 10
choose(5, 2)
## [1] 10
                            * 3.*

I create a function that calculates the pmf of binomial distributions. I use the already defined function my_c and I specify the new function so that it corresponds to the pmf - f(x)=combination(n, k)(pk)((1-p)(n-k)) where I specify p to be between 0 and 1 as this is the probability of success

my_b <- function(k, n, p) {
  
       if(p < 0) {return("Error")
      }
       if(p > 1) {return("Error")
      } 
      else {return (my_c(n, k)*((p^k)*((1-p)^(n-k))))
      }
}

Below are some checks and the comparison with the command “dbinom()”. If the probability is larger than 1 or smaller than 0 it will give an error

my_b(2,5,.2)  
## [1] 0.2048
my_b(4,10,0.5)
## [1] 0.2050781
dbinom(4,10,0.5)
## [1] 0.2050781
                              *4.*

Finally I try to make a graph of the binomial distribution using various parameters

my_g <- function(n,p){
          x <- my_b(0:n, n, p)
         return(barplot(x,names.arg=0:n, ylim=c(0, 1)))
}

my_g(2, 0.2) 
## 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 > 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 > 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

my_g(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 > 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 > 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

my_g(5, 0.2) 
## 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 > 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 > 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 > 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 > 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 > 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

my_g(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 > 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 > 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 > 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 > 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 > 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

my_g(10, 0.2) 
## 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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

my_g(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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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

my_g(50, 0.2) 
## 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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

my_g(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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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 > 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

Second function my_b2

# factorial with ifelse

my_f2 <- function(n)
ifelse(n == 0, 1, n*my_f2(n-1))

# combination with factorial my_f2

my_c2 <- function(n, k) {
  
  return(my_f2(n) / (my_f2(k) * my_f2(n-k)))
  
}

# pmf of binomial - my_f2 and my_c2

my_b2 <- function(k, n, p) {
  
    if(p < 0) {return("Error")
    }
    if(p > 1) {return("Error")
    }
    else {return (my_c(n, k)*((p^k)*((1-p)^(n-k))))
    }
}


# graph of pmf - my_f2, my_c2, my_b2

my_g2 <- function(n,p){
          x <- my_b2(0:n, n, p)
         return(barplot(x,names.arg=0:n, ylim=c(0, 1)))
}

my_g2(2, 0.2) 
## 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 > 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 > 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

Unfortunately, neither of these commands work in order to create a graph of the pmf for the binomial distributions.I get the message:

“In if (n > 0) { : the condition has length > 1 and only the first element will be used In if (n == 0) { : the condition has length > 1 and only the first element will be used”. I try to fix that by using the ifelse command when I am creating the factorial but unfortunately nothing changes.

Below I try the same thing but instead of using my created factorial I use the built-in command for it. For everything else I use my own functions. Apparantly, when I do that the graphs work fine so I suspect that the problem comes from the first command for factorial in exercise 1. At this point, however, I am unable to fix the problem due to the limitations of my knowledge

mycombination <-  function(n, k) {
  
  return(factorial(n) / (factorial(k) * factorial(n-k)))
  
}

mybinomial <- function(k, n, p) {
    if(p < 0) {return("Error")
    }
    if(p > 1) {return("Error")
    }
    else {return (mycombination(n, k)*((p^k)*((1-p)^(n-k))))
    }
}

mygraph <- function(n,p){
          x <- mybinomial(0:n, n, p)
         return(barplot(x,names.arg=0:n, ylim=c(0, 1)))
}

mygraph(2, 0.2) 

mygraph(2, 0.7) 

mygraph(5, 0.2) 

mygraph(5, 0.7) 

mygraph(10, 0.2) 

mygraph(10, 0.7) 

mygraph(50, 0.2) 

mygraph(50, 0.7)