Random effects: hierarchical thermal tolerance

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 3.6,
  dpi = 96
)

This vignette is for a thermal biologist whose survival assays are grouped: colonies, clutches, populations, tanks, or broods. It shows how to model between-group variation without spending a fixed coefficient on every group. That is exactly what a random intercept does: it treats the per-group deviations as draws from a Gaussian, estimates a single between-group standard deviation, and shrinks each group's estimate toward the overall mean (partial pooling). In freqTLS a random intercept is available on the thermal-tolerance midpoint CTmax, the thermal-sensitivity log_z, and the shape coordinates low and log_k. CTmax accepts multiple additive independent grouping terms, such as CTmax ~ 1 + (1 | plant) + (1 | meas_day).

library(freqTLS)

Fixed groups vs. a random intercept

With a handful of well-sampled groups you can give each its own fixed CTmax (CTmax ~ group, see vignette("freqTLS")). With many groups, or when you regard groups as a sample from a population, a random intercept CTmax ~ 1 + (1 | group) is the better tool: it costs one variance parameter instead of one coefficient per group, and the group estimates borrow strength from each other. The deviations b_g ~ N(0, sigma_CTmax^2) are integrated out by TMB's Laplace approximation; sigma_CTmax is estimated by marginal maximum likelihood. A formula without (1 | group) uses the ordinary fixed-effects objective. Adding (1 | group) is not a no-op: it adds and integrates a group-level Gaussian block, even when its fitted variance is near zero.

Crossed plant and measurement-day effects

For assays that sample plants across measurement days, the scientific question may be whether thermal tolerance varies among plants and whether some days shift all observations. This simulated design includes both crossed grouping columns and fits additive independent random intercepts. CTmax is the critical thermal maximum at reference time tref; z is thermal sensitivity, in degrees per decade of exposure duration. The model idea comes from the bayesTLS framework introduced by Noble, Arnold, Nakagawa and Pottier; this example shows the crossed random-intercept extension in freqTLS.

set.seed(81)
crossed <- expand.grid(
  plant = factor(sprintf("P%02d", 1:12)),
  meas_day = factor(sprintf("D%02d", 1:8)),
  temp = c(33, 35, 37, 39),
  duration = c(30, 60, 120, 240),
  KEEP.OUT.ATTRS = FALSE
)
crossed$ctmax_i <- 36 + rnorm(12, 0, 0.8)[crossed$plant] +
  rnorm(8, 0, 0.35)[crossed$meas_day]
mid <- log10(60) - (crossed$temp - crossed$ctmax_i) / 4
crossed$prob <- plogis(-5 * (log10(crossed$duration) - mid))
crossed$total <- 12L
crossed$survived <- rbinom(nrow(crossed), crossed$total, crossed$prob)

crossed_fit <- fit_tls(
  tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
         CTmax ~ 1 + (1 | plant) + (1 | meas_day)),
  data = crossed, family = "binomial", tref = 60, quiet = TRUE
)
crossed_fit$estimates[
  crossed_fit$estimates$parameter %in% c("CTmax", "sigma_CTmax:plant",
                                          "sigma_CTmax:meas_day"),
  c("parameter", "estimate", "std.error")
]

Population prediction sets both random intercepts to zero. Conditional prediction adds the fitted plant and day deviations, so newdata must contain both grouping columns:

new_crossed <- data.frame(
  temp = 36, duration = 60,
  plant = as.character(crossed$plant[1]),
  meas_day = as.character(crossed$meas_day[1])
)
data.frame(
  prediction = c("population", "plant and day conditional"),
  survival = c(
    predict(crossed_fit, new_crossed, type = "survival", re.form = "population"),
    predict(crossed_fit, new_crossed, type = "survival", re.form = "conditional")
  )
)

These terms assume independent plant and day deviations; they do not estimate a plant-by-day interaction or correlation between grouping factors. Real studies need enough independent plants and days to estimate both variance components.

A random intercept on CTmax

We simulate 14 colonies whose CTmax varies with a between-colony SD of 1.5 °C, then fit a single random intercept.

set.seed(1)
d <- simulate_tls(family = "binomial", CTmax = 36, z = 4,
                  re_sd = 1.5, n_re_groups = 14, seed = 1)
fit <- fit_tls(
  tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
         CTmax ~ 1 + (1 | colony)),
  data = d, family = "binomial", tref = 1, quiet = TRUE)

# The estimates table carries the population CTmax / z and the between-colony SD.
est <- fit$estimates
est[est$parameter %in% c("CTmax", "z", "sigma_CTmax"),
    c("parameter", "estimate", "std.error")]

sigma_CTmax is the estimated between-colony standard deviation of CTmax, in °C; it is directly interpretable on the thermal-tolerance scale. It is a maximum- likelihood variance component and can be weakly identified with few groups. The table reports results from the package's earlier recovery study. Repository maintainers can regenerate that study with the source-only data-raw/re-recovery-study.R script, which is not installed:

rr_path <- system.file("extdata", "re_recovery_results.rds", package = "freqTLS")
if (nzchar(rr_path) && file.exists(rr_path)) {
  rr <- readRDS(rr_path)
  knitr::kable(
    rr$re_recovery[, c("n_groups", "mean_sigma", "rel_bias", "CTmax_wald_coverage")],
    col.names = c("n groups", "mean sigma_CTmax", "rel. bias", "CTmax 95% coverage"),
    caption = sprintf(
      paste("Random-intercept recovery vs number of groups (true sigma_CTmax = %.1f;",
            "%d sims/cell). The ML SD and fixed-effect CTmax Wald coverage are",
            "shown for this simulation design."),
      rr$meta$truth_sigma, rr$meta$nsim),
    digits = 3)
}

The table describes one simulation design and the Wald interval. A separate binomial random-intercept comparison found substantial undercoverage at three groups across the evaluated methods. Treat variance estimates and interval limits cautiously with few groups. The sigma_CTmax estimate is the model's between-group standard deviation.

The conditional modes (BLUPs), each colony's shrunken deviation from the population CTmax, come from ranef():

head(ranef(fit), 4)

Prediction distinguishes the population curve from an observed colony's conditional curve. Population predictions set the random intercept to zero; conditional predictions add the fitted BLUP and require the grouping column in newdata:

colony_1 <- as.character(ranef(fit)$group[1])
new_colony <- data.frame(temp = 36, duration = 2, colony = colony_1)
data.frame(
  target = c("population", "colony"),
  survival = c(
    predict(fit, new_colony, re.form = "population"),
    predict(fit, new_colony, re.form = "conditional")
  )
)

Calling predict() without re.form on a random-effects fit warns and returns the population prediction. An unseen group cannot receive an estimated BLUP, so conditional prediction stops and points to re.form = "population".

confint() routes uncertainty by target. It gives a positive, log-scale Wald interval for sigma_CTmax. It profiles the population CTmax under the random effect, re-running the Laplace approximation at each grid point, so this is slower than a fixed-effects profile. If that fixed-effect profile does not close, the default fallback is Wald; request method = "bootstrap" to redraw the random effects and refit the full model instead.

suppressMessages(confint(fit, "sigma_CTmax", method = "wald"))[
  , c("parameter", "conf.low", "conf.high")]
suppressMessages(confint(fit, "CTmax", method = "profile", npoints = 8))[
  , c("parameter", "conf.low", "conf.high", "method")]

The Confidence Eye always uses Wald intervals for a random-effects fit, even when method = "profile" is requested, because a profile eye would re-run the Laplace approximation at every grid point for every row. Use confint(fit, method = "profile") for fixed-effect profiles or confint(fit, method = "bootstrap") for random-effects-aware bootstrap intervals. The eye remains a confidence-interval display, never a posterior.

suppressMessages(plot_confidence_eye(fit, parm = "CTmax"))

Random effects on sensitivity and curve shape

The same (1 | group) syntax works on log_z (thermal sensitivity) and on the shape coordinates low (the lower, long-exposure asymptote) and log_k (the steepness). One nuance matters: because z, low, and k are modelled on transformed scales, their variance components live on those scales: sigma_logz is a SD on log(z), sigma_low on logit(low), sigma_logk on log(k). Read sigma_logz as an approximate multiplicative spread on z: exp(sigma_logz) is the fold-change across groups; sigma_logz has no z-unit interpretation.

set.seed(2)
dz <- simulate_tls(family = "binomial", CTmax = 36, z = 4,
                   times = c(30, 60, 120, 240, 480), tref = 60,
                   re_sd_z = 0.3, n_re_groups = 14, seed = 2)
fit_z <- fit_tls(
  tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
         log_z ~ 1 + (1 | colony)),
  data = dz, family = "binomial", tref = 60, quiet = TRUE)
sg <- fit_z$estimates$estimate[fit_z$estimates$parameter == "sigma_logz"]
c(sigma_logz = round(sg, 3),
  approx_fold_spread_in_z = round(exp(sg), 3))

The upper asymptote up is the one shape with no random effect: under the disjoint-bounds parameterisation up = up_min + up_w * plogis(beta_up) it has its own coordinate beta_up, but the compiled objective carries no random-intercept term for up (and its profile path is not wired, which also leaves up with a delta-method interval). Put the random intercept on low or log_k instead.

Combining random effects, and what is out of scope

You can place random intercepts on more than one coordinate. When two or more share the same grouping factor, freqTLS fits them as independent variances (all cross-correlations forced to zero) and warns:

set.seed(3)
db <- simulate_tls(family = "binomial", CTmax = 36, z = 4,
                   times = c(30, 60, 120, 240, 480), tref = 60,
                   re_sd = 1.2, re_sd_z = 0.3, n_re_groups = 14, seed = 3)
# Both random intercepts on the same `colony` grouping. This fits, but emits a
# warning that the two variances are independent (no correlation term); we pass
# `quiet = TRUE` to silence it here and discuss it below.
fit_both <- fit_tls(
  tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
         CTmax ~ 1 + (1 | colony), log_z ~ 1 + (1 | colony)),
  data = db, family = "binomial", tref = 60, quiet = TRUE)
fit_both$estimates$parameter[grepl("^sigma", fit_both$estimates$parameter)]

In real data the group-level CTmax and z deviations are usually correlated; by forcing that correlation to zero, the independent-variance fit absorbs it into the marginal SDs and the fixed-effect intervals. If you need a correlated random effect with estimated covariance, nested grouping syntax, or a random slope, use bayesTLS, whose Stan back end fits the covariance structure. freqTLS supports additive independent plant/day intercepts on CTmax, but it does not estimate their covariance. It supports one intercept on each of log_z, low, and log_k.

The likelihood view of shrinkage

A Gaussian random effect is a distributional model for the latent group deviations, not a prior placed on the reported population parameters. Its conditional modes (BLUPs) have an empirical-Bayes interpretation and shrink toward zero. The fixed effects and variance components are estimated by marginal maximum likelihood, and their intervals are profile-likelihood intervals for fixed-effect coordinates, log-scale Wald intervals for variance components, or parametric-bootstrap intervals when explicitly requested; they are not credible intervals. That is the same complementary stance the rest of freqTLS takes toward bayesTLS (see vignette("frequentist-and-bayesian")): the random effect buys you partial pooling without committing to priors on the parameters you report.



Try the freqTLS package in your browser

Any scripts or data that you put into this service are public.

freqTLS documentation built on Dec. 4, 2026, 5:11 p.m.