assn2_solns (4).doc - Stat 359 Assignment 2 Solutions Thanks to Dr Laura Cowen for preparing these solutions 1 Central Limit Theorem First initialize

# assn2_solns (4).doc - Stat 359 Assignment 2 Solutions...

• 10
• 100% (1) 1 out of 1 people found this document helpful

This preview shows page 1 - 3 out of 10 pages.

Stat 359 Assignment 2 Solutions Thanks to Dr. Laura Cowen for preparing these solutions. 1. Central Limit Theorem # First initialize the size of the samples and the number of samples you want to take. # Also initialize your matrices that will hold your sample means to zero. size<-c(10,20,50,100) samp<-c(10,100,1000) u10<- matrix(rep(0,times=length(size)*10), nrow=length(size), ncol=10) u100<- matrix(rep(0,times=length(size)*100), nrow=length(size), ncol=100) u1000<- matrix(rep(0,times=length(size)*1000), nrow=length(size), ncol=1000) b10<- matrix(rep(0,times=length(size)*10), nrow=length(size), ncol=10) b100<- matrix(rep(0,times=length(size)*100), nrow=length(size), ncol=100) b1000<- matrix(rep(0,times=length(size)*1000), nrow=length(size), ncol=1000) p10<- matrix(rep(0,times=length(size)*10), nrow=length(size), ncol=10) p100<- matrix(rep(0,times=length(size)*100), nrow=length(size), ncol=100) p1000<- matrix(rep(0,times=length(size)*1000), nrow=length(size), ncol=1000) # This part of the code could have be done various ways. Here I take 3 loops that will # produce 9 matrices of means. For instance, u10 is a 4x10 matrix where the rows # represent the size of the samples (10,20,50,100) and the columns are the 10 sampled # means. for (k in samp){ # k indicates the number of samples 10, 100,l000 index<-0 # counter for the rows in the matrices for (i in size){ # i changes with size of the sample 10, 20, 50, 100 index<-index+1 # increase the row number by 1 for (j in 1:k){ # j indicates the sample number, 1 to sample size if (k==10){ # when size of the sample is 10 to produce a 4x10 matrix u10[index,j]<-mean(runif(i, min=0,max=1)) p10[index,j]<- mean(rpois(i, 5)) b10[index,j]<- mean(rbinom(i, 1, 0.20)) } if (k==100){ # when size of the sample is 100 to produce a 4x100 matrix u100[index, j]<- mean(runif(i, min=0,max=1)) p100[index, j]<- mean(rpois(i, 5)) b100[index, j]<- mean(rbinom(i, 1, 0.20)) } if (k==1000){ # when size of the sample is 1000 to produce a 4x1000 matrix u1000[index, j]<- mean(runif(i, min=0,max=1)) p1000[index, j]<- mean(rpois(i, 5)) b1000[index, j]<- mean(rbinom(i, 1, 0.20)) } } } } To conserve plots, put 12 on a page, 1 page for each distribution. Make sure that for comparison purposes the x axis are the same so that we can see how the distributions change. Note that we could also keep the y axis the same, however it may be too different from plot to plot. 1
par(mfrow=c(4,3)) # Uniform: hist(u10[1,], main=”10 means of size 10”, xlab=”Uniform(0,1)”, xlim=c(0,0.8)) hist(u100[1,], main=”100 means of size 10”, xlab=”Uniform(0,1)” , xlim=c(0,0.8)) hist(u1000[1,], main=”1000 means of size 10”, xlab=”Uniform(0,1)” , xlim=c(0,0.8)) hist(u10[2,], main=”10 means of size 20”, xlab=”Uniform(0,1)” , xlim=c(0,0.8))