#' mybin
#'
#' Generates samples with a binomial distribution given number of samples, number of iterations,
#' and a probability
#'
#' @param iter number of samples to generate
#' @param n number of trials
#' @param p probability of success
#'
#' @return a table representing the observed number of successes
#'
#' @export
#'
#' @examples
#'
#' mybin(iter=100,n=10,p=0.7)
#' mybin(iter=200,n=10,p=0.7)
#' mybin(iter=500,n=10,p=0.7)
#' mybin(iter=1000,n=10,p=0.7)
#' mybin(iter=10000,n=10,p=0.7)
mybin=function(iter,n,p)
{
# make a matrix to hold the samples
#initially filled with NA's
sam.mat=matrix(NA,nrow=n,ncol=iter, byrow=TRUE)
#Make a vector to hold the number of successes in each trial
succ=c()
for( i in 1:iter)
{
#Fill each column with a new sample
sam.mat[,i]=sample(c(1,0),n,replace=TRUE, prob=c(p,1-p))
#Calculate a statistic from the sample (this case it is the sum)
succ[i]=sum(sam.mat[,i])
}
#Make a table of successes
succ.tab=table(factor(succ,levels=0:n))
#Make a barplot of the proportions
barplot(succ.tab/(iter), col=rainbow(n+1), main="Binomial simulation", xlab="Number of successes")
succ.tab/iter
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.