# Correcting measurement error with SIMEX
# https://tidyecology.com/posts/simex-measurement-error-correction/
#
# 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-10-14

## ---- setup ----
suppressMessages({library(ggplot2); library(dplyr)})
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_pt <- "#275139"; col_q <- "#b5534e"; col_r <- "#cda23f"; col_true <- "#2f8f63"

## ---- simulate ----
set.seed(78)
n     <- 300
b0    <- 1; b1 <- 2
sdx   <- 3; sde <- 2
sdu   <- 2                       # measurement-error sd; variance 4 assumed known
var_u <- sdu^2

x <- rnorm(n, 10, sdx)
y <- b0 + b1 * x + rnorm(n, 0, sde)
w <- x + rnorm(n, 0, sdu)

## ---- simex-loop ----
lambda_grid <- seq(0, 2, by = 0.25)
B           <- 200
beta_lambda <- numeric(length(lambda_grid))

set.seed(99)
for (i in seq_along(lambda_grid)) {
  la <- lambda_grid[i]
  bb <- numeric(B)
  for (b in seq_len(B)) {
    w_star <- w + rnorm(n, 0, sqrt(la * var_u))   # extra error, variance la * var_u
    bb[b]  <- coef(lm(y ~ w_star))[2]
  }
  beta_lambda[i] <- mean(bb)
}

## ---- simex-numbers ----
b_naive <- beta_lambda[1]

## ---- extrapolate-quad ----
ext_q   <- lm(beta_lambda ~ lambda_grid + I(lambda_grid^2))
simex_q <- unname(predict(ext_q, newdata = data.frame(lambda_grid = -1)))

## ---- fig-simex ----
grid_plot <- seq(-1, 2, length.out = 200)
dcur <- data.frame(lambda_add = grid_plot,
                   slope = predict(ext_q, data.frame(lambda_grid = grid_plot)))
dpts <- data.frame(lambda_add = lambda_grid, slope = beta_lambda)

ggplot() +
  annotate("rect", xmin = -1, xmax = 0, ymin = -Inf, ymax = Inf,
           fill = "#93a87f", alpha = 0.14) +
  geom_hline(yintercept = b1, colour = col_true, linewidth = 0.5) +
  geom_line(data = subset(dcur, lambda_add >= 0), aes(lambda_add, slope),
            colour = col_q, linewidth = 1.0) +
  geom_line(data = subset(dcur, lambda_add <= 0), aes(lambda_add, slope),
            colour = col_q, linewidth = 1.0, linetype = "22") +
  geom_point(data = dpts, aes(lambda_add, slope), colour = col_pt, size = 2.4) +
  annotate("point", x = 0, y = b_naive, colour = "#16241d", size = 3) +
  annotate("point", x = -1, y = simex_q, colour = "#16241d", size = 3) +
  annotate("text", x = 0.05, y = b_naive, label = "naive", hjust = 0, vjust = 1.6, size = 3.4) +
  annotate("text", x = -0.95, y = simex_q, label = "SIMEX", hjust = 0, vjust = -0.9, size = 3.4) +
  labs(title = "SIMEX: simulate more error, then extrapolate back to none",
       x = "added measurement error (multiples of the known variance)",
       y = "estimated slope") +
  theme_te

## ---- extrapolate-rational ----
# beta(lambda) = (g0 + g1*lambda)/(g2 + lambda) is linear in the parameters once the
# denominator is cleared: beta*lambda = g0 + g1*lambda - g2*beta. That linear fit supplies
# starting values near the optimum, and from them the port algorithm converges on this
# grid, though the valley is shallow and ill-conditioned (the grid points are nearly
# straight in lambda over 0 to 2): with other seeds it can stop on a false-convergence
# error, and optim() on the same sum of squares from the same start is the fallback.
lin_start <- lm(I(beta_lambda * lambda_grid) ~ lambda_grid + beta_lambda)
ext_r   <- nls(beta_lambda ~ (g0 + g1 * lambda_grid) / (g2 + lambda_grid),
               start = list(g0 =  unname(coef(lin_start)[1]),
                            g1 =  unname(coef(lin_start)[2]),
                            g2 = -unname(coef(lin_start)[3])),
               algorithm = "port")
cf      <- coef(ext_r)
simex_r <- unname((cf["g0"] + cf["g1"] * (-1)) / (cf["g2"] + (-1)))

lam_hat  <- (var(w) - var_u) / var(w)
b_moment <- unname(coef(lm(y ~ w))[2] / lam_hat)

## ---- fig-extrapolant ----
pr <- (cf["g0"] + cf["g1"] * grid_plot) / (cf["g2"] + grid_plot)
d2 <- rbind(
  data.frame(lambda_add = grid_plot, slope = dcur$slope, extrapolant = "quadratic"),
  data.frame(lambda_add = grid_plot, slope = pr,        extrapolant = "rational linear"))

ggplot() +
  geom_hline(yintercept = b1, colour = col_true, linewidth = 0.5) +
  geom_vline(xintercept = 0, colour = "#dad9ca", linewidth = 0.4) +
  geom_line(data = d2, aes(lambda_add, slope, colour = extrapolant), linewidth = 1.0) +
  geom_point(data = dpts, aes(lambda_add, slope), colour = col_pt, size = 2.2) +
  annotate("point", x = -1, y = simex_q, colour = col_q, size = 3) +
  annotate("point", x = -1, y = simex_r, colour = col_r, size = 3) +
  scale_colour_manual(values = c("quadratic" = col_q, "rational linear" = col_r), name = NULL) +
  labs(title = "The extrapolant is a modelling choice",
       x = "added measurement error", y = "estimated slope") +
  theme_te
