# How to fit the model for crossed and nested design using lme function in R?

Suppose A is a fixed factor and B is a random factor. A and B are crossed. Another random factor C is nested in B. Taking into account the AB and AC interactions, how should I fit the model using lme function in the nlme package? Thanks.

-
You mention the lme package on the title, and nlme on your question. Which is the correct package? –  João Daniel Apr 22 '12 at 22:07
AND .... where's the code? –  DWin Apr 22 '12 at 23:22
I said the lme function in the nlme package. lme is not a package. And I'm just wondering how to specify the arguments in this function for my problem. –  Knightgu Apr 26 '12 at 19:59
Have you tried lmer() in the lme4 package? See e.g. this book chapter –  guest May 7 '12 at 0:05
Thank you so much. Yes, the lmer() function can deal with crossed random effects easily. That is why the lme4 package is a big improvement for the nlme package. I realized later that it was really hard to use lme() function to fit crossed random effects. Since we've got the newer package, why not use it？ –  Knightgu May 16 '12 at 22:27

## migrated from stackoverflow.comMay 6 '12 at 18:22

This question came from our site for professional and enthusiast programmers.

The tricky part here is to specify the right formula.

library(nlme)
# Assume y is your response variable.
lme(fixed = y ~ A, random = ~ A | B/C)


Here B/C stands for C nested in B and is short for B + B:C. The interaction with A is present because A is repeated in the random part.

For a basic introduction to nlme, I recommend this document.

-
Thank you very much for your solution. I think you are right. I will use a practical example to check it. By the way, really nice reference. Thank you. –  Knightgu Jun 4 '12 at 16:27
I have tried the formula you mentioned using R. Unfortunately, the data set I used did not justify the formula. The lme() function applied simply did not converge to a numerical solution. Seems that the lmer() function in the lme4 package does the right thing. –  Knightgu Jun 15 '12 at 21:34