11-02 · Longitudinal model recap

What researchers did

Researchers assign Control or Enhanced feed to each of 32 calves, with 16 calves receiving each diet. They balance diets within four rooms and weigh every calf at weeks 0, 2, 4, 6 and 8. Five weights from a calf describe its growth; they do not add more calves to the comparison. All records are invented for teaching.

Describe growth before interpreting coefficients

Treating visits as named groups gives a separate mean at each visit. Using numerical weeks instead chooses a relationship with elapsed time, here a straight line or a shared curve over weeks 0–8.

In diet * week, diet describes the starting difference, week the Control growth rate, and diet:week the additional Enhanced growth rate. The last term is an interaction: the diet difference changes with week. The expected week-8 difference combines the starting difference with eight times the extra weekly gain.

Adding I(week^2) allows both diets to share curvature. The week coefficient then gives Control’s rate at week 0; later rates change as the path bends. Do not extend this curve to week 16 without further evidence.

Recompute the model comparisons

Run these direct calls from the project root to compare expected Enhanced minus Control weight at week 8, in kg. The AIC comparisons use ML copies; the estimates and intervals use REML. The last fit is the optional residual-correlation extension.

library("dplyr")
library("lme4")
library("marginaleffects")
library("nlme")

# Use the same calves and categories as the in-class script.
d <- readr::read_csv("data/calf-growth.csv") |>
  mutate(
    diet = factor(diet, levels = c("Control", "Enhanced")),
    room = factor(room),
    calf = factor(calf),
    week = time_week,
    visit_index = match(week, c(0, 2, 4, 6, 8))
  )
linear <- lme4::lmer(
  weight_kg ~ diet * week + room + (1 | calf),
  data = d,
  REML = TRUE
)
curved <- lme4::lmer(
  weight_kg ~ diet * week + I(week^2) + room + (1 | calf),
  data = d,
  REML = TRUE
)
# Compare different fixed effects with ML copies; estimate with REML.
linear_ml <- update(linear, REML = FALSE)
curved_ml <- update(curved, REML = FALSE)
AIC(linear_ml, curved_ml)
          df      AIC
linear_ml  9 774.4759
curved_ml 10 747.5685
week8 <- tidyr::crossing(
  week = 8,
  diet = d$diet,
  room = d$room
) |>
  mutate(calf = d$calf[1])
avg_comparisons(
  linear,
  newdata = week8, variables = "diet", re.form = NA
)

 Estimate Std. Error    z Pr(>|z|)   S  2.5 % 97.5 %
     3.63       1.97 1.85   0.0649 3.9 -0.224   7.48

Term: diet
Type: response
Comparison: Enhanced - Control
avg_comparisons(
  curved,
  newdata = week8, variables = "diet", re.form = NA
)

 Estimate Std. Error    z Pr(>|z|)   S  2.5 % 97.5 %
     3.63       1.96 1.85   0.0638 4.0 -0.208   7.46

Term: diet
Type: response
Comparison: Enhanced - Control
# Optional: lme4 cannot fit residual AR(1) correlation. nlme keeps the
# calf intercept and one correlated residual process for two-week visits.
ar1 <- nlme::lme(
  weight_kg ~ diet * week + I(week^2) + room,
  random = ~ 1 | calf,
  correlation = nlme::corAR1(form = ~ visit_index | calf),
  data = d,
  method = "REML"
)
ar1_ml <- update(ar1, method = "ML")
AIC(curved_ml, ar1_ml)
          df      AIC
curved_ml 10 747.5685
ar1_ml    11 740.5392
avg_comparisons(
  ar1,
  newdata = week8, variables = "diet", level = 0
)

 Estimate Std. Error    z Pr(>|z|)   S  2.5 % 97.5 %
     3.51       1.98 1.77   0.0762 3.7 -0.369   7.38

Term: diet
Type: response
Comparison: Enhanced - Control

The values use the same invented calf records. They depend on the chosen growth curve and on assumptions about how measurements from one calf are related. Do not choose a model because a treatment interval includes or excludes zero.

Explain expected weights and checks

Our question gives each of the four rooms equal weight and sets each calf’s fitted difference to zero. With this normal-outcome model, that also gives the expected weight averaged over the assumed distribution of new calves. An interval for this mean or a diet difference does not describe the range of future individual calf weights.

Errors are observed minus fitted weights, also called residuals. Plot them against week and fitted weight, and inspect their shape and spread. The calf term allows an animal to remain consistently heavier or lighter. It does not automatically allow different calf growth rates or extra relationships between errors at nearby visits.

In this particular draw, errors from neighbouring visits have near-zero sample correlation even though the data generating process includes a relationship between them. That reassuring plot cannot prove the errors are unrelated. The optional AR(1) model estimates a correlation of about 0.416 for errors at adjacent two-week visits after allowing for the mean and shared calf difference. The raw weight correlation also includes the persistent calf differences.

Offline recap · Workshop · Follow-up