4.7 Practice
The practice problems for Chapter 4 ask you to fit linear and polynomial regressions, interpret posterior intervals, and compare models with different predictors.
Easy problems
4E1. In the model definition below, which line is the likelihood?
4E2. In the model above, how many parameters are in the posterior distribution?
4E3. Using the prior predictive simulation, what range of heights does the prior imply for a person of average weight?
# 4E3: Prior predictive for alpha
set.seed(42)
alpha_prior <- rnorm(1e4, 178, 20)
cat("Prior for alpha:\n")
cat(" Mean:", round(mean(alpha_prior), 2), "\n")
cat(" 95% interval: [", round(quantile(alpha_prior, 0.025), 2), ",",
round(quantile(alpha_prior, 0.975), 2), "]\n")4E4. What does the prior imply about the slope?
Medium problems
4M1. Simulate observed height from the prior predictive distribution for the following model:
# 4M1: Prior predictive simulation
set.seed(42)
n <- 100
x <- runif(n, 0, 10)
# Draw from priors
alpha <- rnorm(1, 0, 10)
beta <- rnorm(1, 0, 1)
sigma <- runif(1, 0, 10)
# Simulate data
mu <- alpha + beta * x
y <- rnorm(n, mu, sigma)
cat("Prior draw:\n")
cat(" alpha:", round(alpha, 2), "\n")
cat(" beta:", round(beta, 2), "\n")
cat(" sigma:", round(sigma, 2), "\n")
plot(x, y, pch = 16, col = "steelblue",
main = "4M1: Prior predictive simulation",
xlab = "x", ylab = "y")
abline(a = alpha, b = beta, lwd = 2, col = "firebrick")4M2. Translate the model above into a quadratic approximation using lm or quap.
4M3. A researcher says "the slope is 2.3, so a 1 kg increase in weight causes a 2.3 cm increase in height." Is this interpretation correct? What is missing?
Hard problems
4H1. Fit a linear regression of height on weight using the simulated Howell data. Compute the posterior distribution of the expected height for a person weighing 50 kg.
# 4H1: Posterior prediction at weight = 50 kg
set.seed(42)
n <- 352
weight <- rnorm(n, mean = 35, sd = 5)
height <- 100 + 2.5 * weight + rnorm(n, sd = 4)
fit <- lm(height ~ weight)
library(MASS)
post_samples <- mvrnorm(1e4, mu = coef(fit), Sigma = vcov(fit))
# Predict height at weight = 50
pred_50 <- post_samples[, 1] + post_samples[, 2] * 50
cat("Posterior prediction at 50 kg:\n")
cat(" Mean:", round(mean(pred_50), 2), "cm\n")
cat(" 95% interval: [", round(quantile(pred_50, 0.025), 2), ",",
round(quantile(pred_50, 0.975), 2), "] cm\n")4H2. Fit a quadratic regression of height on weight. Compare it to the linear model using AIC. Which model is preferred?
# 4H2: Compare linear and quadratic models with AIC
set.seed(42)
n <- 352
weight <- rnorm(n, mean = 35, sd = 5)
height <- 100 + 2.5 * weight + rnorm(n, sd = 4)
m_linear <- lm(height ~ weight)
m_quad <- lm(height ~ weight + I(weight^2))
cat("AIC comparison:\n")
cat("Linear:", round(AIC(m_linear), 2), "\n")
cat("Quadratic:", round(AIC(m_quad), 2), "\n")
cat("Difference:", round(AIC(m_linear) - AIC(m_quad), 2), "\n")
cat("\nIf difference > 2, the quadratic model is preferred\n")4H3. Fit a cubic regression and plot the posterior predictive lines. Does the cubic term improve the model? Use posterior predictive checks to decide.