R/myBin.R

Defines functions myBin

Documented in myBin

#' Binomial simulation function
#'
#' This function will create a binomial simulation. This function was provided by Dr. Stewart
#' for use within the Applied Statistical Methods course taught at OU.
#'
#' @param iter integer number of iterations
#' @param n integer sample size
#' @param p decimal probability for a success
#'
#' @examples
#' myBin(1000,1,0.5)
#' myBin(1000,10,0.7)
#'
#' @importFrom grDevices rainbow
#'
#' @export


# set default values
myBin = function(iter=100,n=10, p=0.5){
  # 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
}
josephhgrimes/MyPackage documentation built on March 31, 2022, 3:22 a.m.