Longitudinal analysis: consolidate and explain

Week 11 · Optional after-class review

Gavin Simpson

Aarhus University

Wednesday, 18 November 2026

Read the same calf study

Researchers assign Control or Enhanced feed to each of 32 calves, balancing the two diets within each of four rooms. They weigh each calf at weeks 0, 2, 4, 6 and 8. These invented records illustrate longitudinal data: measurements collected from the same animals over time.

library("dplyr")

library("lme4")
library("performance")
library("marginaleffects", warn.conflicts = FALSE)
library("ggplot2")
d <- readr::read_csv("data/calf-growth.csv")
d <- d |>
  mutate(
    diet = factor(diet, levels = c("Control", "Enhanced")),
    room = factor(room),
    calf = factor(calf),
    week = time_week
  )
head(d |> select("calf", "diet", "week", "weight_kg"), 3)

week counts weeks since researchers assigned the diets. They record week 0 before feeding starts.

Read the same calf study

# A tibble: 3 × 4
  calf  diet     week weight_kg
  <fct> <fct>   <dbl>     <dbl>
1 C01   Control     0      47.9
2 C01   Control     2      58.6
3 C01   Control     4      65.8

Choose what to review

Work Minutes
Review this deck and recap 20
Revisit code and supplied extension 20–25
Revise project methods and result 20–30

Choose the steps that help with your Unit 12 draft for 25 November.

Fit the curve directly

curved <- lmer(
  weight_kg ~ diet * week + I(week^2) + room + (1 | calf),
  data = d,
  REML = TRUE
)
summary(curved)

Explain what changes when I(week^2) is added. Which parts of the model still connect repeated weights?

Fit the curve directly

Linear mixed model fit by REML ['lmerMod']
Formula: weight_kg ~ diet * week + I(week^2) + room + (1 | calf)
   Data: d

REML criterion at convergence: 725.7

Scaled residuals: 
     Min       1Q   Median       3Q      Max 
-2.27062 -0.60674  0.06788  0.62813  2.19214 

Random effects:
 Groups   Name        Variance Std.Dev.
 calf     (Intercept) 29.084   5.393   
 Residual              2.595   1.611   
Number of obs: 160, groups:  calf, 32

Fixed effects:
                  Estimate Std. Error t value
(Intercept)       38.39388    2.17105  17.684
dietEnhanced       0.20787    1.95706   0.106
week               5.38766    0.16500  32.652
I(week^2)         -0.10708    0.01903  -5.628
roomR2             0.05862    2.72041   0.022
roomR3             2.40960    2.72041   0.886
roomR4             4.03347    2.72041   1.483
dietEnhanced:week  0.42749    0.09005   4.747

Correlation of Fixed Effects:
            (Intr) dtEnhn week   I(w^2) roomR2 roomR3 roomR4
dietEnhancd -0.451                                          
week        -0.110  0.050                                   
I(week^2)    0.070  0.000 -0.923                            
roomR2      -0.627  0.000  0.000  0.000                     
roomR3      -0.627  0.000  0.000  0.000  0.500              
roomR4      -0.627  0.000  0.000  0.000  0.500  0.500       
dtEnhncd:wk  0.083 -0.184 -0.273  0.000  0.000  0.000  0.000

Choose weeks and rooms

Compare the diets at weeks 0, 4 and 8, giving each room equal weight.

grid <- tidyr::crossing(
  week = c(0, 4, 8),
  diet = d$diet,
  room = d$room
) |>
  mutate(calf = d$calf[1])
head(grid, 3)
# A tibble: 3 × 4
   week diet    room  calf 
  <dbl> <fct>   <fct> <fct>
1     0 Control R1    C01  
2     0 Control R2    C01  
3     0 Control R3    C01  

Compare the three weeks

Use the same fitted curve. Leave out an existing calf’s fitted difference with re.form = NA and compare Enhanced minus Control.

differences <- avg_comparisons(
  curved,
  newdata = grid,
  variables = list(diet = c("Control", "Enhanced")),
  by = "week",
  re.form = NA
)
differences

Researchers record week 0 before feeding starts. Which row describes chance starting differences, and which answers the eight-week feeding question?

Compare the three weeks


 week Estimate Std. Error     z Pr(>|z|)   S  2.5 % 97.5 %
    0    0.208       1.96 0.106   0.9154 0.1 -3.628   4.04
    4    1.918       1.92 0.997   0.3188 1.6 -1.852   5.69
    8    3.628       1.96 1.854   0.0638 4.0 -0.208   7.46

Term: diet
Type: response
Comparison: Enhanced - Control

Keep the uncertainty target clear

The intervals concern differences in expected weight, in kg.

They do not describe individual future weights.

Our question weights rooms equally and averages over new calves under the model.

Even if errors at nearby visits have near-zero sample correlation, that does not prove a calf’s remaining errors are unrelated.

Use checks to ask better questions

check_singularity(curved)
[1] FALSE
check_normality(curved)
OK: residuals appear as normally distributed (p = 0.268).

Read the supplied visit-correlation extension in reference/extension-output.txt.

What assumption changed? Did the question or units change?

Revise your project

Write 150–200 words. Explain what researchers did to the animals or groups and what they measured. Give one useful estimate with its uncertainty, one model check, an alternative assumption worth comparing and a limitation.

Use this optional review to refine the same project draft you will bring on 25 November.

Use lme4, performance and marginaleffects directly. Choose a model and comparison that fit your own study.