2

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.

Dàvid Nagy
  • 159
  • 3
  • 10

1 Answers1

2

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.

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • Thank you for your answer. When should I use binom.test and prop.test? Is it possible to use the prop.test in my case (100 binary element)? If yes then with or without continuity correction? – Dàvid Nagy Mar 17 '18 at 21:52
  • @DàvidNagy You can use prop.test in your case. By default prop.test uses continuity correction. I suggest to use this default and (very important) to read something about the theory behind the test ! – Marco Sandri Mar 17 '18 at 22:06