# illustrating rejection regions and the power of a statistical test # graphic for lower tail test of population mean with known variance # http://www.r-tutor.com/elementary-statistics/hypothesis-testing/lower-tail-test-population-mean-known-variance alpha <- .05 z.alpha <- qnorm(1-alpha) xs <- seq(from=-5, to=5, by=.01) poly.xs <- seq(from=-5, to=-z.alpha, by=.01) test.statistic <- -4.5644 # pdf for X ~ N(0, 1) plot(xs, dnorm(xs), type="l", col="dark red", xlab="z (orange = rejection region; pin = test.statistic)", ylab="probability", main="X ~ N(0,1) : Rejection Region") # rejection region polygon(x=c(-5, poly.xs, -z.alpha), y=c(0,dnorm(poly.xs),0), col="orange", border="red") # pin indicating test.statistic lines(x=c(test.statistic, test.statistic), y=c(0, .03), lwd="2", col="violetred3") points(x=test.statistic, y=.03, pch=19, col="violetred3") # power of a statistical test mu1 <- 1 sigma1 <- 1 mu2 <- -2 sigma2 <- 1 alpha <- .15 z.alpha <- qnorm(1-alpha, mean=mu1, sd=sigma1) xs <- seq(from=-5, to=5, by=.01) plot(c(-5, 5), c(0, .4), type="n", xlab="z (with test sensitivity cutoff in purple))", ylab="probability (power == area of the blue region)", main="Power of a Statistical Test\nH0 : mu = mu0 (red) vs. Ha : mu = mu1 (orange)\npower == area of the blue region") lines(xs, dnorm(xs, mean=mu1, sd=sigma1), type="l", col="red") lines(xs, dnorm(xs, mean=mu2, sd=sigma2), type="l", col="orange") # rejection region lines(x=c(mu1 - z.alpha, mu1 - z.alpha), y=c(0, dnorm(mu1 - z.alpha, mean=mu2, sd=sigma2)), lwd="2", col="violetred4") # power poly.xs <- seq(from=-5, to=mu1 - z.alpha, by=.01) polygon(x=c(-5, poly.xs, mu1 - z.alpha), y=c(0,dnorm(poly.xs, mean=mu2, sd=sigma2),0), col="skyblue", border="violetred4") lines(xs, dnorm(xs, mean=mu1, sd=sigma1), type="l", col="red")