# Test whether difference in proportions differs from a non-zero constant

I am using the prop.test function in R to test the differences of proportions against the alternative that the difference of the two proportions are significantly different from zero, however I wish to perform the same test, but instead of testing if they are significantly different from zero I want to test if they are different from some constant (lets say C) where more often then not C is not equal to 0.

Does anyone know of a function in R to do this?

• Welcome to our site! "Does anyone know an R function that does X" is not really on-topic here - see our help center - as it is really a programming question. However I wonder if you have an underlying statistical issue that would be on-topic here. I think your question would benefit from some clarification on what you're interested so we can determine whether your needs are really on-topic here. Also you should clarify - when you say "testing if they are significant different from zero", you mean "testing if the difference in proportions is significantly different from zero"? Aug 23, 2016 at 20:50
• Very simple - is there a way to test if the difference of proportions is significantly different from a constant that is not zero that is in R? if not then I need to code it. Aug 23, 2016 at 20:53
• This is the correct site for the statistical question but it's not the site for questions specifically about R code. That's why @Silverfish requested a clarification.
– whuber
Aug 23, 2016 at 21:16
• you could fit a generalized linear model with a binomial response and an offset equal to your null value ... Aug 24, 2016 at 1:04

The standard formula for testing equality of 2 proportions (using the normal approximation) uses a pooled estimate of the proportion that is appropriate when the null of equal proportions is true. In your case the proportions are not equal, so the pooled proportion is not appropriate.

One option is that you can code the formula that does not pool the proportion, then compute the p-value, etc. from the normal approximation.

Another option is to just use prop.test but ignore the p-value part and look to see if the confidence interval includes the C value that you are interested in. If C is not in the interval then that is equivalent to rejecting the null and if C is in the interval then that is equivalent of a p-value greater than alpha (not enough evidence to reject). You don't get an exact p-value, but you get the same decision.

• Your last suggestion was to be my last resort. I was hoping there was a function that could do this otherwise my next option was as you suggested to code a function that does the job. Thank you for the suggestions. Aug 23, 2016 at 20:49

A test for difference between proportions is as follows:

$H_0: p_2 - p_1 = C$

$H_A: p_2 - p_1 \neq C$

Then the test statistic is:

$$Z = \frac{(\hat{p}_2 - \hat{p}_1) - C}{\sqrt{\frac{\hat{p}_1(1-\hat{p}_1)}{n_1} + \frac{\hat{p}_2(1-\hat{p}_2)}{n_2}}}$$

The following R code ought to work:

SE = sqrt((p1*(1-p1)/n1 + p2*(1-p2)/n2))
z = ((p1 - p2) - C)/SE
1 - pnorm(abs(z))

Note: For this test to be appropriate, each sample much be independent and random, and must have at least 10 successes and 10 failures. It's also problematic when $p_i$ or $1-p_i$ is close to zero. Thanks to @whuber for pointing out these requirements.

• This test is appropriate only under some unstated assumptions, including that the $n_i p_i$ both be sufficiently large. What justifies using a pooled estimate when the null is that the proportions differ? It would be of interest to know what all those assumptions are and how this test performs relative to those that do not require them.
– whuber
Aug 23, 2016 at 21:18
• Do you have a source to refer to or some other way to demonstrate that your procedure is adequate?
– whuber
Aug 23, 2016 at 21:21
• Comparing Two Proportions Aug 23, 2016 at 21:30
• That is for the case $C=0$. It doesn't appear to work well for nonzero $C$--which is why this question is being asked.
– whuber
Aug 23, 2016 at 21:33
• I'm fairly certain it's as good of an approximation for $C\neq 0$ as it is for $C=0$; will post justification soon Aug 23, 2016 at 21:36