# Detecting Random & Crossed Random-Effects from Model Syntax in R (lme4)

I'm following-up on this question, and inspired by this great answer. I have 3 lme4 longitudinal mixed-models. Throughout, y is the response variable, group is a binary indicator for "control" vs. "treatment", therapist (a clustering indicator), subjects (a clustering indicator), and time is the measurement time indicator (e.g., 0, 1, 2, 3).

Question: Suppose each model syntax below, as well as the data structure used to fit them, precisely represent the study design by a researcher, THEN: In each model, can we determine which random effects are taken as NESTED and which one(s) as CROSSED?

# FIRST:
lmer(y ~ time * group +
(time | therapist:subjects) +
(time * group || therapist),
data = data)

# SECOND:
lmer(y ~ time * group +
(time | therapist:subjects) +
(time | therapist) +
(0 + group + time:group | therapist),
data = data)

# THIRD:
lmer(y ~ time * group +
(1 | therapist:subjects) +
(0 + time | therapist:subjects) +
(0 + time:group | therapist) +
(0 + group | therapist),
data = data)

• I'm not sure what ou are asking. Are you asking about determining whether factos are nested or crossed, from the model formulae alone ? Dec 2, 2020 at 10:49
• @RobertLong, Rob, yes, my question is especially inspired by This (for Model 1 above), This (for Model 2 above), and This (for Model 3 above). The linked blogpost frequently justifies the use of the model syntax by the fact that therapist and subject effects are crossed or nested . . . Dec 3, 2020 at 1:34
• @RobertLong, . . . So, I'm asking how could one understand the nature (i.e., crossed vs. nested) such random-effects by examining the model syntax used to fit them. Dec 3, 2020 at 1:35

Question: Suppose each model syntax below, as well as the data structure used to fit them, precisely represent the study design by a researcher, THEN: In each model, can we determine which random effects are taken as NESTED and which one(s) as CROSSED?

So, I'm asking how could one understand the nature (i.e., crossed vs. nested) such random-effects by examining the model syntax used to fit them.

No. There is no way to determine whether factors are nested by inspecting the formula. This is because nesting and crossing are properties of the study design, not the model.

If we use:

(1 | therapist) + (1 | therapist:subject)


then the intention is often to specify that subject is nested in therapist.

However, if the reality of the study design is that subject is crossed with therapist, then this model would be incorrect. The model cannot tell us whether the factors or nested or crossed.

If we write:

(1 | therapist) + (1 | subject)


then there can be ambiguity because the results will depend on how the data are encoded. There are two scenarios:

• the factors are actually crossed

• the factors are actually nested

In the former case then (1 | therapist) + (1 | subject) is correct. We can of course still fit (1 | therapist) + (1 | therapist:subject) however this would be wrong. Note that the model formula cannot tell us whether the factors are nested or crossed.

In the latter case, it depends on how the data are encoded. For example, suppose we have 3 therapists, each of whom sees 3 subjects, and each subject is unique. So there are 9 unique subjects. If these are coded like this:

     subject therapist
1       1         1
2       2         1
3       3         1
4       1         2
5       2         2
6       3         2
7       1         3
8       2         3
9       3         3


here subject is encoded such thateach therapist "shares" the same subject ID numbers, even though they are different individuals. So here we can see that even looking at the data, there is no way to know whether these factors are crossed or nested - the only way to know is with reference to the study design. Obviously it follows that if the data cannot tell us whether the factors are crossed or nested, then the model formulae also cannot. In this case (1 | therapist) + (1 | subject) is incorrect, and (1 | therapist) + (1 | therapist:subject) should be used.

If subject is coded like this:

     subject therapist
1       1         1
2       2         1
3       3         1
4       4         2
5       5         2
6       6         2
7       7         3
8       8         3
9       9         3


then (1 | therapist) + (1 | subject) and (1 | therapist) + (1 | therapist:subject) will yield exactly the same results. Again, the model formulae cannot inform us about whether the factors are nested or crossed.

• Thank you Rob, but notice that the question intentionally is saying that assume that model syntax and the data "precisely represent the study design by a researcher". That is, there is no ambiguity whether the model syntax matches the data structure or the study design or not. Also, the linked blog post clearly shows the study design for each model syntax. So at its core, the question is how the random part of each model syntax can support is corresponding study design. In another question I will directly share the design and model syntax and ask how the syntax supports the design. --Thanks.
– Reza
Dec 3, 2020 at 15:40
• @Reza Sorry but I don't follow you. The question specifically asks about infering whether factors are nested or crossed, from the model formula. A model cannot inform the modeller about the nesting or crossing of factors. If you already know whether the data are crossed or nested, then all that is needed is to specify the random structure appropriately. Alternatively the wrong structure can be specified, and invalid inferences potentially obtained. However the formula will not provide any insight about the study design. Dec 3, 2020 at 15:51
• Dear Robert, HERE is a question that might be of interest to you. Feb 4, 2021 at 5:40