NSamp = seq(10,100,10) ## The sample sizes to consider nrep = 1000 ## The number of data sets to generate ## A place to store the results. V = NULL ## Vary the sample size over 10, 20, ..., 100. for (k in 1:length(NSamp)) { ## The sample size to use in this iteration. nsamp = NSamp[k] ## Generate a 1000xnsamp array of standard normal draws. D = rnorm(nrep*nsamp) X = array(D, c(nrep,nsamp)) ## Get the mean of each row of X. Y = apply(X, 1, mean) ## Calculate the sample variance of Y. V[k] = var(Y) } ## Plot the simulation results. plot(NSamp, V, t='b', xlab='Sample size', ylab='Variance of Xbar') ## Overplot the exact results. lines(NSamp, 1/NSamp, t='b', col='red') ## Add a legend. legend(x='topright', legend=c('Simulation', 'Theory'), col=c('black', 'red'), lty=c(1,1))