Chapter 13. Hypothesis Testing#

Figure 13.1. Null and Alternative Hypotheses#

suppressPackageStartupMessages(library(spatstat)) 

plot.new()
plot.window(c(-5,5),c(-5,5))
W1 <- ellipse(a=.82,b=1,centre=c(0,0),phi=0,npoly=1024)
W2 <- ellipse(a=4.2,b=5,centre=c(0,0),phi=0,npoly=1024)
plot(W1,add=TRUE)
plot(W2,add=TRUE)
text(0,0,expression(H[0]),cex=1)
text(2.7,2.7,expression(H[1]),cex=1)

Figure 13.2. Acceptance and Rejection Regions: Early Childhood Education Example#

plot.new()
plot.window(c(-.5,5),c(-.5,5))
polygon(c(5,5,1),c(0,4,0),col=gray(.8),border=NA)
arrows(0,0,5,0,angle=20,length=.1)
arrows(0,0,0,5,angle=20,length=.1)
lines(c(1,5),c(0,4))
points(3.5,4.2,pch=19,col="black",cex=.75)
text(3.5,4,"a",cex=.75)
points(4.5,2.5,pch=19,col="black",cex=.75)
text(4.5,2.3,"b",cex=.75)
text(1.5,3,expression(paste(bar(W)[1]-bar(W)[2],""<=A)),cex=.75)
text(4,1.5,expression(bar(W)[1]-bar(W)[2]>A),cex=.75)
text(3.5,.75,"Rejection Region",cex=.75)
text(1.2,2.3,"Acceptance Region",cex=.75)
text(4.7,-.3,expression(bar(W)[1]),cex=.75)
text(-.3,4.7,expression(bar(W)[2]),cex=.75)

Figures 13.3. Acceptance and Rejection Regions: Bus Travel Example#

plot.new()
plot.window(c(-.5,5),c(-.5,5))
polygon(c(5,5,1),c(0,4,0),col=gray(.8),border=NA)
polygon(c(0,0,4),c(1,5,5),col=gray(.8),border=NA)
lines(c(0,5),c(0,0))
lines(c(0,0),c(0,5))
lines(c(5,5),c(0,5))
lines(c(0,5),c(5,5))
lines(c(0,4),c(1,5))
lines(c(1,5),c(0,4))
lines(c(0,5),c(0,5))
text(2.7,-.3,expression(X[1]),cex=.75)
text(-.3,2.7,expression(X[2]),cex=.75)
text(1,3.5,expression(X[2]-X[1]>B),cex=.75)
text(2.3,2.37,expression(paste("|",X[2]-X[1],"|",""<=B)),cex=.75)
text(3.5,1,expression(X[1]-X[2]>B),cex=.75)
points(3,4.5,pch=19,col="black",cex=.75)
text(3,4.8,"a",cex=.75)
points(4.3,3.8,pch=19,col="black",cex=.75)
text(4.3,3.6,"b",cex=.75)
points(4.5,2.5,pch=19,col="black",cex=.75)
text(4.5,2.3,"c",cex=.75)
text(1,4.5,"Rejection Region",cex=.75)
text(3,.5,"Rejection Region",cex=.75)
text(1.2,1.3,"Acceptance",cex=.75)
text(1.2,1.0,"Region",cex=.75) 

Figure 13.4. Acceptance and Rejection Regions for Test Statistic#

wd <- 1.4

# One-Sided Test
plot.new()
plot.window(c(-5,5),c(-1,1))
arrows(0,0,5,0,angle=20,length=.1,lwd=wd)
arrows(0,0,-5,0,angle=20,length=.1,lwd=wd)
lines(c(0,0),c(-.05,.05),lwd=wd)
text(2.5,.2,expression(paste("Reject ",H[0])))
text(-2.5,.2,expression(paste("Accept ",H[0])))
text(0,-.2,expression(c))
text(2.5,-.2,expression(T>c))
text(-2.5,-.2,expression(paste(T,""<=c))) 
# Two-Sided Test
plot.new()
plot.window(c(-5,5),c(-1,1))
arrows(0,0,5,0,angle=20,length=.1,lwd=wd)
arrows(0,0,-5,0,angle=20,length=.1,lwd=wd)
lines(c(2,2),c(-.05,.05),lwd=wd)
lines(c(-2,-2),c(-.05,.05),lwd=wd)
text(3.5,.2,expression(paste("Reject ",H[0])))
text(-3.5,.2,expression(paste("Reject ",H[0])))
text(0,.2,expression(paste("Accept ",H[0])))
text(2,-.15,expression(c))
text(-2,-.15,expression(-c))
text(3.5,-.2,expression(T>c))
text(-3.5,-.2,expression(paste(T<"",-c)))
text(0,-.2,expression(paste("| ",T," |",""<=c))) 

Figure 13.5. Null Sampling Distribution for a One-Sided Test#

x <- seq(-3,3,by=0.01)
c <- 1.645
f <- dnorm(x)
fc <- dnorm(c)
x2 <- seq(1.65,3,by=0.01)
f2 <- dnorm(x2) 

plot(x,f,type="l",lty=1,xaxs="i",yaxs="i",ylab="",xlab="",ylim=c(-.05,.4),xaxt="n",yaxt="n",bty="n")
polygon(c(3,c,c,x2),c(0,0,fc,f2),col=gray(.8),border=NA)
lines(x,f)
abline(h=0)
lines(c(c,c),c(0,fc))
lines(c(0,0),c(-.005,.005))
text(0,-.02,expression(theta[0]),cex=.8)
text(c,-.02,"c",cex=.8)
text(-1,.02,expression(paste("Accept ",H[0])),cex=.75)
text(2,.02,"Reject",cex=.75)
text(-1.2,.3,expression(F[0]),cex=.75) 

Figure 13.6. Sampling Distribution Under the Alternative for a One-Sided Test#

x <- seq(-3,5,by=0.01)
f <- dnorm(x)
t <- 1.3
f1 <- dnorm(x-t)
f1c <- dnorm(c-t)
x2 <- seq(1.65,5,by=0.01)
f2 <- dnorm(x2)
f3 <- dnorm(x2-t)
 
plot(x,f,type="l",lty=1,xaxs="i",yaxs="i",ylab="",xlab="",xlim=c(-3,5),ylim=c(-.05,.4),xaxt="n",yaxt="n",bty="n")
polygon(c(3,c,c,x2),c(0,0,fc,f2),col=gray(.8),border=NA)
polygon(c(c,c,x2,rev(x2)),c(fc,f1c,f3,rev(f2)),col=gray(.9),border=NA)
abline(h=0)
lines(x,f)
lines(x,f1)
lines(c(0,0),c(-.005,.005))
text(0,-.02,expression(theta[0]),cex=.8)
lines(c(t,t),c(-.005,.005))
text(t,-.02,expression(theta[1]),cex=.8)
lines(c(c,c),c(0,fc))
lines(c(c,c),c(fc,f1c),lty=2)
text(c,-.02,"c",cex=.8)
text(0,.02,expression(paste("Accept ",H[0])),cex=.75)
text(3.0,.02,"Reject",cex=.75)
text(-1.2,.3,expression(F[0]),cex=.75)
text(2.5,.3,expression(F[1]),cex=.75) 

Figure 13.7. Power Function#

x <- seq(0,4,by=.01)
a1 <- .1
a2 <- .05
a3 <- .005
z1 <- qnorm(1-a1)
z2 <- qnorm(1-a2)
z3 <- qnorm(1-a3)
p1 <- 1 - pnorm(z1-x)
p2 <- 1 - pnorm(z2-x)
p3 <- 1 - pnorm(z3-x)

plot(x,p1,type="l",lty=1,xaxs="i",yaxs="i",ylab="Power",xlab=expression(theta/sigma),xlim=c(0,4),ylim=c(0,1),bty="n",yaxt="n",cex.lab=.8)
axis(side=2,seq(0,1,.10))
lines(x,p2)
lines(x,p3)
text(2.75,.4,expression(alpha==0.005),cex=.75)
text(2,.5,expression(alpha==0.05),cex=.75)
text(1.2,.6,expression(alpha==0.1),cex=.75)

Figure 13.8 Null Sampling Distribution for a Two-Sided Test#

x <- seq(-3,3,by=0.01)
c <- 1.96
f <- dnorm(x)
fc <- dnorm(c)
x2 <- seq(1.96,3,by=0.01)
f2 <- dnorm(x2)
x3 <- seq(-3,-1.96,by=0.01)
f3 <- dnorm(x3)
 
plot(x,f,type="l",lty=1,xaxs="i",yaxs="i",ylab="",xlab="",ylim=c(-.05,.4),xaxt="n",yaxt="n",bty="n")
polygon(c(3,c,c,x2),c(0,0,fc,f2),col=gray(.8),border=NA)
polygon(c(-3,-c,-c,rev(x3)),c(0,0,fc,rev(f3)),col=gray(.8),border=NA)
lines(x,f,lty=1)
abline(h=0)
lines(c(c,c),c(0,fc))
lines(c(-c,-c),c(0,fc))
lines(c(0,0),c(-.005,.005))
text(0,-.02,expression(theta[0]),cex=.8)
text(c,-.02,"c",cex=.8)
text(-c,-.02,"c",cex=.8)
text(0,.1,expression(paste("Accept ",H[0])),cex=.75)
text(2.25,.01,"Reject",cex=.7)
text(-2.25,.01,"Reject",cex=.7)
text(-1.2,.3,expression(F[0]),cex=.75) 

Figure 13.9. Sampling Distribution Under Alternative for a Two-Sided Test#

x <- seq(-3,5,by=0.01)
f <- dnorm(x)
t <- 1.3
f1 <- dnorm(x-t)
f1c <- dnorm(c-t)
x2 <- seq(1.96,5,by=0.01)
f2 <- dnorm(x2)
f4 <- dnorm(x2-t)

plot(x,f,type="l",lty=1,xaxs="i",yaxs="i",ylab="",xlab="",xlim=c(-3,5),ylim=c(-.05,.4),xaxt="n",yaxt="n",bty="n")
polygon(c(3,c,c,x2),c(0,0,fc,f2),col=gray(.8),border=NA)
polygon(c(-3,-c,-c,rev(x3)),c(0,0,fc,rev(f3)),col=gray(.8),border=NA)
polygon(c(c,c,x2,rev(x2)),c(fc,f1c,f4,rev(f2)),col=gray(.9),border=NA)
lines(x,f,lty=1)
abline(h=0)
lines(x,f1)
lines(c(0,0),c(-.005,.005))
text(0,-.02,expression(theta[0]),cex=.8)
lines(c(t,t),c(-.005,.005))
text(t,-.02,expression(theta[1]),cex=.8)
lines(c(c,c),c(0,fc))
lines(c(-c,-c),c(0,fc))
lines(c(c,c),c(fc,f1c),lty=2)
text(c,-.02,"c",cex=.8)
text(-c,-.02,"c",cex=.8)
text(-1.2,.3,expression(F[0]),cex=.75)
text(2.5,.3,expression(F[1]),cex=.75)
text(0.5,.1,expression(paste("Accept ",H[0])),cex=.75)
text(-2.3,.008,"Reject",cex=.7)
text(2.7,.04,"Reject",cex=.75) 

Figure 13.10. Likelihood Ratio#

x <- seq(.558,10,by=0.01)
xbar <- 2
L <- -log(x) - xbar/x
x0 <- 5
x1 <- xbar
L0 <- -log(x0) - xbar/x0
L1 <- -log(x1) - xbar/x1

plot(x,L,type="l",lty=1,xaxs="i",yaxs="i",ylab="",xlab="",ylim=c(-3.15,max(L)+.2),xaxt="n",yaxt="n",bty="n",cex.lab=.75)
abline(h=-3)
points(x0,L0,pch=19,col="black",cex=.8)
points(x1,L1,pch=19,col="black",cex=.8)
points(x0,-3,pch=19,col="black",cex=.8)
points(x1,-3,pch=19,col="black",cex=.8)
arrows(x0,-3,x0,L0,angle=20,length=.15,lty=1)
text(x0,-3.08,expression(theta[0]),cex=.8)
text(x0,L0+.1,expression(l[n](theta[0])),cex=.8)
arrows(x1,L1,x1,-3,angle=20,length=.15,lty=1)
text(x1,-3.08,expression(hat(theta)),cex=.8)
text(x1,L1+.1,expression(l[n](hat(theta))),cex=.8)
lines(c(x0,x1),c(L0,L0))
lines(c(x1,x1),c(L0,L1),lwd=2)
text(x1-.4,(L0+L1)/2,expression(LR[n]),cex=.8)