Prepare data: set the comparison plan before fitting
Question. Which records belong to the same individual and occasion, and which trait measurements are available to fit the model?
Data checks that precede a formula
Verify that every repeated record has a stable individual identifier and an occasion identifier.
Confirm trait units, direction, transformations, and impossible values from the original data documentation.
Count observed values by trait and by individual. A covariance cannot be repaired by a clever formula when repeats are absent.
Record missing responses and distinguish them from a biological zero, a detection limit, or a censoring bound.
Check whether traits are redundant measurements of the same quantity. Redundancy can manufacture a dramatic latent axis.
Start from documented inputs
The data notes give the sources and checksums. Use the run instructions to prepare the two datasets. The scripts record aggregate counts and save fit-ready objects in analysis/cache/tutorial-data/.
Example
Source records
Analysis records
Repeated structure
Responses used
Arctic charr
2,370 trials
2,370 trials; 21,278 observed values
1,185 fish; two trials each
Nine behaviours selected for the teaching analysis; see the prepared trait list
Dogs
900 annual records
900 records; 9,000 scores
100 dogs; nine years each
Ten scores with fewer than half their responses at zero
Start with two trials from one fish. The same fish number can occur in different forms, so combine form and fish number before using trial to mark a repeat. If a trait is missing on a trial, the other recorded traits can still contribute to the fit. The red-knot extension gives a second example where the occasion is a study period.
The comparison plan
Hold fixed
Why it matters
Analysis rows and trait order
A score change cannot then be caused by a different dataset.
Individual, observation, and cluster identifiers
The covariance levels retain the same meaning.
Fixed-effect design and contrasts
A rank change does not silently change the mean model.
Likelihood criterion
Use ML for comparing fixed effects or covariance ranks; reserve REML for a final Gaussian estimate only when the fixed design is fixed.
Residual convention
State whether residual variance is trait-specific, shared, modelled separately, or absorbed by an observation-level component.
Prepare the records you will analyse
The nine-trait charr example is the main route, so begin by opening its workbook and identifying one fish’s two trials. The Morph and Indiv columns together identify the fish; Replicate identifies the trial.
# A tibble: 6 × 6
Morph Indiv Replicate Dtot Exit.time Ang.vel
<chr> <dbl> <chr> <dbl> <dbl> <chr>
1 LB 5986768 A 365. 33.8 359.92099999999999
2 LB 3586774 A 105. 560. 230.25299999999999
3 LB 3720093 A 153. 15.3 201.83199999999999
4 LB 3588180 A 1324. 23.9 658.36900000000003
5 LB 6502792 A 39.3 14.0 627.12199999999996
6 LB 3831260 A 2516. 13.8 133.22999999999999
The first two rows need not belong to the same fish. I combine the fish form and number to make an identifier, then choose one fish and put its trials in order:
# A tibble: 2 × 5
Morph Indiv Replicate Dtot Exit.time
<chr> <dbl> <chr> <dbl> <dbl>
1 LB 5986768 A 365. 33.8
2 LB 5986768 B 189. 62.0
These rows are the two trials from that fish. Next, I keep the columns needed for our example and give them shorter names:
one_fish_example <- one_fish[, c("Replicate", "Dtot", "Exit.time")]names(one_fish_example) <-c("Trial", "Distance (cm)", "Exit time (s)")knitr::kable( one_fish_example,row.names =FALSE,caption ="Distance and exit time in two trials from one fish.")
Distance and exit time in two trials from one fish.
Trial
Distance (cm)
Exit time (s)
A
364.773
33.78
B
188.520
61.98
Some cells in Ang.vel are stored as the text NA; the preparation script converts these to missing values and also marks angular velocity as missing when total distance is zero. It leaves other observed traits from those trials in the data. It sets two negative border-duration records to missing, as documented in the source audit. It never turns missing responses into zeros.
The dog file retains all 14 supplied scores. The main lesson selects ten scores with fewer than half of their responses exactly zero. This keeps the teaching example readable; it does not imply that the four omitted scores lack biological value.
The red-knot preparation retains six traits and the full response mask. It logit-transforms the three proportions before standardising them. Activity and feeding are observed only in year one. That extension uses an intercept-only mean model; the period grouping does not imply simultaneous measurement.
The preparation scripts are the authoritative route for each example. The short code in a lesson explains the key decision; the scripts preserve the full input checks, response masks, transformations, and checksums.
TipMinimum learning path
Before fitting, write the biological question, list the units and identifiers, and complete the comparison plan above. Keeping those decisions visible makes it easier to tell whether a rank comparison changed the model or the data.
A long-format target
For a trait-level glmmTMB analysis, aim for one row per individual × occasion × trait, with columns such as value, trait, individual, occasion, and predictors. For a wide gllvmTMB workflow, one row may be an individual × occasion record with multiple trait columns; for long input it uses a trait column and explicit unit/unit_obs identifiers.
TipApply these checks to your own data
If you want to try these checks on your own data now, go to Use your data.
The downloads page contains the preparation scripts used for these examples. Do not substitute historical top-level scripts: they were exploratory work, not the analysis shown here.