0% found this document useful (0 votes)
8 views

Log-linear Models for Two-Way Tables in R

The document explains log-linear models for analyzing relationships between categorical variables in two-way and three-way contingency tables using R. It provides examples using the Titanic dataset to demonstrate how to fit these models and interpret the significance of interactions. The approach aids in understanding associations and dependencies among categorical variables.

Uploaded by

Navon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Log-linear Models for Two-Way Tables in R

The document explains log-linear models for analyzing relationships between categorical variables in two-way and three-way contingency tables using R. It provides examples using the Titanic dataset to demonstrate how to fit these models and interpret the significance of interactions. The approach aids in understanding associations and dependencies among categorical variables.

Uploaded by

Navon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Log-linear Models for Two-Way Tables in R

A log-linear model for a two-way contingency table helps analyze the relationship between
two categorical variables. It models the logarithm of expected cell counts as a function of the
main effects and their interaction.

Example in R

We'll use the built-in Titanic dataset (converted to a two-way table) to examine the
relationship between Survival and Class.

# Load necessary library


library(MASS)

# Load Titanic dataset and convert to a two-way contingency table


data("Titanic")
titanic_table <- margin.table(Titanic, c(1, 4)) # Select Class and Survival

# Fit a log-linear model


log_model <- loglm(~ Class + Survived + Class:Survived, data = titanic_table)

# Display model summary


summary(log_model)

Explanation

margin.table(Titanic, c(1, 4)):

Extracts a two-way contingency table of Class and Survival from the Titanic dataset.

loglm(~ Class + Survived + Class:Survived, data = titanic_table):

Fits a log-linear model with main effects (Class and Survived) and their interaction
(Class:Survived).

summary(log_model):

Displays the goodness-of-fit and tests the significance of interactions.

Interpretation

If the interaction term (Class:Survived) is significant, it indicates a dependence between


Class and Survival (i.e., survival chances vary by class).
If it is not significant, Class and Survived are independent.

This approach helps in understanding associations between categorical variables in two-way


tables.

Log-linear Models for Three-Way Tables in R

A log-linear model for a three-way contingency table analyzes the relationships among three
categorical variables. It helps determine whether interactions exist between them by
modeling the logarithm of expected cell counts.

Example in R

We'll use the Titanic dataset to study the relationships between Class, Survival, and Sex.

# Load necessary library


library(MASS)

# Load Titanic dataset and create a three-way contingency table


data("Titanic")
titanic_table <- margin.table(Titanic, c(1, 2, 4)) # Selecting Class, Sex, and Survival

# Fit a log-linear model


log_model <- loglm(~ Class + Sex + Survived +
Class:Sex + Class:Survived + Sex:Survived,
data = titanic_table)

# Display model summary


summary(log_model)

Explanation

margin.table(Titanic, c(1, 2, 4)):

Creates a three-way contingency table with Class, Sex, and Survival.

loglm(~ Class + Sex + Survived + Class:Sex + Class:Survived + Sex:Survived, data =


titanic_table):

Includes main effects (Class, Sex, Survived) and two-way interactions (Class:Sex,
Class:Survived, Sex:Survived).

summary(log_model):

Displays the goodness-of-fit and significance of interactions.


Interpretation

If two-way interactions are significant, it suggests dependence among the variables.

If higher-order interactions (like Class:Sex:Survived) are added and significant, it means that
survival patterns depend on the combination of both Class and Sex.

This method helps in understanding complex categorical data relationships in three-way


contingency tables.

You might also like