# Controlling for a third variable in chi-squared analysis

I have $n$ individuals belonging to five different groups. I would like to know if those groups have a significant impact on a certain factor $Y$ (having three possible levels, A, B and C). So, I am basically in the case of a chi-squared analysis with "Group" and $Y$ as inputs.

The problem is the following. I know that the sex of an individual also impacts $Y$, and the sex-ratio in each group is not balanced: some groups have more men and other groups have more women. Thus, I would like to control for the factor Sex in this analysis.

I'm not interested in testing the independence of all factors ("are there more women in Group1 than in Group2?" is clearly not interesting for me), but really in testing the independance of Group and $Y$ when controlling for Sex.

Which kind of model or analysis should I use?

• You have a 2 x 5 x 3 table with 2 x (5-1) x (3-1) degrees of freedom. Oct 23, 2017 at 8:12
• Indeed, but what am I supposed to do with this table? (Especially if all my variables do not play the same role, and one of them is not strictly a variable of interest but must control the link between the two other variables?) Oct 23, 2017 at 8:36
• If you would not have include this second variable, then you would have had a typic 5 X 3 table with (5-1) x (3-1) degrees of freedom. The effect of adding this new variable, adding another level to the table, is a reduction in the bias (due to possible correlation between groups and sex), making it actually more difficult to find an effect, but more accurate if you do. So you have a model P(level) = significant function of 'sex' and 'group'. With chi-squere test you assume the opposite: P(level)$\neq$ significant function of 'sex' and 'group', and test whether your observations match this. Oct 23, 2017 at 9:07
• The difference in roles occurs in the place where you let the sex variable not reduce the degrees of freedom. Your model is $P(level=i \vert sex, group) = P(level=i, \vert group)$. Thus you predict your cells in group,level,sex by: $N_{g,l,s} = N_s * P(g) * P(l)$ instead of $N_{g,l,s} = N * P(s) * P(g) * P(l)$ Oct 23, 2017 at 9:10
• I mean $$N_{g,l,s}= N_s * P(g) * P(l \vert g)$$ instead of $$N_{g,l,s}= N * P(s) * P(g \vert s) * P(l \vert s,g)$$ You have got 2x5x3 observed $N_{g,l,s}$ and fit 2 $N_s$, 2x4 independent $P(g \vert s)$ and 2x3 independent $P(l \vert s,g)$. Oct 23, 2017 at 9:27

Your case, as you have described it, is exactly the situation the Cochran-Mantel-Haenszel test is designed for. Many people are familiar with it only in the case where there are many $2\times 2$ tables, but there are generalizations that apply to tables with more than two rows and more than two columns. Moreover, you can have just two strata. Thus, in your case you have $3$ rows and $5$ columns that you want to test for independence (like a traditional chi-squared test), but controlling for sex (which amounts to $2$ strata).

One thing to note is that the CMH test assumes the effect of group on Y is the same in all levels of sex, i.e., there is no group x sex interaction. If you believed there was, or wanted to test for such an interaction, you would need a different test. However, you don't seem to be interested in that, and this is the simplest test to give you what you want since you are operating under that assumption.

Most statistical software should be able to do this for you. Here is a quick example, coded in R:

dat = as.table(array(1:30, dim=c(3, 5, 2),
dimnames=list(    Y=c("A", "B", "C"),
Group=c("g1", "g2", "g3", "g4", "g5"),
Sex=c("Male", "Female"))))
dat
# , , Sex = Male
#
#    Group
# Y   g1 g2 g3 g4 g5
#   A  1  4  7 10 13
#   B  2  5  8 11 14
#   C  3  6  9 12 15
#
# , , Sex = Female
#
#    Group
# Y   g1 g2 g3 g4 g5
#   A 16 19 22 25 28
#   B 17 20 23 26 29
#   C 18 21 24 27 30
mantelhaen.test(dat)
#   Cochran-Mantel-Haenszel test
#
# data:  dat
# Cochran-Mantel-Haenszel M^2 = 0.19448, df = 8, p-value = 1

• Thank you very much, I didn't know this test. Very interesting! Oct 24, 2017 at 11:53

I suggest using multinomial logistic regression with Y as the dependent variable and sex and group as independent variables.