library(scales) alpha(colours(), 0.5) windows(6,4) par(oma=c(0,0,0,0),mar=c(2,1,0,0)) mean=0; sd=1 lb=1.64; ub=4 x <- seq(-4,4,length=100)*sd + mean hx <- dnorm(x,mean,sd) plot(x, hx, type="n", xlim=c(-4, 8), ylim=c(0, 0.5), ylab = "", xlab = "", axes=FALSE) axis(1, lwd=2,cex.axis=1.3,line=-0.5) shift = qnorm(1-0.05, mean=0, sd=1)*1.7 xfit2 <- x + shift yfit2 <- dnorm(xfit2, mean=shift, sd=1) # Print null hypothesis area col_null = "#FF0000E3" lines(x, hx, lwd=2) i <- x >= lb & x <= ub polygon(c(lb,x[i],ub), c(0,hx[i],0), col="#FF0000E3",lwd=3,ylim=c(0,0.48)) area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd) # The alternative hypothesis area ## The red - underpowered area lb <- min(xfit2) ub <- round(qnorm(.95),2) col1 = "#3D3D3D80" i <- xfit2 >= lb & xfit2 <= ub polygon(c(lb,xfit2[i],ub), c(0,yfit2[i],0), col=col1) ## The green area where the power is col2 = "#76EE0080" lines(c(ub,xfit2,max(xfit2)), c(0,yfit2,0),lwd=2) # Outline the alternative hypothesis lines(xfit2, yfit2, lwd=2) legend("topright", inset=.05, c(expression(paste("Type I error (",alpha,")")),expression(paste("Type II error (",beta,")"))), fill=c(col_null, col1, col2), horiz=FALSE) lines(c(1.64,1.64),c(0,0.45),col="blue") mtext(side=1,expression(paste("1-",alpha)),cex=1.5,line=-4.5,adj=0.33) mtext(side=1,expression(paste("1-",beta)),cex=1.5,line=-4.5,adj=0.62) arrows(2.9, 0.03, 2.1, 0.01, lwd=2) mtext(side=1,expression(paste(alpha)),cex=1.5,line=-2.4,adj=0.59) mtext(side=1,expression(paste(beta)),cex=1.5,line=-2,adj=0.43) mtext(side=3,expression("H"[0]),line=-3.5,adj=0.34,cex=1.5) mtext(side=3,expression("H"[a]),line=-3.5,adj=0.61,cex=1.5)