简体   繁体   English

R:prop.test根据矩阵或向量是否传递给它返回不同的值

[英]R: prop.test returns different values based on whether matrix or vectors are passed to it

Why would r 's prop.test function ( documentation here ) return different results based on whether I pass it a matrix or vectors? 为什么rprop.test函数( 此处的文档 )会根据我是否将matrix或向量传递给它而返回不同的结果?

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: 在这里,我创建一个matrix并将其传递给:

> 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. 我希望返回两种方案的结果相同。

When x and n are entered as separate vectors, they are treated, respectively, as the number of successes and the total number of trials. xn作为单独的向量输入时,它们分别被处理为成功次数和试验总次数。 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 : prop.test的帮助:

 xa 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是成功次数, n是试验次数)。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM