Problema
En R deseamos crear una tabla de referencias cruzadas similar a la producida en SAS (SAS Proc Freq) o crosstabs SPSS.
Solución
Utilizamos la función CrossTable del paquete gmodels.
CrossTable(mtcars$am, mtcars$gear, dnn = c("am", "gear"), expected = TRUE, format = "SAS")
Cell Contents
|-------------------------|
| N |
| Expected N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 32
| gear
am | 3 | 4 | 5 | Row Total |
-------------|-----------|-----------|-----------|-----------|
0 | 15 | 4 | 0 | 19 |
| 8.906 | 7.125 | 2.969 | |
| 4.169 | 1.371 | 2.969 | |
| 0.789 | 0.211 | 0.000 | 0.594 |
| 1.000 | 0.333 | 0.000 | |
| 0.469 | 0.125 | 0.000 | |
-------------|-----------|-----------|-----------|-----------|
1 | 0 | 8 | 5 | 13 |
| 6.094 | 4.875 | 2.031 | |
| 6.094 | 2.003 | 4.339 | |
| 0.000 | 0.615 | 0.385 | 0.406 |
| 0.000 | 0.667 | 1.000 | |
| 0.000 | 0.250 | 0.156 | |
-------------|-----------|-----------|-----------|-----------|
Column Total | 15 | 12 | 5 | 32 |
| 0.469 | 0.375 | 0.156 | |
-------------|-----------|-----------|-----------|-----------|
Statistics for All Table Factors
Pearson's Chi-squared test
------------------------------------------------------------
Chi^2 = 20.94467 d.f. = 2 p = 2.830889e-05
CrossTable(mtcars$am, mtcars$gear, dnn = c("am", "gear"), expected = TRUE, format = "SPSS")
Cell Contents
|-------------------------|
| Count |
| Expected Values |
| Chi-square contribution |
| Row Percent |
| Column Percent |
| Total Percent |
|-------------------------|
Total Observations in Table: 32
| gear
am | 3 | 4 | 5 | Row Total |
-------------|-----------|-----------|-----------|-----------|
0 | 15 | 4 | 0 | 19 |
| 8.906 | 7.125 | 2.969 | |
| 4.169 | 1.371 | 2.969 | |
| 78.947% | 21.053% | 0.000% | 59.375% |
| 100.000% | 33.333% | 0.000% | |
| 46.875% | 12.500% | 0.000% | |
-------------|-----------|-----------|-----------|-----------|
1 | 0 | 8 | 5 | 13 |
| 6.094 | 4.875 | 2.031 | |
| 6.094 | 2.003 | 4.339 | |
| 0.000% | 61.538% | 38.462% | 40.625% |
| 0.000% | 66.667% | 100.000% | |
| 0.000% | 25.000% | 15.625% | |
-------------|-----------|-----------|-----------|-----------|
Column Total | 15 | 12 | 5 | 32 |
| 46.875% | 37.500% | 15.625% | |
-------------|-----------|-----------|-----------|-----------|
Statistics for All Table Factors
Pearson's Chi-squared test
------------------------------------------------------------
Chi^2 = 20.94467 d.f. = 2 p = 2.830889e-05
Minimum expected frequency: 2.03125
Cells with Expected Frequency < 5: 3 of 6 (50%)
Notas
Argumentos:
expected - si es TRUE para incluir la prueba chi-cuadrado de Pearson.
format - indicamos el formato de la salida de datos, SAS —por defecto— o SPSS.
dnn - nombres de las dimensiones en el resultado.
Referencias
No hay comentarios:
Publicar un comentario