R/mycltu.R

Defines functions mycltu

Documented in mycltu

#' @title My Central Limit Theory Uniform Function
#'
#' @param n sample size
#' @param iter how many iterations
#' @param a variable used to calculate mean, sd, variance, and the curve
#' @param b variable used to calculate mean, sd, variance, and the curve
#'
#' @return A histograph with mutliple thoeretical curves aligned to the sample distribution
#' @export
#'
#' @examples
mycltu=function(n,iter,a=0,b=10){
  ### CLT uniform
  ## my Central Limit Function
  ## Notice that I have assigned default values which can be changed when the function is called
  ## r-random sample from the uniform
  y=runif(n*iter,a,b)
  ## Place these numbers into a matrix
  ## The columns will correspond to the iteration and the rows will equal the sample size n
  data=matrix(y,nr=n,nc=iter,byrow=TRUE)
  ## apply the function mean to the columns (2) of the matrix
  ## these are placed in a vector w
  w=apply(data,2,mean)
  ## We will make a histogram of the values in w
  ## How high should we make y axis?
  ## All the values used to make a histogram are placed in param (nothing is plotted yet)
  param=hist(w,plot=FALSE)
  ## Since the histogram will be a density plot we will find the max density

  ymax=max(param$density)
  ## To be on the safe side we will add 10% more to this
  ymax=1.1*ymax
  ## Now we can make the histogram
  hist(w,freq=FALSE,  ylim=c(0,ymax), main=paste("Histogram of sample mean",
                                                 "\n", "sample size= ",n,sep=""),xlab="Sample mean")
  ## add a density curve made from the sample distribution
  lines(density(w),col="Blue",lwd=3) # add a density plot
  ## Add a theoretical normal curve
  curve(dnorm(x,mean=(a+b)/2,sd=(b-a)/(sqrt(12*n))),add=TRUE,col="Red",lty=2,lwd=3) # add a theoretical curve
  ## Add the density from which the samples were taken
  curve(dunif(x,a,b),add=TRUE,lwd=4)

}
#mycltu(n=20,iter=100000)
sarah-karamitis/math4753karamitis documentation built on May 11, 2022, 1:51 a.m.