# Testing for contingency table with three variables

How does one make conclusions using contingency table with three variables? In the two variable case, you can test for association through the independence test using pearsons chisq statistic, but what happens with 3 variables? For example(hypothetical data set): I have gender(male/female), smoking(yes/no) and cancer(yes/no) data for a population. How does one find out if smoking has a bigger impact on cancer occurrence for female than for male?

In no way contradicting the answer by Ian_Fin, you may want to take a look at three-way contingency tables.

Looking for a made-up example in R, it could look like this:

, , nicotine_habit = smoker

gender
disease   male female
cancer    15     20
healthy  110     32

, , nicotine_habit = nonsmoker

gender
disease   male female
cancer    18      6
healthy   15     97


Notice the set-up: in the entry $\small[1,2]$ of the first conditional table (smokers), the cancer rate for females goes through the roof ($\small 20$), yet if we generate the table of marginal distributions:

        disease
gender   cancer healthy
male       33     125
female     26     129


we see that there are more cancers in males than females. So if the data are manicured enough we can already anticipate a Simpson's paradox: the odds for cancer among smokers will be higher for females than for males, but this effect will dissipate when pooling all the data together. In a journal, we could scroll down to the "Conclusions" section and see something along the lines: "although males seem more predisposed to lung cancer, the effect of tobacco appears to have a selectively negative effect in females." Let's see...

1. Marginal odds ratio:

The odds ratios for gender (male) and disease (cancer) independent of nicotine addiction is $1.3$, reflecting the higher counts of cancer among men. However, it is not significant, as evidenced by the 95% confidence interval:

                              2.5 %   97.5 %
male:female/cancer:healthy 0.740903 2.315684


which includes the value $1$ (the null hypothesis is understood to be $\small \text{odds ratio}=1$). Further a chi square test of independence yields X-squared = 0.61691, df = 1, p-value = 0.4322 - hence, there is no significant difference in the odds of cancer with respect to gender, even though we found more cancers among men.

2. Conditional odds ratio:

When we take into consideration the presence or absence of a smoking habit, the effect of this confounding variable tells a whole different story. The odds ratio of cancer by gender (male) in smokers is $0.2$- much lower in males than females as we had anticipated, while the odds ratio in non-smokers is a whopping $19.4$! If we control for the marked sensitivity to tobacco in females, the odds of having cancer are much higher in males, and only balanced in the marginal analysis by the $20$ cancers among female smokers.

This is statistically significant, of course:

                                         2.5 %     97.5 %
cancer:healthy/male:female|smoker    0.1003545  0.4743514
cancer:healthy/male:female|nonsmoker 6.6405326 56.6761767


code here

This approach wouldn't use a contingency table, so it may not be the sort of answer you're looking for, but for a research question such as you describe, "Is the effect of smoking on cancer occurrence different for males and females?", logistic regression would be one way of finding an answer.

With the logistic regression you'd have an outcome, whether or not the person had cancer, and two predictors, their gender and whether they smoked. By testing for an interaction between the two predictors, you could see whether the difference between non-smokers and smokers in the probability, or more accurately the log-odds, of getting cancer varied between the genders.

With more complex contingency tables logistic regression would still be a possibility, although it may get a little more complex as well.