Anuncio
```Test chi-cuadrado y del cociente de verosimilitudes
5 de junio de 2008
Vamos a aplicar los tests X 2 y G2 .
&gt;
+
&gt;
+
+
&gt;
&gt;
religion.counts &lt;- c(178, 138, 108, 570, 648, 442, 138, 252,
252)
table.3.2 = cbind(expand.grid(list(Religious.Beliefs = c(&quot;Fund&quot;,
&quot;Mod&quot;, &quot;Lib&quot;), Highest.Degree = c(&quot;&lt;HS&quot;, &quot;HS or JH&quot;, &quot;Bachelor or Grad&quot;))),
count = religion.counts)
table.3.2.array = tapply(table.3.2\$count, table.3.2[, 1:2], sum)
(res = chisq.test(table.3.2.array))
Pearson's Chi-squared test
data: table.3.2.array
X-squared = 69.1568, df = 4, p-value = 3.42e-14
Los conteos esperados los obtenemos con
&gt; res\$expected
Highest.Degree
Religious.Beliefs
&lt;HS HS or JH Bachelor or Grad
Fund 137.8078 539.5304
208.6618
Mod 161.4497 632.0910
244.4593
Lib 124.7425 488.3786
188.8789
Los tests y frecuencias esperadas pueden obtenerse con el paquete vcd.
&gt; library(vcd)
&gt; assocstats(table.3.2.array)
X^2 df
P(&gt; X^2)
Likelihood Ratio 69.812 4 2.4869e-14
Pearson
69.157 4 3.4195e-14
Phi-Coefficient
: 0.159
Contingency Coeff.: 0.157
Cramer's V
: 0.113
La función chisq.test lleva test de Montecarlo.
&gt; chisq.test(table.3.2.array, sim = T, B = 2000)
Pearson's Chi-squared test with simulated p-value (based on 2000
replicates)
data: table.3.2.array
X-squared = 69.1568, df = NA, p-value = 0.0004998
1
El test del cociente de verosimilitud G2 se obtiene como
&gt; fit.glm = glm(count ~ Religious.Beliefs + Highest.Degree, data = table.3.2,
+
family = poisson)
&gt; fit.glm\$deviance
[1] 69.81162
&gt; temp &lt;- predict(fit.glm, type = &quot;response&quot;)
&gt; matrix(temp, nc = 3, byrow = T, dimnames = list(c(&quot;&lt;HS&quot;, &quot;HS or JH&quot;,
+