2016-08-08 60 views
3

Warum sollte r ‚s prop.test Funktion (documentation here) unterschiedliche Ergebnisse basierend darauf, ob es ein I matrix oder Vektoren bestehen?R: prop.test unterschiedliche Werte zurückgibt, basierend auf ob Matrix oder Vektoren an sie übergeben werden

Hier gebe ich es Vektoren:

> 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 

Hier erstelle ich ein matrix und es in stattdessen passieren:

> 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 

Warum ich unterschiedliche Ergebnisse erhalten? Ich erwarte, dass die gleichen Ergebnisse für beide Szenarien zurückgegeben werden.

Antwort

2

Wenn x und n als separate Vektoren eingegeben werden, werden sie jeweils als die Anzahl der Erfolge und die Gesamtzahl der Versuche behandelt. Wenn Sie jedoch eine Matrix eingeben, wird die erste Spalte als die Anzahl der Erfolge und die zweite als Anzahl der Fehler behandelt. Von der Hilfe für 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. 

also das gleiche Ergebnis mit einer Matrix zu erhalten, müssen Sie die zweite Spalte der Matrix auf die Anzahl der Ausfälle konvertieren (unter der Annahme, dass in Ihrem Beispiel x ist die Anzahl der Erfolge und n ist die Anzahl der Versuche).

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 
+0

Vielen Dank für die Klarheit in diesem Fall. Der Teil "Anzahl der Fehler" anstelle von "Anzahl der Versuche" war etwas, das mein Gehirn vermisste. Ich habe erwartet, dass die Eingabe die Gesamtzahl der Versuche ist und nicht (1 - Erfolge). – Jarad