> data(infert, package = "datasets")
> tt = xtabs(~education + induced + spontaneous, data = infert)
> ftable(tt)
spontaneous 0 1 2
education induced
0-5yrs 0 2 1 1
1 1 0 1
2 6 0 0
6-11yrs 0 46 19 13
1 15 9 3
2 10 5 0
12+ yrs 0 19 27 15
1 29 7 3
2 13 3 0
xtabs
generates nice tables, but I'm wondering if there's a way to also have it show the row totals and column totals. Also, is it possible for it to show some sort of frequency i.e. N/row total and N/column total?
I have tried the CrossTable
function in the gmodels
package, and it works very nicely. However, it seems to only work for 2 variables, whereas I would like to compare 2+ variables at once.
> library(gmodels)
> CrossTable(infert$education, infert$induced, expected = TRUE)
Cell Contents
|-------------------------|
| N |
| Expected N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 248
| infert$induced
infert$education | 0 | 1 | 2 | Row Total |
-----------------|-----------|-----------|-----------|-----------|
0-5yrs | 4 | 2 | 6 | 12 |
| 6.919 | 3.290 | 1.790 | |
| 1.232 | 0.506 | 9.898 | |
| 0.333 | 0.167 | 0.500 | 0.048 |
| 0.028 | 0.029 | 0.162 | |
| 0.016 | 0.008 | 0.024 | |
-----------------|-----------|-----------|-----------|-----------|
6-11yrs | 78 | 27 | 15 | 120 |
| 69.194 | 32.903 | 17.903 | |
| 1.121 | 1.059 | 0.471 | |
| 0.650 | 0.225 | 0.125 | 0.484 |
| 0.545 | 0.397 | 0.405 | |
| 0.315 | 0.109 | 0.060 | |
-----------------|-----------|-----------|-----------|-----------|
12+ yrs | 61 | 39 | 16 | 116 |
| 66.887 | 31.806 | 17.306 | |
| 0.518 | 1.627 | 0.099 | |
| 0.526 | 0.336 | 0.138 | 0.468 |
| 0.427 | 0.574 | 0.432 | |
| 0.246 | 0.157 | 0.065 | |
-----------------|-----------|-----------|-----------|-----------|
Column Total | 143 | 68 | 37 | 248 |
| 0.577 | 0.274 | 0.149 | |
-----------------|-----------|-----------|-----------|-----------|
Statistics for All Table Factors
Pearson's Chi-squared test
------------------------------------------------------------
Chi^2 = 16.53059 d.f. = 4 p = 0.002383898
You can generate frequency tables with prop.table
and add margins with addmargins
data(infert, package='datasets')
prop.table(addmargins(xtabs(~education + induced + spontaneous, data=infert)))