Hypothesis Test for a Proportion

knitr::opts_chunk$set(echo = TRUE,comment=NA)
library(interpretCI)
library(glue)
x<-params$result
#x=propCI(acs$Dx)

two.sided<-greater<-less<-FALSE
if(x$result$alternative=="two.sided") two.sided=TRUE
if(x$result$alternative=="less") less=TRUE
if(x$result$alternative=="greater") greater=TRUE

twoS="The null hypothesis will be rejected if the sample proportion is too big or if it is too small."
lessS="The null hypothesis will be rejected if the sample proportion is too small."
greaterS="The null hypothesis will be rejected if the sample proportion is too big."

This document is prepared automatically using the following R command.

call=paste0(deparse(x$call),collapse="")
x1=paste0("library(interpretCI)\nx=",call,"\ninterpret(x)")
textBox(x1,italic=TRUE,bg="grey95",lcolor="grey50")

Problem

string=glue("The CEO of a large electric utility claims that {(1-x$result$P)*100} percent of his {x$result$n*100} customers are very satisfied with the service they receive. To test this claim, the local newspaper surveyed {x$result$n} customers, using simple random sampling. Among the sampled customers, {english2((1-x$result$p)*100,1)} percent say they are very satisified. Based on these findings, can we reject the CEO's hypothesis that {round((1-x$result$P)*100,2)}% of the customers are very satisfied? Use a {x$result$alpha} level of significance.")

textBox(string)

Confidence interval of a sample proportion

The approach that we used to solve this problem is valid when the following conditions are met.

Solution

This approach consists of four steps:

1. State the hypotheses

The first step is to state the null hypothesis and an alternative hypothesis.

$$Null\ hypothesis(H_0): P r ifelse(two.sided,"=",ifelse(less,">=","<=")) r x$result$P$$ $$Alternative\ hypothesis(H_1): P r ifelse(two.sided, "\\neq" ,ifelse(less,"<",">")) r x$result$P$$

Note that these hypotheses constitute a r ifelse(two.sided,"two","one")-tailed test. r ifelse(two.sided,twoS,ifelse(less,lessS,greaterS)).

2. Formulate an analysis plan

For this analysis, the significance level is `r x$result$alpha``. The test method, shown in the next section, is a one-sample z-test.

2. Select a confidence level.

In this analysis, the confidence level is defined for us in the problem. We are working with a r (1-x$result$alpha)*100% confidence level.

3. Analyze sample data

Using sample data, we calculate the standard deviation (sd) and compute the z-score test statistic (z).

$$sd=\sqrt{\frac{P\times(1-P)}{n}}$$ $$sd=\sqrt{\frac{r x$result$P\times(1-r x$result$P)}{r x$result$n}}=r round(x$result$se,3)$$ $$z=\frac{p-P}{sd}=\frac{r round(x$result$p,3)-r x$result$P}{r round(x$result$se,3)}=r round(x$result$z,3)$$ where $P$ is the hypothesized value of population proportion in the null hypothesis, $p$ is the sample proportion, and $n$ is the sample size.

Since we have a r ifelse(two.sided,"two","one")-tailed test, the P-value is the probability that the z statistic is r if(!greater) "less than" r if(!greater) round(-abs(x$result$z),2) r if(!less) "or greater than " r if(!less) round(abs(x$result$z),2).

We can use following R code to find the p value.

if(two.sided){
               string=glue("pnorm(-abs({round(x$result$z,3)}))\\times2")
} else if(greater){
               string=glue("pnorm({round(x$result$z,3)},lower.tail=FALSE)")
} else{
               string=glue("pnorm({round(x$result$z,3)})")
          }

$$p=r string=r round(x$result$pvalue,3)$$

Alternatively,we can use the Normal Distribution curve to find p value.

draw_n(z=x$result$z,alternative=x$result$alternative)

4. Interpret results.

Since the P-value (r round(x$result$pvalue,3)) is r ifelse(x$result$pvalue>x$result$alpha,"greater","less") than the significance level (r x$result$alpha), we canr if(x$result$pvalue>x$result$alpha) "not" reject the null hypothesis.

Result of propCI()

print(x)

Reference

The contents of this document are modified from StatTrek.com. Berman H.B., "AP Statistics Tutorial", [online] Available at: https://stattrek.com/hypothesis-test/proportion.aspx?tutorial=AP URL[Accessed Data: 1/23/2022].



Try the interpretCI package in your browser

Any scripts or data that you put into this service are public.

interpretCI documentation built on Jan. 28, 2022, 9:07 a.m.