library("lme4")
library("performance")
library("marginaleffects")
x <- readr::read_csv("data/cow-crossover.csv")
head(x, 4)Class 10: a complete crossover analysis
Aarhus University
Wednesday, 11 November 2026
13:55–14:05. In the synthetic study, 24 cows were assigned to AB or BA. Every cow was measured under both rations.
The question is: how much does mean intake differ under B compared with A, in kg/day?
We assume the earlier ration no longer affects intake when the next outcome is recorded. The outcome file cannot establish this assumption.
Each row is a cow’s average intake over the final three days of one treatment period.
# A tibble: 4 × 5
cow sequence period treatment intake_kg_day
<chr> <chr> <dbl> <chr> <dbl>
1 W01 AB 1 A 21.6
2 W01 AB 2 B 22.7
3 W02 BA 1 B 22.5
4 W02 BA 2 A 20.0
# A tibble: 2 × 2
sequence n
<fct> <int>
1 AB 12
2 BA 12
Twelve cows were randomly assigned to each order. Each order must remain in the data even when we rearrange the rows.
# A tibble: 4 × 3
period treatment n
<fct> <fct> <int>
1 1 A 12
2 1 B 12
3 2 A 12
4 2 B 12
cows cow_period_rows
24 48
Researchers measured the same 24 cows under A and under B. Each cow’s two outcomes form a pair; the two treatment columns do not describe different sets of cows.
tidyr::pivot_wider() rearranges existing observations. It does not fit a model.
# A tibble: 3 × 3
cow intake_kg_day.A intake_kg_day.B
<fct> <dbl> <dbl>
1 W01 21.6 22.7
2 W02 20.0 22.5
3 W03 19.3 21.5
We can now compare A with B for the same cow. The full model will also allow for period and assigned order.
Above the dashed line means the cow ate more under B. Why might pairing help when usual intake differs between cows?
(1 | cow) allows cows’ usual differences in appetite.
period allows a common first-to-second-period change. sequence allows an average difference between the two assigned-order groups. That group difference may reflect chance differences among cows; it does not identify a lingering treatment effect.
Linear mixed model fit by REML ['lmerMod']
Formula: intake_kg_day ~ treatment + period + sequence + (1 | cow)
Data: x
REML criterion at convergence: 180.8
Scaled residuals:
Min 1Q Median 3Q Max
-1.70501 -0.37577 0.03845 0.41809 1.80745
Random effects:
Groups Name Variance Std.Dev.
cow (Intercept) 5.0542 2.2482
Residual 0.7033 0.8386
Number of obs: 48, groups: cow, 24
Fixed effects:
Estimate Std. Error t value
(Intercept) 22.4265 0.6927 32.377
treatmentB 1.0401 0.2421 4.296
period2 -0.7238 0.2421 -2.990
sequenceBA -2.2258 0.9492 -2.345
Correlation of Fixed Effects:
(Intr) trtmnB perid2
treatmentB -0.175
period2 -0.175 0.000
sequenceBA -0.685 0.000 0.000
The estimate is in kg/day. The interval describes uncertainty in the mean comparison, after allowing for period and assigned order.
re.form = NA sets each cow’s fitted intake difference to zero. Both periods and assigned orders receive equal weight in these complete study records.
Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
1.04 0.242 4.3 <0.001 15.8 0.566 1.51
Term: treatment
Type: response
Comparison: B - A
These are mean intakes, averaging over the periods and assigned orders in this balanced dataset. They are not ranges for an individual cow’s future intake.
treatment Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
A 21 0.49 42.8 <0.001 Inf 20 21.9
B 22 0.49 44.9 <0.001 Inf 21 23.0
Type: response
[1] TRUE
attr(,"gradient")
[1] 6.234418e-08
[1] FALSE
TRUE on the first line: fitting completed successfully.
FALSE on the second: the estimated variation in cows’ usual intakes did not collapse to zero.
Neither result establishes that the washout was adequate.
Look for a pattern or changing spread against fitted values. In the other panel, compare the points with the reference line.
Including sequence records and allows for the assigned order. It does not remove an earlier ration’s lingering influence.
With just two periods, we cannot reliably separate a treatment’s current effect from all possible lingering effects using these outcomes alone.
Failing to detect an order difference does not establish that carryover is absent.
The researcher wants to know whether ration B increases intake under conditions where earlier treatment effects have worn off.
In your group, write three sentences:
14:40–14:55; then break 14:55–15:10.
15:10–15:35. Use your own group’s project case.
Save the analysis plan before fitting:
A calf stays on one diet and is weighed five times. A cow receives both rations in two periods.
For each study, report: what was randomised, what a row means, and what links observations from the same animal.
What additional scientific assumption does the cow study require?
A colleague says: “There are 48 cow-period records, so the crossover has 48 separately chosen treatment orders.” Correct the claim.
Another says: “Adding sequence means washout no longer matters.” Explain the error.
After class: 60 minutes, including the calf change comparison and two growth-curve sketches for Class 11.