# How to specify random effects in lme?

I have searched for this online for hours but none of online posts is what I am looking for. My question is very easy to implement in SAS Proc mixed procedure but I am not sure how to do it in lme and/or lmer packages. Assume, I have a model, $y = \mu + \alpha + \beta +\alpha\beta + e$, where $\alpha$ is fixed but $\beta$ and $\alpha\beta$ are random. My R code is

 f1 = lme(y ~ factor(a), data = mydata,
random = list(factor(b) = ~ 1, factor(a):factor(b) = ~ 1))


Error: unexpected = in:

 f1 = lme(y ~ factor(a), data = mydata,
random = list(factor(a) =


Could someone please tell me how to specify these random effects in lme? many thanks in advance

• It helps to use dput to get the code needed to recreate your data. From the comment you left, the result is structure(list(method = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor"), day = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"), level = c(142.3, 144, 134.9, 146.3, 148.6, 156.5, 152, 151.4, 142.9, 147.4, 125.9, 127.6, 135.5, 138.9, 142.9, 142.3)), .Names = c("method", "day", "level"), row.names = c(NA, -16L), class = "data.frame") May 17, 2011 at 20:11
• it's a really old post but if someone else like me end up here he/she might like this one too rpsychologist.com/… Sep 17, 2020 at 12:31

Try this, it's a standard way to do a split plot. The notation / means that method is nested in day.

lme(level~method, random=~1|day/method, data=d)

• Hi Aaron, Thank you very much. Your R output is exactly the same with SAS output and the textbook. But Why we have to use "nested" in R syntax. Because in the textbook, it clearly states that $\alpha\beta$ is an random interaction term and I also use random interaction term in SAS. Could you please tell me why/how to specify an random interaction effect in lme, if it is possible? many many thanks Tu.2
– Tu.2
May 17, 2011 at 21:19
• Your question isn't about R syntax, it's about what nesting means. Nesting B in A (with A/B) creates two variables, A and the interaction between A and B, which is exactly what you describe. May 18, 2011 at 14:40
• Hi, This is a great explanation. Thank you very much.
– Tu.2
May 18, 2011 at 17:03

It would help a lot if you provided a data.frame. Now it is not clear what is a grouping factor. I judge that it is $\beta$. Then in lme notation your model should be written as follows:

lme(y~a,random=~a|b, data=mydata)

• Hi mpiktas, thank you for your response but the R result is very different from SAS proc mixed procedure. The SAS output is the same with the answer in a textbook(Kuehk RO. 1999. Design of experiments: statistical principles of research design and analysis, 2nd edition). The dataset is method day level 1 1 142.3 1 1 144.0 1 2 134.9 1 2 146.3 1 3 148.6 1 3 156.5 1 4 152.0 1 4 151.4 2 1 142.9 2 1 147.4 2 2 125.9 2 2 127.6 2 3 135.5 2 3 138.9 2 4 142.9 2 4 142.3
– Tu.2
May 17, 2011 at 19:23