From a Study Design to a Formula
An R model formula is a compact description of how a response is related to predictors and, in a mixed model, how observations are grouped. Translating a study into a formula is therefore a design task: first identify what each row represents, which predictors belong in the systematic part of the model, and which observations share a grouping unit. Then express those roles with formula operators.
A formula is not a complete GLMM specification. It does not, by itself, choose the outcome distribution, link function, estimation method, or diagnostic procedure. Those choices matter, but here the concern is the formula’s structure and whether that structure represents the study design.
For example, `outcome ~ treatment + age + (1 | clinic)` names `outcome` as the response, includes `treatment` and `age` as fixed-effect predictors, and includes a clinic-level random intercept. The formula assumes that the variables named in it exist in the analysis data in appropriate forms. A clinic identifier should identify clinics, for example, rather than contain a different value for every row.
Fixed-Effect Operators: `+`, `:`, and `*`
A plus sign includes terms in the model formula. Thus, `treatment + age` includes a treatment term and an age term. It does not say that treatment causes the outcome, and it is not ordinary arithmetic on the variables. In a standard formula with an intercept, the intercept is included unless it is explicitly removed.
The colon specifies an interaction term. For two predictors, `treatment:time` includes the interaction but not, by itself, the separate treatment and time terms. The asterisk is shorthand: `treatment * time` expands to `treatment + time + treatment:time`. This expansion is especially useful when the study question calls for both main effects and an interaction.
For categorical predictors, the actual model-matrix columns depend on how their levels are coded. With the usual treatment coding, the selected reference level determines the comparison represented by a coefficient. The formula states which terms are included; it does not make those coding choices irrelevant.
Worked Example: Additive Clinic-Level Model
Study design. Suppose a hypothetical study records whether each patient improved (0 = no, 1 = yes), the patient’s treatment group, baseline age in years, and clinic. There are four clinics with eight patients each, so the data contain \(4 \times 8 = 32\) patient rows. Each patient contributes one row, and treatment groups are represented within clinics.
Translate the design. The response is `improved`. Treatment and baseline age are patient-level predictors, and clinic is the grouping factor. If the intended structure allows clinics to differ in their underlying log odds of improvement, while using common treatment and age effects, write:
Formula: `improved ~ treatment + baseline_age + (1 | clinic)`
The `+` includes separate fixed terms for treatment and baseline age. The term `(1 | clinic)` supplies a clinic-specific random intercept. For instance, a row with `improved = 1`, `treatment = "active"`, `baseline_age = 60`, and `clinic = "C2"` contributes one observed response with those predictor and grouping values. The formula does not convert age into a categorical variable or alter the row; it uses the recorded age as a predictor.
The 32 rows are still patient observations, not four clinic averages. The clinic term represents their shared grouping, rather than replacing patient-level data with one row per clinic. Whether a particular response distribution and link are suitable is a separate model-specification decision.
Parenthesized Grouping Terms
In `(1 | clinic)`, the `1` denotes an intercept and the variable after the vertical bar identifies the grouping factor. In the usual GLMM interpretation, each clinic has a random intercept, commonly modeled as a draw from a mean-zero normal distribution with variance \(\tau^2\). This allows the clinic-specific linear predictors to depart from the population-level intercept.
A random slope allows the association for a predictor to vary across groups. For example, `(1 + time | person)` specifies a person-level random intercept and a person-level random slope for `time`. In the usual `lme4` syntax, this structure also estimates a covariance between those random effects. A time variable must vary within persons for person-specific time slopes to be informed by the data.
A short justification helps distinguish a random intercept from a fixed predictor. Write the linear predictor for observation \(i\) in group \(j\) as \(\eta_{ij} = \mathbf{x}_{ij}^{T}\boldsymbol{\beta} + b_j\), where \(b_j\) is that group’s random intercept. If the random intercepts have variance \(\tau^2\) and are independent across groups, then two observations in the same group share the same \(b_j\), while observations in different groups have independent random intercepts. Consequently,
Thus, the term encodes a shared group contribution to the linear predictor. In a GLMM, this is not generally the same as a constant covariance between observed outcomes: the relationship on the outcome scale also depends on the response distribution and link.
Worked Example: Repeated Measurements and a Random Slope
Study design. In a hypothetical study, 20 people are assigned to one of two treatment groups. Each person has measurements at five visits, coded as time values 0, 1, 2, 3, and 4. Thus, there are \(20 \times 5 = 100\) person-visit rows. Treatment is constant within a person, while time varies within each person.
Suppose the scientific specification calls for fixed treatment, time, and treatment-by-time terms, together with person-specific intercepts and time slopes. The formula is:
Formula: `response ~ treatment * time + (1 + time | person)`
The fixed part expands as `treatment + time + treatment:time`. The interaction allows the time association to differ between treatment groups. The random part allows people to have different starting levels and different time slopes, and estimates their intercept-slope covariance. Because each person contributes measurements at five distinct times, there is within-person time variation to support the slope structure; the data must still contain enough information to estimate it reliably.
Treatment does not need a person-specific random slope in this formula: it does not vary within person, so such a slope would not represent a person-specific treatment contrast from repeated observations. The formula encodes the stated structure; it does not establish that the structure will fit successfully or that all of its parameters will be estimable.
Nested and Crossed Grouping Structures
Grouping factors are nested when levels of one factor belong to only one level of another. For example, classrooms may each belong to one school. They are crossed when levels of the factors occur in combination across the data, such as when multiple patients are assessed by multiple clinicians and clinicians assess multiple patients.
R’s slash notation can express a nested structure: `(1 | school/classroom)` expands to `(1 | school) + (1 | school:classroom)`. The second term identifies classrooms within schools. An equivalent explicit version is often easier to audit. If classroom labels are reused across schools, the school-by-classroom combination matters: classroom “1” in one school is not the same grouping unit as classroom “1” in another.
For crossed grouping factors, separate terms are used, as in `(1 | patient) + (1 | clinician)`. Each term represents a different source of group-level variation. Whether a design is truly crossed or nested depends on which units appear with which other units in the observed data, not on the order in which the variables are written.
Worked Example: Classrooms Nested Within Schools
Study design. Suppose a hypothetical assessment records one binary result for each of 60 students in six schools. Each school contributes two classrooms, with five students per classroom: \(6 \times 2 \times 5 = 60\). Classroom labels 1 and 2 are reused in every school. The response is `passed`, and the predictor is a student-level program indicator.
If the intended structure includes school-level and classroom-within-school random intercepts, a formula is:
Formula: `passed ~ program + (1 | school/classroom)`
Its grouping terms expand to `(1 | school) + (1 | school:classroom)`. There are six school groups and twelve distinct school-classroom combinations because each of the six schools has two classrooms. The twelve combinations, not the two reused classroom labels alone, identify the classroom grouping units. The expanded formula makes both levels explicit and can be written as `passed ~ program + (1 | school) + (1 | school:classroom)`.
If the design instead had students assessed by several clinicians, with each clinician assessing students from multiple schools, clinician and school could be crossed rather than nested. In that case, the separate terms `(1 | school) + (1 | clinician)` would reflect the two grouping factors; replacing them with a nested term would encode a different design.
A Translation Workflow and Its Checks
A reliable translation starts from the analysis rows and proceeds from the response outward. The formula should match the observation unit and the actual identifier structure established during data preparation. Use this sequence to check the translation:
Choose the outcome recorded for each analysis row. Check that the variable represents the intended outcome and is encoded appropriately for the planned model.
For each predictor, decide whether it contributes a main effect and whether the scientific specification calls for interactions. Use `+` for separate terms, `:` for an interaction term, and `*` for both main effects and their interaction.
Determine which observations share a unit, whether groups are nested or crossed, and whether a predictor varies within the group where a random slope is proposed.
Check that every name exists in the analysis data, that identifiers distinguish the intended groups, and that the resulting terms match the design rather than merely the available column names.
The operators have different roles, so visual inspection is not enough. For instance, `treatment:time` alone is not a shortcut for `treatment * time`; it omits the main-effect terms. Conversely, `(1 + time | person)` is not a fixed treatment-by-time interaction. It describes varying person-level coefficients, while `treatment:time` describes a fixed interaction in the systematic part of the model.
Alternatives, Failure Modes, and Limits
| Design question | One formula choice | What it represents |
|---|---|---|
| Do groups differ in baseline level? | `(1 | group)` | A random intercept across groups |
| Does a predictor’s association vary by group? | `(1 + x | group)` | Group-specific intercepts and slopes for `x` |
| Are classrooms nested in schools? | `(1 | school) + (1 | school:classroom)` | School and classroom-within-school variation |
| Do two grouping factors cross? | `(1 | patient) + (1 | clinician)` | Separate variation for each grouping factor |
Treating a group identifier as a fixed predictor is an alternative to a random grouping term, but it represents a different structure: it estimates separate fixed contrasts for observed group levels rather than a shared distribution of group effects. That choice depends on the scientific target and design. It is not a mechanical substitute for a random effect.
Several checks can reveal a formula that does not match the study. A grouping variable with one unique value per row provides no repeated grouping information for a random intercept. A supposed nested identifier that is reused across parents can accidentally combine distinct groups if the formula does not include the parent identifier. A random slope for a predictor that does not vary within groups is generally unsupported by within-group comparisons. A complicated random-effects term can also be difficult to estimate when there are few groups or little variation in the predictor.
A common mistake is to read `treatment * time` as only an interaction, or to believe that `+` means “add these raw columns together.” The correction is to expand the formula terms: `treatment * time` means `treatment + time + treatment:time`, while `+` includes terms in the model specification. Another mistake is to assume that writing a plausible formula proves the model is appropriate. A formula is a design statement to audit; successful fitting and adequate diagnostics require additional checks.
Formula syntax also has limits. It cannot repair incorrect observation units, missing or misclassified identifiers, inadequate within-group variation, or a mismatch between the outcome and the intended response distribution. Nor does the formula alone specify family and link; those are separate parts of a GLMM specification.
Check Your Understanding
For each question, first identify the formula structure, then compare your reasoning with the solutions.
- A study includes outcome `event`, predictors `dose` and `visit`, and a random intercept for `site`. Write a formula that includes additive fixed effects only.
- What fixed-effect terms are included by `group * time`? How does that differ from `group:time`?
- In repeated observations on participants, time varies within participant. Write a formula with fixed treatment-by-time terms and participant-specific intercepts and time slopes.
- Six schools each have two classrooms, and classroom labels are reused in every school. Write explicit random-intercept terms for schools and classrooms within schools.
- Why is `(1 + treatment | person)` not automatically an appropriate way to represent treatment in a study where each person receives only one treatment?
Solutions. (1) `event ~ dose + visit + (1 | site)`. This includes separate fixed terms for dose and visit, and a site random intercept. (2) `group * time` expands to `group + time + group:time`; `group:time` includes the interaction term alone, not the two main-effect terms. (3) `outcome ~ treatment * time + (1 + time | participant)`. The fixed part includes both main effects and their interaction, while the random part gives participants varying intercepts and time slopes. (4) `(1 | school) + (1 | school:classroom)`. The school-classroom combination distinguishes classrooms with the same reused label in different schools. (5) Treatment does not vary within a person in this design, so the data do not provide within-person comparisons to support person-specific treatment slopes. The formula should reflect the actual treatment assignment and the intended estimable structure.