3

Why would r's prop.test function (documentation here) return different results based on whether I pass it a matrix or vectors?

Here I pass it vectors:

> prop.test(x = c(135, 47), n = c(1781, 1443))

    2-sample test for equality of proportions with
    continuity correction

data:  c(135, 47) out of c(1781, 1443)
X-squared = 27.161, df = 1, p-value = 1.872e-07
alternative hypothesis: two.sided
95 percent confidence interval:
 0.02727260 0.05918556
sample estimates:
    prop 1     prop 2 
0.07580011 0.03257103 

Here I create a matrix and pass it in instead:

> table <- matrix(c(135, 47, 1781, 1443), ncol=2)
> prop.test(table)

    2-sample test for equality of proportions with
    continuity correction

data:  table
X-squared = 24.333, df = 1, p-value = 8.105e-07
alternative hypothesis: two.sided
95 percent confidence interval:
 0.02382527 0.05400606
sample estimates:
    prop 1     prop 2 
0.07045929 0.03154362 

Why do I get different results? I expect the same results for both scenarios to be returned.

Jarad
  • 17,409
  • 19
  • 95
  • 154

1 Answers1

3

When x and n are entered as separate vectors, they are treated, respectively, as the number of successes and the total number of trials. But when you enter a matrix, the first column is treated as the number of successes and the second as the number of failures. From the help for prop.test:

x    a vector of counts of successes, a one-dimensional table with two
     entries, or a two-dimensional table (or matrix) with 2 columns, giving
     the counts of successes and failures, respectively.

So, to get the same result with a matrix, you need to convert the second column of the matrix to the number of failures (assuming that in your example x is the number of successes and n is the number of trials).

x = c(135, 47)
n = c(1781, 1443)

prop.test(x, n)  # x = successes; n = total trials
  2-sample test for equality of proportions with continuity correction

data:  x out of n
X-squared = 27.161, df = 1, p-value = 1.872e-07
alternative hypothesis: two.sided
95 percent confidence interval:
 0.02727260 0.05918556
sample estimates:
    prop 1     prop 2 
0.07580011 0.03257103
prop.test(cbind(x, n - x)) # x = successes; convert n to number of failures
  2-sample test for equality of proportions with continuity correction

data:  cbind(x, n - x)
X-squared = 27.161, df = 1, p-value = 1.872e-07
alternative hypothesis: two.sided
95 percent confidence interval:
 0.02727260 0.05918556
sample estimates:
    prop 1     prop 2 
0.07580011 0.03257103
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thanks for the clarity on this one. The "number of failures" part instead of "number of trials" was something my brain missed. I was expecting the input to be total number of trials and not (1 - successes). – Jarad Aug 09 '16 at 03:37