################ R Simple Simulation ################ ################## Rolling a die 10 times ################## vals=c(0,1) counts=c(4,2) urn=rep(vals,times=counts) urn sample(urn,size=10,replace=TRUE) sample(urn,size=10,replace=TRUE) sample(urn,size=10,replace=TRUE) sample(urn,size=10,replace=TRUE) mean(sample(urn,size=10,replace=TRUE)) mean(sample(urn,size=10,replace=TRUE)) mean(sample(urn,size=10,replace=TRUE)) mean(sample(urn,size=10,replace=TRUE)) results=replicate(10000,mean(sample(urn,size=10,replace=TRUE))) head(results) results[4000] results[131] mean(results) sd(results) ### to make the plot plot(table(results),type="h",ylab="Counts") sum(results>=.8) ################## Polio Vaccine ################## vals=c(0,1) counts=c(399802,198) urn=rep(vals,times=counts) results=replicate(10000,sum(sample(urn,200000,replace=FALSE))) sum(results<=56) ################## Calcium Supplement ################## calcium=c(7,-4,18,17,-3,-5,1,10,11,-2) placebo=c(-1,12,-1,-3,3,-5,5,2,-11,-1,-3) urn=c(calcium,placebo) urn results=replicate(10000,mean(sample(urn,size=10,replace=FALSE))) mean(results>=mean(calcium)) hist(results)