R/multinom.R

Defines functions mymult

Documented in mymult

#' Multidimenstional simulation
#'
#' @description Function from lab 5
#'
#' @param iter number of iterations
#' @param n number of trials
#' @param p probability vector
#'
#' @return barplot and table of relative frequencies
#' @export
#'
#' @examples
#' \dontrun{mymult()}
mymult=function(iter=100,n=10, p=c(1,1,1,1)/4){
  # make a matrix to hold the samples
  #initially filled with NA's
  sam.mat=matrix(NA,nrow=n,ncol=iter, byrow=TRUE)
  #The number of categories is k
  k=length(p)
  # Make a matrix that will hold the frequencies in each sample
  tab.mat=matrix(NA,nrow=k,ncol=iter, byrow=TRUE)


  for(i in 1:iter){
    #Fill each column with a new sample
    sam.mat[,i]=sample(1:k,n,replace=TRUE, prob=p)
    #Collect all the frequencies of each of the k values
    tab.mat[,i]=table(factor(sam.mat[,i],levels=1:k))
  }
  # sum the frequecies
  freq=apply(tab.mat,1,sum)
  # put names to them
  names(freq)=1:k
  #create a barplot of refative freq
  barplot(freq/(n*iter),col=rainbow(k),main='Relative Frequency',xlab='Categories',ylab='Relative Frequency')
  obj = tab.mat
}
TylerPardun/MATH4753pard0003 documentation built on Dec. 31, 2020, 5:34 p.m.