Skip to content
Statistical RethinkingStatistical Rethinking

5.7 Practice

The practice problems for Chapter 5 ask you to fit multivariate regressions, diagnose multicollinearity, and explore the effect of adding or removing variables.

Practice

Easy problems

5E1. Which of the linear models below are multiple regressions?

(a) yi∼Normal(α+βxi,σ)y_i \sim \text{Normal}(\alpha + \beta x_i, \sigma)

(b) yi∼Normal(α+β1x1,i+β2x2,i,σ)y_i \sim \text{Normal}(\alpha + \beta_1 x_{1,i} + \beta_2 x_{2,i}, \sigma)

(c) yi∼Normal(α+β1x1,i+β2x2,i+β3x3,i,σ)y_i \sim \text{Normal}(\alpha + \beta_1 x_{1,i} + \beta_2 x_{2,i} + \beta_3 x_{3,i}, \sigma)

5E2. Write down a multiple regression to evaluate the claim: Neither amount of funding nor test scores predict teacher quality, but together they do.

5E3. Write down a multiple regression to evaluate the claim: The amount of funding is positively associated with teacher quality, but only when controlling for the percentage of students in poverty.

Medium problems

5M1. Invent your own example of a spurious association. Draw the causal graph and simulate data to demonstrate.

# 5M1: Spurious association example
# Ice cream sales and sunburns are both caused by temperature
set.seed(42)
n <- 100

# Temperature is the common cause

temp <- rnorm(n, mean = 25, sd = 8)

# Ice cream sales increase with temperature

ice_cream <- 10 + 2 * temp + rnorm(n, sd = 5)

# Sunburns increase with temperature

sunburns <- 5 + 1.5 * temp + rnorm(n, sd = 4)

# Bivariate: ice cream vs sunburns (spurious)

m_biv <- lm(sunburns ~ ice_cream)
cat("Bivariate (sunburns ~ ice_cream):\n")
cat(" beta:", round(coef(m_biv)[2], 3), "\n")

# Multivariate: controlling for temperature

m_multi <- lm(sunburns ~ ice_cream + temp)
cat("\nMultivariate (sunburns ~ ice_cream + temp):\n")
cat(" beta_ice_cream:", round(coef(m_multi)[2], 3), "\n")
cat(" beta_temp:", round(coef(m_multi)[3], 3), "\n")

cat("\nThe ice cream effect disappears after controlling for temperature.\n")
Click Run to execute this code in your browser with WebR

5M2. Invent your own example of a masked relationship. Draw the causal graph and simulate data to demonstrate.

5M3. Invent your own example of post-treatment bias. Draw the causal graph and simulate data to demonstrate.

Hard problems

5H1. Fit a multivariate regression with two predictors. Compute the posterior distribution of both coefficients and their 95% credible intervals.

# 5H1: Multivariate regression with posterior intervals
set.seed(42)
n <- 100
x1 <- rnorm(n)
x2 <- 0.5 * x1 + rnorm(n, sd = 0.5)
y <- 1 + 2 * x1 - 1 * x2 + rnorm(n, sd = 1)

fit <- lm(y ~ x1 + x2)
library(MASS)
post <- mvrnorm(1e4, mu = coef(fit), Sigma = vcov(fit))

cat("Posterior summaries:\n")
cat(" alpha: mean =", round(mean(post[,1]), 3),
" 95% CI = [", round(quantile(post[,1], 0.025), 3), ",",
round(quantile(post[,1], 0.975), 3), "]\n")
cat(" beta1: mean =", round(mean(post[,2]), 3),
" 95% CI = [", round(quantile(post[,2], 0.025), 3), ",",
round(quantile(post[,2], 0.975), 3), "]\n")
cat(" beta2: mean =", round(mean(post[,3]), 3),
" 95% CI = [", round(quantile(post[,3], 0.025), 3), ",",
round(quantile(post[,3], 0.975), 3), "]\n")
Click Run to execute this code in your browser with WebR

5H2. Fit a model with a categorical predictor using index variables. Compare the posterior means of each category.

5H3. Fit a model with both a categorical and a continuous predictor. Plot the posterior predictive lines for each category.