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?
 A: 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

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