# Regression dilution and measurement error in R
# https://tidyecology.com/posts/measurement-error-regression-dilution/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-11-15

## ---- setup ----
suppressMessages({library(ggplot2); library(dplyr); library(tidyr)})

theme_te <- theme_minimal(base_size = 12) +
  theme(
        plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
        panel.background = element_rect(fill = "#f5f4ee", colour = NA),
        panel.grid.minor = element_blank(),
        plot.title = element_text(face = "bold"),
        legend.position = "top")
col_true <- "#2f8f63"; col_obs <- "#b5534e"; col_corr <- "#cda23f"

## ---- simulate ----
set.seed(4127)
n   <- 300
b0  <- 1; b1 <- 2
sdx <- 3            # spread of the true predictor: var_x = 9
sde <- 2            # residual spread
sdu <- 3            # measurement-error spread: var_u = 9

x <- rnorm(n, mean = 10, sd = sdx)
y <- b0 + b1 * x + rnorm(n, 0, sde)
w <- x + rnorm(n, 0, sdu)          # what we actually record

fit_true  <- lm(y ~ x)             # unavailable in practice
fit_naive <- lm(y ~ w)             # what everyone fits

## ---- naive-numbers ----
b_true  <- unname(coef(fit_true)[2]);  se_true  <- summary(fit_true)$coef[2, 2]
b_naive <- unname(coef(fit_naive)[2]); se_naive <- summary(fit_naive)$coef[2, 2]
lambda  <- sdx^2 / (sdx^2 + sdu^2)

## ---- fig-attenuation ----
d1 <- bind_rows(
  data.frame(pred = x, y = y, kind = "true X"),
  data.frame(pred = w, y = y, kind = "recorded W")
)
d1$kind <- factor(d1$kind, levels = c("true X", "recorded W"))

ggplot(d1, aes(pred, y, colour = kind)) +
  geom_point(alpha = 0.35, size = 1.1) +
  geom_smooth(method = "lm", formula = y ~ x, se = FALSE, linewidth = 1.1) +
  scale_colour_manual(values = c("true X" = col_true, "recorded W" = col_obs),
                      name = NULL) +
  labs(title = "Adding error to a predictor flattens the slope",
       x = "predictor value", y = "response") +
  theme_te

## ---- correct ----
var_u      <- sdu^2                       # assumed known
lambda_hat <- (var(w) - var_u) / var(w)   # reliability from the recorded data
b_corr     <- b_naive / lambda_hat        # attenuation-corrected slope
se_corr    <- se_naive / lambda_hat       # naive SE scaled up: too small, see text

## ---- sweep ----
set.seed(2024)
sdu_grid <- seq(0, 4, by = 0.5)
reps     <- 400
naive_s <- corr_s <- numeric(length(sdu_grid))
for (i in seq_along(sdu_grid)) {
  su <- sdu_grid[i]
  ns <- cs <- numeric(reps)
  for (r in seq_len(reps)) {
    xx <- rnorm(n, 10, sdx)
    yy <- b0 + b1 * xx + rnorm(n, 0, sde)
    ww <- xx + rnorm(n, 0, su)
    bn <- coef(lm(yy ~ ww))[2]
    lh <- (var(ww) - su^2) / var(ww)
    ns[r] <- bn; cs[r] <- bn / lh
  }
  naive_s[i] <- mean(ns); corr_s[i] <- mean(cs)
}
lam_grid <- sdx^2 / (sdx^2 + sdu_grid^2)

## ---- sweep-numbers ----
max_dev_corr  <- max(abs(corr_s - b1))
max_dev_naive <- max(abs(naive_s - b1 * lam_grid))

## ---- fig-curve ----
d2 <- data.frame(lambda = lam_grid, naive = naive_s, corrected = corr_s,
                 theory = b1 * lam_grid) |>
  pivot_longer(c(naive, corrected), names_to = "estimator", values_to = "slope")

ggplot(d2, aes(lambda)) +
  geom_line(aes(y = theory), linetype = "dashed", colour = "#5d6b61") +
  geom_hline(yintercept = b1, colour = col_true, linewidth = 0.4) +
  geom_point(aes(y = slope, colour = estimator), size = 2.4) +
  scale_colour_manual(values = c(naive = col_obs, corrected = col_corr), name = NULL) +
  scale_x_reverse() +
  labs(title = "Naive slope tracks the attenuation curve; correction restores it",
       x = "reliability ratio (more error to the right)", y = "estimated slope") +
  theme_te

## ---- response-error ----
set.seed(9931)
y_noisy <- y + rnorm(n, 0, 4)      # error added to the response, not the predictor
fit_yv  <- lm(y_noisy ~ x)

## ---- response-numbers ----
b_yv  <- unname(coef(fit_yv)[2]); se_yv <- summary(fit_yv)$coef[2, 2]
r2_a  <- summary(fit_true)$r.squared
r2_b  <- summary(fit_yv)$r.squared
