# typical hours/week spent on HW STAT 205H, Fall 2017 h=c(22,15,7,10,10,15,14,10,20,20,5,14,20,10,5,10,15,3) m=length(h) # population size n=5 # sample size # compute one mean of size n=5 ybar=0 for(j in 1:n){ index=ceiling(runif(1)*m) cat("Student",index,"spends",h[index],"hours. \n") ybar=ybar+h[index]} cat("Mean of all",n,"is ybar =",ybar/n,"hours. \n") cat("Population mean is ",mean(h),"hours. \n") # make histogram of 1000 sample ybar's, each n=5 total=1000 mu=mean(h) means=rep(0,total) for(i in 1:total){ ybar=0 for(j in 1:n){ybar=ybar+h[ceiling(runif(1)*m)]} ybar=ybar/n means[i]=ybar } par(mfrow=c(2,1)) hist(h,freq=FALSE,main="Population density",xlim=c(0,25),xlab="HW hours") lines(density(h),col="blue") hist(means,freq=FALSE,xlim=c(min(h),max(h)),main="Density of ybar for n=5") x=seq(0,20,length=1000) lines(x,dnorm(x,mean(h),sd(h)/sqrt(n)),col="blue") # use 5 randomly selected students to make CI for mu=mean in class t.test(sample(h,5,replace=T)) # use all 18 randomly students to make CI for mu=mean among honors students t.test(h)