Reputation: 4801
After producing a table with three-variables as in:
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
the vcd
package allows the creation of a mosaic plot like this:
mosaic(salary, shade = T)
in which the surface area is proportional to the counts in the cell, and the color corresponds to the standardized Pearson's residuals.
The question is how to calculate and extract the expected counts?
I know that I can produce the margins like this:
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
and if it were a 2 x 2 contingency table I would use chisq.test(x)$expected
to see the expected values, but I don't know how to go about it in a 3-way contingency table.
Upvotes: 0
Views: 413
Reputation: 3694
There is a designated argument to compute (and visualize) expected values using the type = "expected"
option.
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
Upvotes: 2