I've checked multiple threads about handling or visualizing contingency tables, but can't find one that can help my current question. I have a 2x3 contingency table: "group" variable has 3 levels not necessarily ordered, "level" variable has 2 levels (could be 3, but for simplicity, let's say 2). I would like to test independence between group and level variables, and visualize the pairwise relationship between groups as well (group 1 vs 3, 2 vs 3, etc). I'm using vcd::mosaic which runs loglm (which wraps loglin) to visualize individual significance of cells in the contingency table. I like it that the area of each tile reflects the cell frequency and color reflects the statistical significance (residual).

My question is: when I put the full dataset with all 3 groups in loglm, group 3 does not seem to deviate from the null, but when I use the partial datasets with group 1 and 3, or 2 and 3 only, then each pair significantly deviates from the null. What is the best way to perform the test, visualize and interpret the relationship between the 3 groups? Should I use two plots with the pair of groups or one plot with 3 groups? I provide a dummy example dataset and mosaic plot results below:

d <- data.frame(group = c(rep(1,20),rep(2,20),rep(3,20)), 
                level = c(1, rep(0,19),rep(1,19),0, rep(1,10),rep(0,10)))

# 3 groups
tbl123 <- table(d)
vcd::mosaic( ~ group + level, data = tbl123, gp=shading_max, split_vertical=T)  

# 2 group
tbl13 <- table(d[d$group %in% c(1,3), ])
vcd::mosaic( ~ group + level, data = tbl13, gp=shading_max, split_vertical=T)  

tbl23 <- table(d[d$group %in% c(2,3), ])
vcd::mosaic( ~ group + level, data = tbl23, gp=shading_max, split_vertical=T)

enter image description here enter image description here enter image description here



1 Answer 1


Let me try to answer my question after talking to a few friends. Please feedback/correct where appropriate. Whether to do the loglm test on the full dataset using 3 groups (in which group 3 shows no deviation from null) or on the partial dataset using 2 groups (in which group 3 shows deviation from null) depends on the original practical question at hand. If it makes sense (from the domain knowledge) to do two-group test for complete independence (i.e. group 1 & 3, 2 & 3 or 1 & 2, because the 3 groups arisen due to different causes), then the two-group test is fine. And so it can be visualized with 2x2 mosaic plots above. If the 3 groups are inter-related, e.g. the different eye colors due to the expression of the eye color genes, then should perform the test with the full dataset to get the full picture on the inter-relationship.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.