The answer has to be a wellcommented code in RStudio a softw
     The answer has to be a well-commented code in RStudio (a software package)  Write a function that generates a multinomial distribution. For each trial we have m possible results. We will designate these outcomes by the integers from 1 to m. These outcomes are analogous to the outcomes of \"success\' and \"failure\" for a binomial random variable. The outcome of each trial is independent of the outcome of any other trial, and the probability of obtaining any of the m outcomes is identical for each trial. The function prototype is:  r.multinomial  p = c(0.3, 0.5, 1)  > x = r.multinomial(10, 50, p)  > x  |1|, |, 2| |, 3| |, 4| |, 5| |, 6| |, 7| |, 8| I, 9| |, 10|  |1, | 14 17 20 16 12 15 10 12 12 10 |2, | 7 14 7 14 9 5 17 13 10 8 |3, | 29 19 23 20 29 30 23 25 28 32  > colSums(x)  |1| 50 50 50 50 50 50 50 50 50 50  > row Sums(x)  |1| 138 104 258 
  
  Solution
r.multinomial <- function(n.out,n.trials,p)
 {
 res <- matrix(0,3,n.out);
 for (i in 1:n.out)
 {
 samp <- runif(n.trials)
 res[1,i]<-length(which(samp<=p[1]))
 res[2,i]<-length(which(samp>p[1] & samp<=p[2]))
 res[3,i]<-length(which(samp>p[2]))
 }
 return(res)
 }
Sample Output:
> p<-c(0.3,0.5,1)
> ss<-r.multinomial(10,50,p)
> ss
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,] 10 11 17 8 12 16 21 17 9 18
 [2,] 10 12 6 7 12 8 7 8 6 11
 [3,] 30 27 27 35 26 26 22 25 35 21
> colSums(ss)
 [1] 50 50 50 50 50 50 50 50 50 50
> rowSums(ss)
 [1] 139 87 274

