10-02 · Explain repeated and crossover evidence

A 60-minute follow-up using direct package calls, a calf change comparison, and crossover assumptions.

Plan 60 minutes

Consolidation slides Worksheet · Recap · Reference results

Spend 15 minutes revisiting the two designs, 20 minutes running or reading the calf change comparison below, and 25 minutes explaining both studies, sketching growth curves and updating your project’s next action. after-class-student.R runs from a fresh session, loading the required data and fitting the calf models explicitly. The complete runnable example below starts from the data.

Calves: a different question about the same observations

The in-class question concerned the week-8 diet gap. Now ask: how much did the Enhanced–Control gap change between baseline and week 8? The diets had not started at baseline. We need the last gap minus the first, with uncertainty that recognises that the same calves appear at both visits.

library("dplyr")

library("lme4")
library("marginaleffects")
d <- readr::read_csv("data/calf-growth.csv")
d <- d |>
  mutate(
    room = factor(room),
    calf = factor(calf),
    diet = factor(diet, levels = c("Control", "Enhanced")),
    time_factor = factor(time_week, levels = c(0, 2, 4, 6, 8))
  )
calf_fit <- lmer(
  weight_kg ~ room + diet * time_factor + (1 | calf),
  data = d,
  REML = TRUE
)
calf_gaps <- avg_comparisons(
  calf_fit,
  variables = "diet",
  by = "time_factor",
  re.form = NA
)
calf_gaps

 time_factor Estimate Std. Error      z Pr(>|z|)   S  2.5 % 97.5 %
           0    0.025       1.99 0.0126    0.990 0.0 -3.872   3.92
           2    0.471       1.99 0.2367    0.813 0.3 -3.427   4.37
           4    3.245       1.99 1.6320    0.103 3.3 -0.652   7.14
           6    2.626       1.99 1.3206    0.187 2.4 -1.271   6.52
           8    3.222       1.99 1.6204    0.105 3.2 -0.675   7.12

Term: diet
Type: response
Comparison: Enhanced - Control

First check that the rows run from week 0 to week 8. The five weights below subtract the baseline gap, ignore the middle three rows, and add the week-8 gap. hypotheses() calculates that combination and its uncertainty from the model.

stopifnot(
  identical(as.character(calf_gaps$time_factor), c("0", "2", "4", "6", "8"))
)
change_weights <- matrix(c(-1, 0, 0, 0, 1), ncol = 1)
change_gap <- hypotheses(calf_gaps, hypothesis = change_weights)
change_gap

 Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
      3.2      0.797 4.01   <0.001 14.0  1.64   4.76

Term: custom

The result is in kg. Explain why this difference in changes is not simply the week-8 gap. A calf that tends to be heavy at every visit carries much the same persistent difference into both weights; subtracting the weights removes that part. This can improve precision for change. It does not establish that nearby visits have no further shared influences.

Cows: explain the result with its assumption

Use reference/crossover-comparison.csv or your own crossover_gap result. State B minus A in kg/day with the approximate 95% interval. Explain why we allow for period and cows’ usual differences in appetite. State that 24 cows were randomly assigned to sequence, giving 48 paired cow-period records.

The no-important-carryover assumption needs scientific evidence about the treatment and outcome. Neither the sequence term, an unremarkable model check nor failing to detect an order difference establishes it. Explain why a lasting vaccine effect may make a simple two-period crossover unsuitable.

Bring to Class 11

Bring the completed worksheet, two plausible curves through the pattern of calf mean weights, and your group’s next project action. Keep curves within weeks 0–8 and say where extrapolation would need extra evidence. Class 11 uses the unchanged calf-growth CSV. No new dataset or extra preparation assignment is required.