##### Lesson 91: One-Sample Hypothesis Tests in R ##### # Setting the working directory setwd("path to your folder") ## Data Entry ## # FORD MPG - reported # x = c(21.7, 29, 28.1, 31.5, 24, 21.5, 28.7, 29) # Compared to Baseline mu = 26.2 rangex = c(22,32) sigma = diff(range_ford)/6 n = length(x) p = 1/n ## Hypothesis Test on the Mean ## # T-Test # xbar = mean(x) s = sd(x) to = (xbar-mu)/(s/sqrt(n)) alpha = 0.05 pvalue_ttest = pt(to,df=(n-1)) tcritical = qt(alpha,df=(n-1)) # using t-Test function in R # t.test(x,alternative = "less", mu = mu, conf.level = 0.95) # Visual Perspective # library(plotrix) null_t = rt(100000,df=(n-1)) plot(density(null_t),xlim=c(-4,4),font=2,font.lab=2,xlab="",main="Null Distribution Assuming Ho is True") abline(h=0) ablineclip(v = tcritical, y1 = 0, y2 = dt(tcritical,df=(n-1)), lty=1, lwd=2,col = "red") ablineclip(h = 0, x1 = -4, x2 = tcritical, lty=1, lwd=2,col = "red") points(to,0.01,pch=15,col="blue",cex=2) # Sign-Test # splus = length(which(x>mu)) round(dbinom(0:n,n,prob=0.5),4) round(cumsum(dbinom(0:n,n,prob=0.5)),4) pvalue_signtest = sum(dbinom(0:splus,n,prob=0.5)) # Using Binom Test function in R binom.test(splus,n,p=0.5,alternative = "less") # Visual Perspective # y1 = c("-","+","+","+","-","-","+","+") stripchart(x,ylim=c(0.99,1.5),font=2,font.lab=2) text(x,rep(1.05,length(x)),y1) ablineclip(v = mu, y1 = 0, y2 = 1.1, lty=1, lwd=2,col = "red") plot(0:n,dbinom(0:n,n,prob=0.5),type="o",font=2,font.lab=2,xlab="k",ylab="P(S+ = k)") cord.x <- c(splus,seq(0,splus),splus) cord.y <- c(0, dbinom(0:splus,n,prob=0.5), 0) polygon(cord.x,cord.y,col="grey") text(3,0.1,"P(S+ <= 5) = 0.855") # Bootstrap Test # N = 10000 xmean_null = matrix(NA,nrow=N,ncol=1) for (i in 1:N) { xboot = sample(x,n,replace=T) xmean_null[i,1] = mean(xboot) } hist(xmean_null,col="pink",xlab="Mean of the Distribution",font=2,font.lab=2,main="Null Distribution Assuming Ho is True") points(mu,10,pch=15,cex=2,col="blue") abline(v=c(quantile(xmean_null,0.95)),col="black",lwd=2,lty=2) pvalue_bootstrap = length(which(xmean_null>mu))/N ## Hypothesis Test on the Variance ## # Chi-square Test chi0 = ((n-1)*s^2)/sigma^2 pvalue_chi0 = pchisq(chi0,df=(n-1)) N = 100000 nulldist = rchisq(N,df=(n-1)) plot(density(nulldist),xlim=c(0,40),xlab="",font=2,font.lab=2,main="Null Distribution assuming Ho is True") abline(h=0) chilimit_right = qchisq(0.975,df=(n-1)) chilimit_left = qchisq(0.025,df=(n-1)) ablineclip(v = chilimit_right, y1 = 0, y2 = dchisq(chilimit_right,df=(n-1)), lty=1, lwd=2,col = "red") ablineclip(h = 0, x1 = chilimit_right, x2 = 24, lty=1, lwd=2,col = "red") ablineclip(v = chilimit_left, y1 = 0, y2 = dchisq(chilimit_left,df=(n-1)), lty=1, lwd=2,col = "red") ablineclip(h = 0, x1 = 0, x2 = chilimit_left, lty=1, lwd=2,col = "red") points(chi0,0.002,pch=15,col="blue",cex=2) # Bootstrap Test for Standard Deviation # N = 10000 xsd_null = matrix(NA,nrow=N,ncol=1) for (i in 1:N) { xboot = sample(x,n,replace=T) xsd_null[i,1] = sd(xboot) } hist(xsd_null,col="pink",xlab="Standard Deviation of the Distribution",font=2,font.lab=2,main="Null Distribution Assuming Ho is True") points(sigma,10,pch=15,cex=2,col="blue") abline(v=c(quantile(xsd_null,0.025),quantile(xsd_null,0.975)),col="black",lwd=2,lty=2) pvalue_bootstrap = length(which(xsd_null>sigma))/N ## Hypothesis Test on the Proportion ## ncars = length(which(x<22)) plot(0:n,dbinom(0:n,n,prob=p),type="o",xlab="Number of cars with MPG less than the range",ylab="P(X=x)",font=2,font.lab=2) x_at_alpha = qbinom(0.95,n,prop) # approx quantile for 10% rejection #abline(v=x_at_alpha) cord.x <- c(x_at_alpha,seq(x_at_alpha,n),x_at_alpha) cord.y <- c(0, dbinom(x_at_alpha:n,n,prob=p), 0) polygon(cord.x,cord.y,col="pink") points(ncars,0.002,pch=15,col="blue",cex=2) pval = sum(dbinom(ncars:n,n,prob=p)) print(pval) # Bootstrap Test # N = 10000 xprop_null = matrix(NA,nrow=N,ncol=1) for (i in 1:N) { xboot = sample(x,n,replace=T) xprop_null[i,1] = length(which(xboot<22))/n } hist(xprop_null,col="pink",xlab="Proportion of the Distribution",font=2,font.lab=2,main="Null Distribution Assuming Ho is True") points(prop,10,pch=15,cex=2,col="blue") abline(v=c(quantile(xprop_null,0.95)),col="black",lwd=2,lty=2) pval=length(which(xprop_null>prop))/N