Skip to content
Statistical RethinkingStatistical Rethinking

14.4 Practice

The practice problems for Chapter 14 ask you to fit models with measurement error and missing data, and to compare the results with naive analyses.

Practice

Easy problems

14E1. What is regression dilution? How does measurement error in the predictor affect the estimated slope?

14E2. What are the three types of missing data (MCAR, MAR, MNAR)? Which is easiest to handle?

14E3. Why is mean imputation usually a bad idea?

14E4. Why does Bayesian imputation propagate uncertainty better than multiple imputation?

Medium problems

14M1. Simulate data with measurement error in the predictor. Compare the naive slope with the true slope. Show the attenuation.

# 14M1: Measurement error and attenuation
set.seed(42)
n <- 300
x_true <- runif(n, 0, 10)
y <- 1 + 0.6 * x_true + rnorm(n, 0, 1)

# Different levels of measurement error

for (sigma_x in c(0.5, 1.0, 2.0, 3.0)) {
x_obs <- x_true + rnorm(n, 0, sigma_x)
m <- lm(y ~ x_obs)
cat(sprintf(" sigma_x = %.1f: naive slope = %.3f (true = 0.600)\n",
sigma_x, coef(m)[2]))
}

cat("\n Larger measurement error -> more attenuation (slope closer to 0)\n")
Click Run to execute this code in your browser with WebR

14M2. Create missing data under MAR. Compare complete-case analysis with conditional imputation.

# 14M2: MAR missing data — complete-case vs imputation
set.seed(42)
n <- 400
x <- runif(n, 0, 10)
y <- 2 + 0.5 * x + rnorm(n, 0, 1)

# MAR: x missing when y is high

miss_idx <- which(y > quantile(y, 0.75))
x_obs <- x
x_obs[miss_idx] <- NA

# Complete-case

d_cc <- data.frame(y = y, x = x_obs)[complete.cases(data.frame(y = y, x = x_obs)), ]
m_cc <- lm(y ~ x, data = d_cc)

# Conditional imputation (use y to predict missing x)

d_obs <- data.frame(y = y[!is.na(x_obs)], x = x_obs[!is.na(x_obs)])
m_imp <- lm(x ~ y, data = d_obs)
x_imputed <- ifelse(is.na(x_obs),
predict(m_imp, newdata = data.frame(y = y)),
x_obs)
m_full <- lm(y ~ x_imputed)

cat("MAR missing data comparison:\n")
cat(" True slope: 0.500\n")
cat(" Complete-case slope:", round(coef(m_cc)[2], 3), "(biased)\n")
cat(" Imputed slope:", round(coef(m_full)[2], 3), "(closer to true)\n")
cat(" N complete cases:", nrow(d_cc), "out of", n, "\n")
Click Run to execute this code in your browser with WebR

Hard problems

14H1. Simulate data with measurement error in both the predictor and the outcome. Fit a model that accounts for both. How does this change the estimates?

# 14H1: Measurement error in both x and y
set.seed(42)
n <- 300
x_true <- runif(n, 0, 10)
y_true <- 1 + 0.5 * x_true

# Observed with error

x_obs <- x_true + rnorm(n, 0, 1.0)
y_obs <- y_true + rnorm(n, 0, 1.0)

# Naive model (ignores both errors)

m_naive <- lm(y_obs ~ x_obs)

# Corrected model (regression calibration for x)

reliability_x <- var(x_true) / var(x_obs)
x_corrected <- mean(x_obs) + reliability_x * (x_obs - mean(x_obs))
m_corrected <- lm(y_obs ~ x_corrected)

cat("Measurement error in both x and y:\n")
cat(" True slope: 0.500\n")
cat(" Naive slope:", round(coef(m_naive)[2], 3), "(attenuated)\n")
cat(" Corrected slope (x only):", round(coef(m_corrected)[2], 3), "\n")
cat("\n Error in y increases variance but does not bias slope.\n")
cat(" Error in x attenuates the slope.\n")
cat(" Full Bayesian model would handle both simultaneously.\n")
Click Run to execute this code in your browser with WebR

14H2. Create data with MNAR missingness (missingness depends on the unobserved value itself). Show that even Bayesian imputation is biased without an explicit missingness model.

14H3. Fit a model that simultaneously handles measurement error in the predictor and missing data. Compare with a naive analysis that ignores both.