# Stat 6401 # Take-home Midterm # Problem 3 ################################################################################# # CLT # Normal n = 1000 # sample size k = 1000 # number of samples mu = 5 sigma = 2 SEM = sigma/sqrt(n) z_n = rnorm(n*k,mu,sigma) x = matrix(z_n,n,k) # This gives a matrix with the samples # down the columns. x.mean = ( apply(x,2,mean) - mu)/SEM x.down = -4 x.up = 4 y.up = 0.5 hist(x.mean, prob= T, xlim= c(x.down,x.up), ylim= c(0,y.up), main= 'Sampling distribution of the sample mean, Normal case') par(new= T) x = seq(x.down,x.up,0.01) y = dnorm(x,0,1) plot(x, y, type= 'l', xlim= c(x.down,x.up), ylim= c(0,y.up), xlab="", ylab="") ################################################################################# # Chi-Square n = 50 # sample size k = 1000 # number of samples df = 1 mu = n*df sigma = sqrt(2*df)*sqrt(n) z_n = rchisq(n*k,df) x = matrix(z_n,n,k) # This gives a matrix with the samples # down the columns. x.sum = ( apply(x,2,sum) - mu)/sigma x.down = -4 x.up = 4 y.up = 0.5 hist(x.sum, prob= T, xlim= c(x.down,x.up), ylim= c(0,y.up), main= 'Sampling distribution of the sample sum, Chi-Square case') par(new= T) x = seq(x.down,x.up,0.01) y = dnorm(x,0,1) plot(x, y, type= 'l', xlim= c(x.down,x.up), ylim= c(0,y.up), xlab="", ylab="")