R/mycltu.R

Defines functions mycltu

Documented in mycltu

#' Central Limit Theorem Simulation for Uniform Distributions
#'
#' @param n size of sample
#' @param iter number of iterations the simulation will run using n
#' @param a lower limit of uniform distribution
#' @param b upper limit of uniform distribution
#'
#' @return Histogram of sample mean for the uniform distribution with a theoretical normal curve added.
#' @export
#'
#' @examples mycltu(n=20, iter=10000, a=0, b=10)
mycltu=function(n,iter,a=0,b=10){
  y=runif(n*iter,a,b)
  data=matrix(y,nr=n,nc=iter,byrow=TRUE)
  w=apply(data,2,mean)
  param=hist(w,plot=FALSE)
  ymax=max(param$density)
  ymax=1.1*ymax
  hist(w,freq=FALSE,  ylim=c(0,ymax), main=paste("Histogram of sample mean",
                                                 "\n", "sample size= ",n,sep=""),xlab="Sample mean")
  lines(density(w),col="Blue",lwd=3)
  curve(dnorm(x,mean=(a+b)/2,sd=(b-a)/(sqrt(12*n))),add=TRUE,col="Red",lty=2,lwd=3)
  curve(dunif(x,a,b),add=TRUE,lwd=4)

}
sche97/MATH4753 documentation built on April 17, 2020, 9:39 a.m.