R:prop.test returns 不同的值基于是否传递给它的矩阵或向量
R: prop.test returns different values based on whether matrix or vectors are passed to it
为什么 r 的 prop.test
函数 (documentation here) return 根据我是否传递 [=15] 不同的结果=] 还是矢量?
我在这里传递向量:
> 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
这里我创建了一个 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
为什么我得到不同的结果?我希望 return 编辑这两种情况的相同结果。
当x
和n
作为单独的向量输入时,它们分别被视为成功次数和试验总数。但是当您输入矩阵时,第一列被视为成功次数,第二列被视为失败次数。来自 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.
因此,要使用矩阵获得相同的结果,您需要将矩阵的第二列转换为失败次数(假设在您的示例中 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
为什么 r 的 prop.test
函数 (documentation here) return 根据我是否传递 [=15] 不同的结果=] 还是矢量?
我在这里传递向量:
> 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
这里我创建了一个 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
为什么我得到不同的结果?我希望 return 编辑这两种情况的相同结果。
当x
和n
作为单独的向量输入时,它们分别被视为成功次数和试验总数。但是当您输入矩阵时,第一列被视为成功次数,第二列被视为失败次数。来自 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.
因此,要使用矩阵获得相同的结果,您需要将矩阵的第二列转换为失败次数(假设在您的示例中 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