##### Laboratory Activity A ##### ### Preamble source("http://rfs.kvasaheim.com/stat200.R") set.seed(123456) dt = read.csv("http://rfs.kvasaheim.com/data/heightPop.csv") attach(dt) dt names(dt) summary(dt) ##### ### Part I: Simple Random Sampling sampledPeople = sample(1000, 50) sampledPeople height[sampledPeople] mean(height[sampledPeople]) ##### ### Part II: Cluster Sampling galesburg = which(zipCode==61401) ## Galesburg people monmouth = which(zipCode==61462) ## Monmouth people galSample = sample(galesburg, 10) monSample = sample(monmouth, 40) galMean = mean(height[galSample]) monMean = mean(height[monSample]) ( galMean + monMean ) / 2 ##### ### Part III: Stratified Sampling mPeople = which(gender=="M") fPeople = which(gender=="F") mSample = sample(mPeople, 10) fSample = sample(fPeople, 40) mMean = mean(height[mSample]) fMean = mean(height[fSample]) # propMale = 0.75 propMale*mMean + (1-propMale)*fMean # propMale = 0.25 propMale*mMean + (1-propMale)*fMean ## Graphic propMale = seq(0, 1, by=0.01) avgHeight = propMale*mMean + (1-propMale)*fMean plot(propMale, avgHeight, pch=20, col="orange", xlab="Proportion Male", ylab="Estimated Average Height") ##### ### Part IV: Systematic Sampling start = sample(20, 1) popSample = seq(start, 1000, 20) mean(height[popSample]) ## Graphic estHeight = numeric() for( start in 1:20) { popSample = seq(start, 1000, 20) estHeight[start] = mean(height[popSample]) } plot(1:20, estHeight, pch=21, bg="tomato", xlab="Starting Person (Record)", ylab="Estimated Average Height [in.]" ) ##### ### Part V: Convenience Sampling heights = c(70,65,66,70,65) mean(heights) ### End of File