生成一个包含三个变量的表,如:
salary <- array(
c(38, 12, 102, 141, 12, 9, 136, 383),
dim=c(2, 2, 2),
dimnames=list(exposure = c("exposed", "not"),
disease = c("case", "control"),
salary = c("<1000", ">=1000"))
)
salary
, , salary = <1000
disease
exposure case control
exposed 38 102
not 12 141
, , salary = >=1000
disease
exposure case control
exposed 12 136
not 9 383
vcd
包允许创建如下的马赛克图:
mosaic(salary, shade = T)
其中表面积与单元格中的计数成比例,颜色对应于标准化的Pearson残差。
问题是如何计算和提取预期的计数?
我知道我可以产生这样的边距:
addmargins(salary)
, , salary = <1000
disease
exposure case control Sum
exposed 38 102 140
not 12 141 153
Sum 50 243 293
, , salary = >=1000
disease
exposure case control Sum
exposed 12 136 148
not 9 383 392
Sum 21 519 540
, , salary = Sum
disease
exposure case control Sum
exposed 50 238 288
not 21 524 545
Sum 71 762 833
如果它是一个2 x 2列联表,我会使用chisq.test(x)$expected
来查看预期值,但我不知道如何在三向列联表中进行处理。
答案 0 :(得分:2)
使用type = "expected"
选项计算(和可视化)预期值有一个指定的参数。
mos_exp <- mosaic(salary, type = "expected")
mos_exp
#> disease case control
#> exposure salary
#> exposed <1000 8.634326 92.666994
#> >=1000 15.913093 170.785587
#> not <1000 16.339263 175.359416
#> >=1000 30.113318 323.188003