#Example 3 # Testing hypothesis about the population proportion #assumed population proportion pi0 <- 0.20 #sample size n <- 200 # number of families in the sample living below # the poverty level x <- 45 #sample proportion p <- x/n p #hypotheses #H0: pi = 0.20 #H1: pi not= 0.20 #test statistic ts <- (p - pi0)/sqrt(pi0*(1-pi0)/n) ts #lower critical value - 2.5th percentile (0.025th quantile) lower_z025 <- qnorm(.025) lower_z025 #upper critical value - 97.5th percentile (0.975th quantile) upper_z025 <- qnorm(.975) upper_z025 #plot of standard normal density function curve(dnorm(x), from = -4, to = 4, main = 'Standard Normal Distribution', ylab = 'Density', xlab = '') #location of the test statistic and critical values abline(v=ts,col="blue") #location of the lower critical value abline(v=lower_z025,col="red") #location of the upper critical value abline(v=upper_z025,col="red") #determination of the p-value #plot of standard normal density function curve(dnorm(x), from = -4, to = 4, main = 'Standard Normal Distribution', ylab = 'Density', xlab = '') #location of the negative and postive absolute values of #test statistic with hosizonal line showing the value of #the density function abline(v=abs(ts),col="blue") abline(v=-abs(ts),col="blue") #value of the density function when the quantile is the #postive or negative of absolute value of the test statistic dnorm(abs(ts)) dnorm(-abs(ts)) abline(h=dnorm(abs(ts)),col="red") #P(Z > abs(ts)) pv_up <- pnorm(abs(ts),lower.tail=FALSE) pv_up #P(Z < -abs(ts)) pv_lo <-pnorm(-abs(ts),lower.tail=TRUE) pv_lo pvalue <-pv_up + pv_lo pvalue