############################################### # p_1-p_2: sampling distribution # # Repeat 1,000 extractions from a normal # ############################################### sampling.prop.diff <-c() R = 0 # number of replication set.seed(123) repeat{ p1<-0.55 p2<-0.75 R=R+1 n1 = n2 = 10 # ...try to change N value to obtain different np and n(1-p) values... # Random sample from binomial populations, 1 and 2:     X1<-rbinom(1,size=n1,prob=p1) p1_hat=X1/n1 X2<-rbinom(1,size=n2,prob=p2) p2_hat=X2/n2 sampling.prop.diff <-c(sampling.prop.diff,p2_hat-p1_hat) cat("R=",R,"\n") if(R==1000)break() } #-------------------------------------------------- # Histogram of sampling distribution: plot(density(sampling.prop.diff), type="l",lwd=2,col="white",bty="n", xlim=c( (p2-p1)-3*sqrt((p1*(1-p1))/n1+(p2*(1-p2))/n2), (p2-p1)+3*sqrt((p1*(1-p1))/n1+(p2*(1-p2))/n2) ), main=paste("N = ",n1),xlab=expression(hat(p)[2]-hat(p)[1])) polygon(density(sampling.prop.diff), col = rgb(0.25, 0.99, 0.25, alpha = 0.5),border="white") abline(v=p2-p1,lwd=1) #-------------------------------------------------- mean(sampling.prop.diff) p2-p1 sd(sampling.prop.diff) sqrt((p1*(1-p1))/n1+(p2*(1-p2))/n2) #-------------------------------------------------- n1*p1; n1*(1-p1) n2*p2; n2*(1-p2) #-------------------------------------------------- ############################################### # Visualize the two populations # # Normal vs asymmetric chi-square # ############################################### # 1) Normal Population # --------------------- mu1<-12 sd1<-1.5 Population1<-rnorm(50000,mean=mu1,sd=sd1) # 2) Asimmetric chi-square Population # ------------------------------------ mu2=v=8 sd2<-sqrt(2*v) Population2<-rchisq(50000,df=v) # ------------------------------------ # Histogram of population distribution: plot(density(Population1), type="l",lwd=2,col="white",bty="n", main="The two populations",xlim=c(0,20),xaxt="n") axis(1,at=c(0,2,4,6,8,10,12,14,16,18,20)) polygon(density(Population1), col = rgb(0.25, 0.25,0.99, alpha = 0.5),border="white") abline(v=mu1,lwd=2,col="blue") polygon(density(Population2), col = rgb(0.99,0.25, 0.25, alpha = 0.5),border="white") abline(v=mu2,lwd=2,col="red") ############################################### # mu1-mu2: sampling distribution # # Repeat 1,000 extractions from a normal # ############################################### sampling.mean.diff <-c() R = 0 # number of replication # 1) Normal Population # --------------------- mu1<-12 sd1<-1.5 # 2) Asimmetric chi-square Population # ------------------------------------ mu2=v=8 sd2<-sqrt(2*v) set.seed(123) repeat{ R=R+1 n1 = n2 = 50 # try to change N... # Random sample from normal population (1)     X1<-rnorm(n1,mean=mu1,sd=sd1) mu1_hat=sum(X1)/n1 # Random sample from asimmetric population (2)     X2<-rchisq(n2,df=v) mu2_hat=sum(X2)/n2 sampling.mean.diff <-c(sampling.mean.diff,mu1_hat-mu2_hat) cat("R=",R,"\n") if(R==1000)break() } #-------------------------------------------------- # Histogram of sampling distribution: plot(density(sampling.mean.diff), type="l",lwd=2,col="white",bty="n", main=paste("N = ",n1),xlab=expression(bar(x)[1]-bar(x)[2])) polygon(density(sampling.mean.diff), col = rgb(0.25, 0.99, 0.25, alpha = 0.5),border="white") abline(v=mu1-mu2,lwd=1) #-------------------------------------------------- mean(sampling.mean.diff) mu1-mu2 sd(sampling.mean.diff) sqrt(sd1^2/n1+sd2^2/n2) #-------------------------------------------------- #Reading in the data cellPhoneReactions <- read.csv(file='https://raw.githubusercontent.com/artofstat/data/master/Chapter10/cell_phone_reaction_times_long.csv') #To subset data to make the two groups cell <- subset(cellPhoneReactions, Group =='Cell') control <- subset(cellPhoneReactions, Group == 'Control') #To make a hypothesis test comparing the two means t.test(cell$ReactionTime, control$ReactionTime) ## ## Welch Two Sample t-test ## ## data: cell$ReactionTime and control$ReactionTime ## t = 2.6307, df = 56.696, p-value = 0.01095 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## 12.31658 90.87092 ## sample estimates: ## mean of x mean of y ## 585.1875 533.5938