# Comparison of categorical variables with 3+ levels between two groups

Apologies if this is a simple question, but I can't seem to find an answer.

I'm hoping to compare two categorical variables, one with 2 levels and another with 6, summarised here:

              Male       Female
Group A     230          61
Group B     76           15
Group C     27           13
Group D     12           3
Group E     154          28
Group F     149          35


I believe it is possible to use Chi-square test to compare male vs female across all groups, however, this would not be useful as I am looking for differences within each group.

Would it be appropriate to compare using a 2x2 contingency table for each Group?

ie. To identify difference between Males and Females in Group A:

              Male       Female
Group A     230          61
Group B-F   418          94


• Basically, you need to decide what precisely you are trying to determine. For example, if you want to know if the proportions of male and female in each group are not 50/50, then you could conduct a binomial test on each group. ... You can't run a 2x2 chi-square for each group because there are only two counts per group. – Sal Mangiafico Nov 15 at 23:59

In essence I believe that it would be appropriate to do that, but the data would not be in the format that you are showing. It is also correct to not use a chi-squared test as that is to test for independence. Here we want to test to see if the treatment effects are different. At least, that is the assumption I am making when you say "different".

This is what I would do



Male       Female
Group A     230          61
Group B     76           15
Group C     27           13
Group D     12           3
Group E     154          28
Group F     149          35

df <- data.frame(sex = c("male","male", "male", "male", "male", "male", "female", "female", "female", "female", "female", "female"),
group = c("groupA", "groupB", "groupC", "groupD", "groupE", "groupF", "groupA", "groupB", "groupC", "groupD", "groupE", "groupF"),
value = c(230,76,27,12,154,149,61,15,13,3,28,35))

myaov <- aov(value ~ group, data = df)
summary(myaov)

Df Sum Sq Mean Sq F value Pr(>F)
group        5  27149    5430   1.061  0.463
Residuals    6  30716    5119

dfgroup <- df %>%
filter(group != "groupA")

dfgroup <- dfgroup %>%
group_by(sex) %>%
summarise(value = sum(value))

df <- df %>%
filter(group == "groupA") %>%
group_by(sex) %>%
summarise(value = sum(value))

dfgroup$$group <- "GroupB-F" df$$group <- "GroupA"

df <- rbind(dfgroup,df)

myaov <- aov(value ~ sex + group, data = df)
summary(myaov)

Df Sum Sq Mean Sq F value Pr(>F)
sex          1  60762   60762  10.117  0.194
group        1  12210   12210   2.033  0.389
Residuals    1   6006    6006

• Sorry having trouble replying to you! Thanks for your response, Hansel. I might not have been clear, but the first table I showed was the summary table of counts (rather than individual values) of the two variables. ie: table(df $group, df$ gender). Would ANOVA still be appropriate if this is the case? Thanks again – Scarves Nov 14 at 23:24
• Yes, ANOVA would still be appropriate, though then we run into the problem where the groups are not evenly distributed. I'm not sure how big a problem this is. The ANOVA test might be able to account for that, but I would sample your groups to have an equal number. – Hansel Palencia Nov 14 at 23:30
• If the data are counts --- that is, a contingency table --- then anova would probably not be appropriate. – Sal Mangiafico Nov 15 at 23:41