簡體   English   中英

R:prop.test根據矩陣或向量是否傳遞給它返回不同的值

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

為什么rprop.test函數( 此處的文檔 )會根據我是否將matrix或向量傳遞給它而返回不同的結果?

在這里我傳遞它向量:

> 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 

為什么我會得到不同的結果? 我希望返回兩種方案的結果相同。

xn作為單獨的向量輸入時,它們分別被處理為成功次數和試驗總次數。 但是當您輸入矩陣時,第一列被視為成功次數,第二列被視為失敗次數。 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. 

因此,要使用矩陣得到相同的結果,您需要將矩陣的第二列轉換為失敗次數(假設在您的示例中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