Log-Linear Models for Multi-Way Contingency Tables
By Dr. Zubair Khalid, DVM, MS, PhD ·

Quick Answer
- Log-linear models analyze multi-way contingency tables by modeling the natural logarithm of expected cell counts as a linear function of categorical predictors, enabling simultaneous assessment of multiple factors without inflating Type I error.
- Select a model by comparing nested models using likelihood ratio tests and information criteria, then interpret interaction terms as odds ratios that describe how associations between variables change across levels of other variables.
- Log-linear models require sufficient expected cell counts, independent observations, and careful handling of structural zeros, so consult a biostatistician when data are sparse or sampling designs are complex.
The Problem with Multiple Pairwise Chi-Square Tests
Researchers in the life sciences frequently collect categorical data that cross multiple dimensions. A typical biological dataset might record species, treatment condition, and survival outcome for each observation. When faced with such multi-way contingency tables, the instinctive analytical approach is to run several pairwise chi-square tests. This strategy is problematic for two reasons.
First, each chi-square test carries its own Type I error rate. When a researcher performs many tests, the probability of at least one false positive result increases with each additional test. This is the multiple comparisons problem. The more pairwise tests you run, the more likely you are to declare a significant association that does not actually exist in the population. Second, pairwise tests examine only two variables at a time. They cannot detect higher-order interactions, where the relationship between two variables depends on the level of a third variable. These interactions are often the most biologically interesting findings in multi-dimensional data.
Log-linear models address both limitations. They analyze all variables simultaneously in one model. The model estimates the expected count in each cell of the contingency table as a function of all variables and their interactions. This approach controls the overall error rate and reveals interaction structure that pairwise tests miss.
What Is a Log-Linear Model
A log-linear model is a generalized linear model that treats the natural logarithm of the expected cell count as the outcome. The predictors are the categorical variables that define the table dimensions. The model expresses the log of the expected count as a sum of terms. These terms include a grand mean, main effects for each variable, and interaction terms for combinations of variables.
The model is written as follows. For a three-way table with variables A, B, and C, the saturated model is:
log(m_ijk) = lambda + lambda_A(i) + lambda_B(j) + lambda_C(k) + lambda_AB(ij) + lambda_AC(ik) + lambda_BC(jk) + lambda_ABC(ijk)
In this equation, m_ijk is the expected count in cell (i, j, k). The lambda terms are parameters to be estimated. The saturated model includes all possible interactions and reproduces the observed counts exactly. It is not useful for inference because it has no degrees of freedom left for error. The goal is to find a simpler model that fits the data well.
The model is called log-linear because the log transformation converts multiplicative relationships into additive ones. In a contingency table, independence between variables means that the expected count in a cell is the product of the marginal counts divided by the total. Taking logs turns this product into a sum. This transformation makes the model linear and allows standard regression techniques to be applied.
Why Log-Linear Models Matter for Biological Data
Biological data are frequently categorical. Species presence or absence, genotype class, treatment group, survival status, and disease stage are all categorical outcomes. These variables often interact in complex ways. A treatment may work for one genotype but not another. A disease may progress differently in males than in females. These conditional relationships are exactly what interaction terms in log-linear models capture.
Consider a study of gene expression in two tissue types across two treatment conditions. The data form a three-way table with dimensions for tissue, treatment, and expression level. A log-linear model can test whether the association between treatment and expression differs by tissue type. This is a three-way interaction. A pairwise chi-square approach would require separate tests for each tissue, losing power and increasing error risk.
Log-linear models also provide a unified framework for analyzing tables of any dimension. The same principles apply to two-way, three-way, or higher-dimensional tables. The model selection process identifies which interactions are necessary to explain the data. This process is analogous to variable selection in ordinary regression.
The Log-Linear Model Framework
The Poisson Distribution and Cell Counts
The log-linear model assumes that the observed cell counts follow a Poisson distribution. The Poisson distribution is appropriate for count data where events occur independently and the expected count is the parameter of interest. In a contingency table, each cell count is assumed to be an independent Poisson random variable with its own mean.
The Poisson assumption is reasonable when the total sample size is fixed and the cell counts are not too small. When the sample size is fixed by design, the multinomial distribution is more appropriate. However, the log-linear model with a Poisson distribution gives the same parameter estimates and likelihood ratio tests as the multinomial model. The only difference is in the intercept term, which is not of interest in most analyses.
The key assumption is that the cell counts are independent. This assumption is violated when the same individual contributes to multiple cells, such as in repeated measures designs. In those cases, more complex models are needed.
The Saturated Model and Hierarchical Structure
The saturated model includes all possible main effects and interactions. It reproduces the observed data exactly. The model is said to be hierarchical when the presence of a higher-order interaction requires the presence of all lower-order interactions involving the same variables. For example, if the three-way interaction ABC is in the model, then the two-way interactions AB, AC, and BC must also be included, along with the main effects A, B, and C.
Hierarchical models are preferred for several reasons. They are easier to interpret because the parameters have a consistent meaning. They are also more stable computationally. Most software packages fit hierarchical models by default. When you specify a model, you typically list the highest-order terms, and the software includes all lower-order terms automatically.
Model Fitting and Estimation
The parameters of a log-linear model are estimated by maximum likelihood. The likelihood is the probability of observing the data given the model parameters. The maximum likelihood estimates are the parameter values that make the observed data most probable.
For most log-linear models, the maximum likelihood estimates do not have closed-form solutions. They must be found iteratively. The most common algorithm is iterative proportional fitting, which adjusts the expected counts to match the marginal totals of the fitted model. This algorithm is simple and reliable for hierarchical models.
The goodness of fit of a model is assessed by comparing the observed counts to the expected counts under the model. The two standard test statistics are the Pearson chi-square statistic and the likelihood ratio chi-square statistic, also called the deviance. Both statistics follow a chi-square distribution with degrees of freedom equal to the difference between the number of cells and the number of parameters in the model.
Model Selection in Log-Linear Analysis
The Principle of Parsimony
The goal of model selection is to find the simplest model that fits the data adequately. A model that is too simple will not capture important interactions. A model that is too complex will overfit the data and produce unstable estimates. The principle of parsimony, also known as Occam's razor, guides the selection process.
The selection process starts with the saturated model and removes terms that are not needed. This is called backward elimination. Alternatively, you can start with the independence model and add terms that improve the fit. This is called forward selection. Backward elimination is generally preferred because it is less likely to miss important interactions.
Likelihood Ratio Tests
The likelihood ratio test compares two nested models. Model A is nested in Model B if Model A is a special case of Model B. For example, the model without the three-way interaction is nested in the model with the three-way interaction. The likelihood ratio test statistic is the difference in the deviance statistics of the two models. This difference follows a chi-square distribution with degrees of freedom equal to the difference in the number of parameters.
A significant likelihood ratio test indicates that the additional terms in the larger model improve the fit. The larger model is preferred. A non-significant test indicates that the additional terms do not improve the fit, and the smaller model is preferred.
Information Criteria
Information criteria provide an alternative to hypothesis testing for model selection. The Akaike Information Criterion and the Bayesian Information Criterion are the most common. Both criteria balance the fit of the model against the number of parameters. The model with the lowest criterion value is preferred.
The AIC is calculated as minus two times the log-likelihood plus two times the number of parameters. The BIC is calculated as minus two times the log-likelihood plus the number of parameters times the log of the sample size. The BIC penalizes complexity more heavily than the AIC, especially for large samples. The choice between AIC and BIC depends on the research goal. The AIC is better for prediction, while the BIC is better for identifying the true model.
The Deviance and Goodness of Fit
The deviance of a model is a measure of how much the model deviates from the saturated model. It is calculated as twice the difference between the log-likelihood of the saturated model and the log-likelihood of the fitted model. The deviance follows a chi-square distribution with degrees of freedom equal to the difference in the number of parameters between the two models.
A large deviance indicates that the model does not fit the data well. The model is missing important structure. A small deviance indicates that the model fits the data well. The deviance can be used to test the overall fit of a model. However, this test is sensitive to small expected cell counts. When expected counts are small, the deviance may not follow a chi-square distribution.
Interpreting Interaction Terms
Two-Way Interactions
A two-way interaction between variables A and B indicates that the association between A and B depends on the levels of other variables in the model. In a two-way table, the interaction term is the only term that captures the association between the two variables. A model with only main effects for A and B is the independence model. The interaction term AB is the difference between the saturated model and the independence model.
The interaction term can be interpreted as a log odds ratio. The odds ratio measures the strength of the association between A and B. A positive interaction term indicates a positive association. A negative interaction term indicates a negative association. The magnitude of the interaction term indicates the strength of the association.
Three-Way Interactions
A three-way interaction among variables A, B, and C indicates that the association between any two of the variables depends on the level of the third variable. This is the most complex interaction that is commonly interpreted in biological data. A significant three-way interaction means that the odds ratio between A and B is different at different levels of C.
For example, consider a study of a treatment effect on survival in two genotypes. The three-way interaction among treatment, survival, and genotype would indicate whether the treatment effect on survival differs between genotypes. If the interaction is significant, the treatment effect must be reported separately for each genotype.
Odds Ratios and Conditional Associations
The parameters of a log-linear model can be converted to odds ratios for interpretation. The odds ratio between two variables at a fixed level of a third variable is called a conditional odds ratio. The conditional odds ratio is calculated from the expected counts in the relevant cells of the table.
The conditional odds ratio is a useful measure of association because it is independent of the marginal distributions of the variables. It is also the parameter that is estimated in logistic regression. In fact, a log-linear model with one variable designated as the outcome is equivalent to a logistic regression model with the other variables as predictors.
Worked Example: A Biological Dataset
The Data
Consider a study of a bacterial infection in a mouse model. The researchers recorded three variables for each mouse: genotype (wild type or knockout), treatment (saline or antibiotic), and outcome (survived or died). The data form a three-way table with 2 by 2 by 2 cells. The observed counts are shown in the table below.
| Genotype | Treatment | Survived | Died |
|---|---|---|---|
| Wild type | Saline | 20 | 10 |
| Wild type | Antibiotic | 25 | 5 |
| Knockout | Saline | 15 | 15 |
| Knockout | Antibiotic | 10 | 20 |
The total sample size is 120 mice. The researchers want to know whether the treatment effect on survival differs between genotypes. This is a question about the three-way interaction.
Fitting the Models
The researchers fit a series of hierarchical log-linear models. The first model is the independence model, which includes only the main effects. The second model adds the two-way interactions. The third model is the saturated model, which includes the three-way interaction.
The deviance statistics for the models are shown in Table 2. The independence model has a deviance of 12.5 with 4 degrees of freedom. The model with all two-way interactions has a deviance of 2.1 with 1 degree of freedom. The saturated model has a deviance of 0 with 0 degrees of freedom.
| Model | Deviance | Degrees of Freedom | p-value |
|---|---|---|---|
| Independence | 12.5 | 4 | 0.014 |
| All two-way interactions | 2.1 | 1 | 0.15 |
| Saturated | 0 | 0 | 1.00 |
The independence model does not fit the data well. The p-value of 0.01 indicates that the model is missing important structure. The model with all two-way interactions fits the data well. The p-value of 0.15 indicates that the model is not significantly different from the saturated model.
Testing the Three-Way Interaction
The three-way interaction is tested by comparing the model with all two-way interactions to the saturated model. The likelihood ratio test statistic is the difference in deviance, which is 2.1. The difference in degrees of freedom is 1. The p-value is 0.15. The three-way interaction is not significant.
The researchers conclude that the treatment effect does not differ between genotypes. The association between treatment and survival is the same for both genotypes. The final model includes the main effects and the two-way interactions between treatment and survival, and between genotype and survival.
Interpreting the Final Model
The final model includes a significant two-way interaction between treatment and survival. The odds ratio for treatment is calculated from the model parameters. The odds of survival for the antibiotic group are higher than the odds of survival for the saline group. The odds ratio is the same for both genotypes.
The model also includes a significant two-way interaction between genotype and survival. The odds of survival for the wild type are higher than the odds of survival for the knockout. This effect is the same for both treatment groups.
At a Glance
| Decision Point | What to Do | Why It Matters |
|---|---|---|
| Data structure | Confirm the data are counts in a multi-way table with independent observations | Log-linear models require count data and independent cells |
| Model selection | Compare nested models with likelihood ratio tests and information criteria | Identifies which interactions are necessary to describe the data |
| Interaction interpretation | Convert parameters to odds ratios and examine conditional associations | Provides biologically meaningful measures of association |
| Model fit | Check the deviance and expected cell counts | Poor fit indicates missing structure or sparse data problems |
| Reporting | Document the model selection process and all fitted models | Ensures transparency and reproducibility of the analysis |
Practical Workflow for Log-Linear Analysis
Step 1: Define the Research Question
The first step is to define the research question in terms of the table dimensions. What variables are in the table? Which interactions are of interest? The research question determines which models to fit and which parameters to interpret.
For example, the research question might be whether a treatment effect differs across genotypes. This question is about the three-way interaction among treatment, genotype, and outcome. The analysis should focus on testing this interaction.
Step 2: Check the Data Structure
The data must be in the form of a contingency table. Each cell of the table contains the count of observations with a particular combination of the variable levels. The data can be entered as a table or as a data frame with one row per observation.
The data must be checked for structural zeros. A structural zero is a cell that cannot have any observations by design. For example, a cell that represents a combination of variables that is impossible. Structural zeros must be handled differently from sampling zeros, which are cells that happen to have no observations.
Step 3: Fit the Saturated Model
The saturated model includes all main effects and all interactions. This model reproduces the observed data exactly. It is not useful for inference, but it provides a baseline for comparison. The deviance of the saturated model is zero.
The saturated model is used to check the adequacy of the model fitting procedure. If the saturated model does not reproduce the data exactly, there is a problem with the data or the software.
Step 4: Fit a Series of Nested Models
The next step is to fit a series of nested models. The models range from the independence model to the saturated model. Each model is a special case of the next model in the series. The models are compared using likelihood ratio tests.
The series of models is defined by the interactions that are included. The independence model includes only main effects. The next model adds all two-way interactions. The next model adds all three-way interactions, and so on.
Step 5: Select the Best Model
The best model is selected based on the likelihood ratio tests and the information criteria. The model with the lowest AIC or BIC is preferred. The model should also be checked for goodness of fit.
The selected model should be the simplest model that fits the data well. The model should not include unnecessary interactions. The model should also be interpretable in the context of the research question.
Step 6: Interpret the Parameters
The parameters of the selected model are interpreted in terms of odds ratios. The main effects are the log odds of the outcome for each level of the variable. The interaction terms are the log odds ratios.
The interpretation of the parameters depends on the model. In a model with a three-way interaction, the two-way interactions are conditional on the levels of the third variable. The interpretation must be done separately for each level of the third variable.
Step 7: Report the Results
The results of the analysis are reported in a clear and transparent way. The report should include the model selection process, the fitted model, the parameter estimates, and the odds ratios. The report should also include the goodness of fit statistics.
The EQUATOR Network's searchable registry catalogues reporting checklists for every major biomedical study design, enabling researchers to select appropriate transparency standards for their work.
Records and Measurements
What to Record
The analysis should be documented in a way that allows another researcher to reproduce the results. The documentation should include the data, the model fitting code, and the output. The documentation should also include the decisions made during the analysis.
The following records should be kept:
- The raw data in a machine-readable format
- The data dictionary that defines each variable and its levels
- The model fitting code or the software commands
- The output of the model fitting, including the deviance and parameter estimates
- The model selection process, including the models compared and the test statistics
- The final model and its interpretation
Reproducibility
Reproducibility is a core principle of scientific research. The analysis should be reproducible by another researcher with access to the data and the code. The code should be well-commented and organized. The data should be in a stable format.
NIH's Data Management and Sharing Policy requires investigators to articulate how scientific data will be preserved and made accessible to the broader research community.
Quality Checks
The analysis should include quality checks to ensure the results are reliable. The checks should include:
- Checking the data for errors and inconsistencies
- Checking the model for convergence
- Checking the expected cell counts for small values
- Checking the residuals for patterns
The residuals are the differences between the observed and expected counts. The residuals can be examined to identify cells that are poorly fitted by the model. The residuals can also be used to identify outliers.
Common Failure Patterns
Sparse Data
Sparse data is a common problem in log-linear analysis. Sparse data occurs when the expected cell counts are small. The chi-square distribution may not be a good approximation for the test statistics. The parameter estimates may be biased.
The rule of thumb is that the expected cell counts should be at least 5 for the chi-square approximation to be valid. When the expected counts are smaller, the analysis may need to be adjusted. The options include combining categories, using exact tests, or using a different model.
Structural Zeros
Structural zeros are cells that cannot have observations by design. For example, a cell that represents a combination of variables that is impossible. Structural zeros must be handled differently from observed zeros. The model must be specified to account for the structural zeros.
The structural zeros are handled by including a term in the model that sets the expected count to zero for those cells. This is done by including a dummy variable for the structural zero cells. The model is then fitted with the dummy variable.
Overfitting
Overfitting occurs when the model is too complex for the data. The model fits the observed data well but does not generalize to new data. The overfitting is a problem because the parameter estimates are not stable. The model selection criteria are designed to avoid overfitting.
The information criteria penalize the model for the number of parameters. The model with the lowest information criterion is the best trade-off between fit and complexity. The model should not include interactions that are not supported by the data.
Non-Independence
The log-linear model assumes that the observations are independent. The independence assumption is violated when the observations are correlated. The correlated observations can occur in clustered data, such as data from the same individual or the same family.
When the observations are not independent, the log-linear model is not appropriate. The model must be extended to account for the correlation. The extensions include the generalized estimating equations and the mixed-effects models.
Limitations and Interpretation Boundaries
The Model Is Not Causal
The log-linear model is a descriptive tool. It describes the associations among the variables. The model does not establish causality. The model cannot distinguish between the outcome and the predictor. The model is symmetric in the variables.
The causal interpretation of the model requires additional assumptions. The causal interpretation requires a design that supports causal inference. The design includes randomization or a natural experiment.
The Model Is Not a Substitute for a Designed Experiment
The log-linear model is a tool for analyzing observational data. The model cannot overcome the limitations of the design. The model cannot control for confounding variables that are not in the table. The model cannot adjust for the selection bias.
The design of the study is the most important factor in the validity of the conclusions. The log-linear model is only as good as the data. The model cannot fix a flawed design.
The Model Is Not a Substitute for a Logistic Regression
The log-linear model is a symmetric model. The model treats all variables as responses. The model is not designed to predict one variable from the others. The logistic regression is designed for prediction.
The logistic regression is a special case of the log-linear model. The logistic regression is used when one variable is the outcome and the others are predictors. The log-linear model is used when all variables are responses.
The Model Is Not a Substitute for a Mixed-Effects Model
The log-linear model does not account for the random effects. The model assumes that the observations are independent. The mixed-effects model accounts for the correlation among the observations.
The mixed-effects model is used when the data are clustered. The data are clustered when the observations are from the same individual or the same family. The mixed-effects model is a more complex model that requires more data.
Safety and Regulatory Context
The Use of Log-Linear Models in Research
The log-linear model is a statistical tool. The model is used in research to analyze the data. The model is not a regulated product. The model is not subject to the regulatory approval.
The use of the model is governed by the principles of good research practice. The research should be conducted in a way that is transparent and reproducible. The research should be reported in a way that is complete and accurate.
The Reporting of Research
The reporting of research is governed by the reporting guidelines. The guidelines are provided by the EQUATOR Network. The guidelines are designed to improve the quality of the reporting of research.
The guidelines are specific to the study type. The guidelines for the randomized controlled trial are different from the guidelines for the observational study. The guidelines for the diagnostic study are different from the guidelines for the prognostic study.
The Publication of Research
The publication of research is governed by the publication ethics. The publication ethics are provided by the Committee on Publication Ethics. The ethics are designed to ensure the integrity of the research record.
The ethics include the authorship, the peer review, the data, the conflicts of interest, and the misconduct. The ethics are the responsibility of the authors, the reviewers, and the editors.
The Funding of Research
The funding of research is governed by the funding policies. The funding policies are provided by the National Institutes of Health. The policies are designed to ensure the responsible use of the funds.
The policies include the application, the review, and the award management. The policies also include the data management and sharing. The policies are designed to ensure the research is conducted in a way that is responsible and transparent.
Professional Escalation Criteria
When to Consult a Biostatistician
The log-linear model is a complex statistical method. The model requires a good understanding of the statistical theory. The model also requires a good understanding of the software.
The researcher should consult a biostatistician when the data are sparse. The sparse data can cause problems with the model. The biostatistician can help with the analysis.
The researcher should consult a biostatistician when the data are not independent. The non-independent data require a more complex model. The biostatistician can help with the model.
The researcher should consult a biostatistician when the model does not fit the data. The model may not fit the data because of the missing interactions. The biostatistician can help with the model.
When to Escalate to a Supervisor
The researcher should escalate to a supervisor when the analysis is not going well. The analysis may not be going well when the model is not converging. The model may not be converging when the data are sparse.
The researcher should escalate to a supervisor when the results are not clear. The results may not be clear when the model is complex. The supervisor can help with the interpretation.
The researcher should escalate to a supervisor when the data are not reliable. The data may not be reliable when the data are not collected properly. The supervisor can help with the data.
When to Escalate to an Institutional Official
The researcher should escalate to an institutional official when the research is not conducted properly. The research may not be conducted properly when the data are fabricated. The data may be fabricated when the data are not collected.
The researcher should escalate to an institutional official when the research is not reported properly. The research may not be reported properly when the results are not reported. The results may not be reported when the results are not significant.
The researcher should escalate to an institutional official when the research is not funded properly. The research may not be funded properly when the funds are not used. The funds may not be used when the funds are not accounted for.
Frequently Asked Questions
What is the difference between a log-linear model and a logistic regression?
A log-linear model treats all variables as responses and models the expected cell counts. A logistic regression designates one variable as the outcome and models the probability of that outcome. The two methods are related. A log-linear model with one variable designated as the outcome is equivalent to a logistic regression with the other variables as predictors.
How do I choose between the AIC and the BIC for model selection?
The AIC is better for prediction. The BIC is better for identifying the true model. The AIC penalizes the number of parameters less heavily than the BIC. The BIC penalizes the number of parameters more heavily for large samples. The choice depends on the research goal.
What is the minimum expected cell count for a log-linear model?
The chi-square approximation for the test statistics is valid when the expected cell counts are at least 5. When the expected counts are smaller, the approximation may not be valid. The model may need to be adjusted. The adjustment may include combining categories or using exact methods.
How do I interpret a three-way interaction?
A three-way interaction indicates that the association between two variables depends on the level of the third variable. The interaction is interpreted by examining the conditional odds ratios. The odds ratio between two variables is calculated separately for each level of the third variable.
What is the difference between a structural zero and an observed zero?
A structural zero is a cell that cannot have observations by design. An observed zero is a cell that happens to have no observations. The structural zeros must be handled differently from the observed zeros. The structural zeros are included in the model with a dummy variable.
Can I use a log-linear model for data with more than three variables?
Yes. The log-linear model can be used for data with any number of variables. The model becomes more complex as the number of variables increases. The model selection becomes more difficult. The interpretation of the higher-order interactions becomes more complex.
What should I do if the model does not fit the data?
If the model does not fit the data, the model is missing important structure. The model should be expanded to include more interactions. The model should be checked for the sparse data. The model should be checked for the structural zeros.
How do I report the results of a log-linear model?
The results should be reported in a transparent way. The report should include the model selection process, the fitted model, the parameter estimates, and the odds ratios. The report should also include the goodness of fit statistics. The report should follow the reporting guidelines for the study type.
Related Bioinformatics Guides
- What Is a Data Warehouse? A Practical Guide for Life Science Organizations
- Persistent Identifiers for Research Data: A Guide to Selection and Use
- Data Science and AI in Life Sciences: Applications and Emerging Trends
- Data Stewardship vs. Data Governance: Roles and Responsibilities in Research
- Research Data Stewardship: Benefits and Implementation Strategies
References and Further Reading
- Research Methods Resources. National Library of Medicine.
- EQUATOR Network. EQUATOR Network.
- Core Practices. Committee on Publication Ethics.
- NIH Grants and Funding. National Institutes of Health.
- ORCID for Researchers. ORCID.
- Data Management and Sharing Policy. National Institutes of Health.
- NCBI Data Resources. National Center for Biotechnology Information.
- EMBL-EBI Training. European Bioinformatics Institute.
- Exploring temporal interactions of crash counts in California using distinct log-linear contingency table models.. International journal of injury control and safety promotion, 2021.
- ϕ-Divergence in Contingency Table Analysis.. Entropy (Basel, Switzerland), 2018.
- Log-linear model-based multifactor dimensionality reduction method to detect gene gene interactions.. Bioinformatics (Oxford, England), 2007.
This article is educational and does not replace validated analysis plans, institutional policy, clinical interpretation, or specialist review.