I'm looking to perform a nonlinear mixed effects regression.

Sample data:

data.frame("id" = 1:2, 
           "intervention" = c(rep("a",9),rep("b", 9)),
           "area" = 1:3,
           "dv" = sample(18, replace=TRUE))
  • In the sample data there were 3 observations per area and per intervention but the actually data isn't balanced like that.
  • DV is continuous data
  • 2 subjects with repeated observations

I know from R Companion that I can use the following code to nest 1 level


model = lme(dv ~ intervention, random=~1|id,

How do I nest a second level to also account for area?

  1. lme fits linear mixed models, not non-linear ones. For certain types of non-linear mixed models, you can use nlme.

  2. If area is nested in the levels of id, you can use the code

model = lme(dv ~ intervention, random=~1|id/area,

to fit the nested mixed effects model.


If you just specify random = ~ 1|id, you fit the single-level mixed model, which corresponds to the regression equation

$$ \textrm{dv}_{ik} = \beta_0 + \beta_1 \textbf{1}_{\textrm{intervention}} + b_i + \epsilon_{ik} \\ b_i \sim \textrm{Normal} (0, \sigma_b^2) \\ \epsilon_{ik} \sim \textrm{Normal} (0, \sigma^2), $$

where $b_i$ are the random effects due to differences among patients.

Nesting further with random = ~1 | id/area, then the effect of the intervention is further conditional on the random area effects that vary within patients (id's). This is basically the same as conditioning effects on the interaction of area and id. The regression equation in the nested model is

$$ \textrm{dv}_{ijk} = \beta_0 + \beta_1 \textbf{1}_{\textrm{intervention}} + b_i + b_{ij} + \epsilon_{ijk} \\ b_i \sim \textrm{Normal} (0, \sigma_b^2) \\ b_{ij} \sim \textrm{Normal} (0, \sigma_c^2) \\ \epsilon_{ijk} \sim \textrm{Normal} (0, \sigma^2), $$

where $b_i$ is the same as above, but now we additionally have $b_{ij}$, which are random effects due to areas within patient. (Some may prefer to use the notation $b_{i(j)}$ to make it clear that the effects are not crossed).

If there is further nesting, then (I think) the syntax extends the way you would expect it to: random = ~ 1 | id/area/subarea, but we are now pushing the limits of my experience with nlme.

  • $\begingroup$ The intervention is applied multiple times within an area for a given subject. The intervention is applied across multiple areas. What's the logic behind the id/area? What if I wanted to take it a level deeper and there's a subarea? Thanks! $\endgroup$ – myfatson Jan 20 at 22:46
  • $\begingroup$ @myfatson check my edits and report back. $\endgroup$ – JTH Jan 21 at 1:42
  • $\begingroup$ Beautifully explained! What do you use to perform post-hoc testing on a double-nested model like this? $\endgroup$ – myfatson Jan 21 at 2:57
  • $\begingroup$ What would you like to know, post-hoc? The library emmeans has a lot of post-hoc testing capabilities, so does survival::yates. $\endgroup$ – JTH Jan 21 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.