# Checking a measurement-error correction
# https://tidyecology.com/posts/checking-measurement-error-corrections/
#
# 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-09-27

## ---- 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_naive <- "#b5534e"; col_corr <- "#2f8f63"; col_true <- "#275139"; col_acc <- "#cda23f"

## ---- replicates ----
set.seed(7314)
m   <- 80                         # units measured repeatedly
r   <- 3                          # measurements per unit
sdx <- 3; sdu <- 2                # true between-unit sd 3, error sd 2

xt  <- rnorm(m, 10, sdx)          # latent true value of each unit
dat <- data.frame(
  unit = factor(rep(1:m, each = r)),
  w    = rep(xt, each = r) + rnorm(m * r, 0, sdu)
)

fit  <- aov(w ~ unit, data = dat)
ms   <- summary(fit)[[1]][, "Mean Sq"]
ms_b <- ms[1]; ms_w <- ms[2]

var_u_hat <- ms_w                 # within-unit mean square estimates error variance
var_x_hat <- (ms_b - ms_w) / r    # between minus within, scaled, estimates true variance
icc       <- var_x_hat / (var_x_hat + var_u_hat)

## ---- worth-it ----
b1 <- 2; b0 <- 1; n <- 200; sde <- 2; sdx2 <- 3; varx <- sdx2^2
rel_levels <- c(0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99)
reps <- 600

res <- data.frame()
set.seed(202)
for (rel in rel_levels) {
  varu <- varx * (1 - rel) / rel
  bn <- numeric(reps); bc <- numeric(reps)
  for (k in seq_len(reps)) {
    x <- rnorm(n, 10, sdx2)
    y <- b0 + b1 * x + rnorm(n, 0, sde)
    w <- x + rnorm(n, 0, sqrt(varu))
    bn[k] <- coef(lm(y ~ w))[2]
    lam   <- (var(w) - varu) / var(w)
    bc[k] <- bn[k] / lam
  }
  res <- rbind(res, data.frame(rel,
    rmse_naive = sqrt(mean((bn - b1)^2)),
    rmse_corr  = sqrt(mean((bc - b1)^2))))
}

## ---- worth-numbers ----
r_lo <- res[res$rel == 0.5, ]; r_hi <- res[res$rel == 0.99, ]

## ---- fig-worth ----
d1 <- pivot_longer(res, c(rmse_naive, rmse_corr),
                   names_to = "estimator", values_to = "rmse")
d1$estimator <- factor(d1$estimator, levels = c("rmse_naive", "rmse_corr"),
                       labels = c("naive (biased)", "corrected"))
ggplot(d1, aes(rel, rmse, colour = estimator)) +
  geom_line(linewidth = 1.1) + geom_point(size = 2.4) +
  scale_colour_manual(values = c("naive (biased)" = col_naive, "corrected" = col_corr),
                      name = NULL) +
  labs(title = "When is correcting worth it?",
       x = "reliability of the predictor",
       y = "root mean squared error of the slope") +
  theme_te

## ---- sensitivity ----
set.seed(551)
rel_true  <- 0.7
varu_true <- varx * (1 - rel_true) / rel_true
x <- rnorm(n, 10, sdx2)
y <- b0 + b1 * x + rnorm(n, 0, sde)
w <- x + rnorm(n, 0, sqrt(varu_true))

b_naive <- coef(lm(y ~ w))[2]
assumed_rel <- seq(0.5, 1.0, by = 0.05)
corrected   <- b_naive / assumed_rel        # correction under each assumed reliability

## ---- fig-sensitivity ----
sens <- data.frame(assumed_rel, corrected)
ggplot(sens, aes(assumed_rel, corrected)) +
  geom_hline(yintercept = b1, colour = col_true, linewidth = 0.5, linetype = "22") +
  geom_line(colour = col_acc, linewidth = 1.2) +
  geom_point(colour = col_acc, size = 2.4) +
  annotate("point", x = 1.0, y = b_naive, colour = "#16241d", size = 3) +
  annotate("point", x = rel_true, y = b_naive / rel_true, colour = col_true, size = 3.2) +
  annotate("text", x = 0.99, y = b_naive, label = "naive\n(assume no error)",
           hjust = 1, vjust = 1.3, size = 3.3, lineheight = 0.9) +
  annotate("text", x = rel_true, y = b_naive / rel_true, label = "true reliability",
           hjust = -0.08, vjust = -1.3, size = 3.3, colour = col_true) +
  scale_y_continuous(expand = expansion(mult = c(0.12, 0.05))) +
  labs(title = "Sensitivity when the error variance is a guess",
       x = "assumed reliability", y = "corrected slope") +
  theme_te
