# Script for generating sampling variance of means # Wipes any pictures you generated close.screen(,all.screens=T) # Makes 4 Panels for plotting split.screen(c(2,2)) #Assigns to Screen 1 (of the two by two panel set) screen(1) # Helper function to make R simulate X observations and calculate the mean mean_rand<-function(X){mean(rnorm(X,0,1))} # We are going to explore how the number of samples affects the estimate of mean # I use the sapply function, which is part of the apply family # apply is a very powerful tool which can take the place of making loops in R # Loops are terrible in R--they are very very slooooooooow # Basic principle of apply is that R applies the function you specify to vector or table you specify # For more information on apply/sapply ?apply and ?sapply # We will plot the output of this function: sapply(rep(10,500),mean_rand) # The rep function is telling R to replicate the value 10, 500 times # which looks like: [10 10 10 10 .... 10] # sapply is then taking that vector and applying the function mean_rand to each element # So we have 500 elements of value 10, so R will generate mean_rand(10) 500 times # Plot.density plots a density object. Density evaluates your data (similar to histograms) # rnorm randomly creates data following a normal distribution with default mean and stand. dev. # the only argument is the size of simulation 10 in this case, then increasing plot(density(sapply(rep(10,500),mean_rand)),xlab="Estimate of Mean",ylab="Frequency",main="10 sample size mean estimate",xlim=c(-1,1)) screen(2) plot(density(sapply(rep(100,500),mean_rand)),xlab="Estimate of Mean",ylab="Frequency",main="100 sample size mean estimate",xlim=c(-1,1)) screen(3) plot(density(sapply(rep(1000,500),mean_rand)),xlab="Estimate of Mean",ylab="Frequency",main="1,000 sample size mean estimate",xlim=c(-1,1)) screen(4) plot(density(sapply(rep(10000,500),mean_rand)),xlab="Estimate of Mean",ylab="Frequency",main="10,000 sample size mean estimate",xlim=c(-1,1))