Dàvid Nagy
Dàvid Nagy

Reputation: 159

Hypothesis on a proportion in R

I have a random sample of 100 YES/No answers. 13 of them are "YES". The rest are "NO". I have to test a hypotheses on the proportion.

Hypothesis: H0: p = p0

H1: p > p0

Confidence level is 95%

I have the following code: (The z.prop function calculates the test statistic.)

z.prop = function(k, n, p, p0){ zeta = (p - p0) / (sqrt( p0*(1-p0)/n ) ) 
return(zeta) }

k<- 13
n<- 100
p<- k/n
p0<- 0.1

z <- z.prop(k,n,p,p0)
cat("z: ",z)
z.alpha <- qnorm(0.05,lower.tail=FALSE)
cat("z alpha: ",z.alpha)

pval<- pnorm(abs(z),lower.tail = FALSE)
cat("p-value",pval,"\n")

If I use this code then the p-values are different.

binom.test(k, n, p = p0,alternative ="greater",conf.level = 0.95)

Using my function I got 0.1586553 for the p-value. Using the binom.test function I got p-value = 0.1982.

How is this possible? Is my code wrong, or it is just some kind of rounding error? Thank you.

Upvotes: 2

Views: 298

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24252

Your z.prop function implements the same test of the prop.test function in stats (without the Yates continuity correction):

prTest <- prop.test(k, n, p=p0, alternative ="greater", correct = F)
prTest$p.value
# [1] 0.1586553

The binom.test function implements a different test for proportions: the exact binomial test.

Upvotes: 2

Related Questions