> 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
生成漂亮的表格,但我想知道是否有办法让它显示行总计和列总计。此外,它是否有可能显示某种频率,即 N/行总数和 N/列总数?
我已经尝试过包中的CrossTable
功能,gmodels
效果很好。但是,它似乎只适用于 2 个变量,而我想一次比较 2+ 个变量。
> 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