# How to combine random effect and nested random effect with lme

I'm doing a mixed linear model. And I have subjects who have been select in 20 schools. So I want to take this to account.

For this, I want to put a random intercept for the "SCHOOL" variable and a random intercept and slope for my "SUBJECT". But I want to take into account the nested effect.

Does this code fit the nested model:

 lme(Y ~ T + X, random = list(SCHOOL = ~ 1, SUBJECT = ~ T), method = "ML")


T is the time, and Y the outcome.

This code specifies that SUBJECT is nested within SCHOOL, and that for SCHOOL you have random intercepts, and for SUBJECT random intercepts and random slopes.

You could also fit the model with restricted maximum likelihood that provides better estimates for the variance components in small samples.

• Thanks for your answer. I assume it's correct so. Small samples correspond to an effective of less of 100 ? My sample is constituate of about 600 subjects ... – Klmce Apr 11 at 19:24

I'm not sure how to do it in package NLME, but with LME4 you could do it with:

lmer(y ~ t + x + (1 + T|SUBJECT:SCHOOL), REML = FALSE)