# Three-level hierarchical regression using lmer

I would like to fit a 3-level hierarchical regression in lmer, however, I don't know how to specify the grouping factor above the second level. the model would be:

lmer(depedent ~ independent 1 + independent2 + (1|group1)....


And I would like to specify another group nested within group1.

I've tried (1|group1/group2) but this gives an error message and group1:group2 is an interaction.

I've also tried separately (1|group1) + (1|group2) but i'm not sure if this is correct.
thanks

-

Not enough reputation to comment, so I'll post this as an answer. There are a number of questions like this already around. you might want to look at this message.

However, (1|group1/group2) should work with all but very old versions of lme4, so if that gives you an error, there is probably something wrong with the way you set up your data. Note that once your data are correctly set up, (1|group1/group2) and (1|group1) + (1|group2) should give the same results.

-
dl.dropbox.com/u/22681355/dataset.csv this is the dataset, i'm trying to use choicenum and ipnum as grouping factors. the error I get is: > model <-lmer(data$ene ~ data$videocond + data$ifrelevant + data$videorelevant + data$choicenum + (1 | data$choicenum/data$ipnum), REML=TRUE) Error in data:$ : NA/NaN argument In addition: Warning message: In data:$ : numerical expression has 12 elements: only the first used > – DBR Jul 7 '11 at 17:14 your syntax is not correct. you should go with: model <- lmer(ene ~ videocond + ... + (1|choicenum/ipnum), data=data); look at the help pages. also you should not really use a name like "data" which is also an R keyword – wolf.rauch Jul 7 '11 at 17:41 ok, now I really looked at your data. The above still applies: in lme*, DO NOT address variables by data$var. Also from your data it seems that choicenum is nested within ipnum and not the other way around (i.e. for every ipnum there are several choicenums). Definitely you have to read some more basic information on lme*, like the Gelman & Hill book or similar. – wolf.rauch Jul 7 '11 at 18:13
thanks for the points. I normally don't use the data$var format, but without it lmer doesn't recognize the variables for some reason. Also i'll change the name from data thanks for the suggestion. – DBR Jul 7 '11 at 18:25 ive changed the name to dataset, now i don't need the data$ thanks! i've tried fitting the model you wrote above, however its been processing it for 10 minutes. is that normal? – DBR Jul 7 '11 at 19:04

Based on your dataset and the above comments as well as your other post on run time problem with lmer, you'll need to specify that choicenum and ipnum are factors or lmer will treat them as covariates. This is probably what was causing your error message that group1:group2 is an interaction. I ran the model on your dataset as described and it worked fine.

dataset$choicenum <- as.factor(dataset$choicenum)
dataset$ipnum <- as.factor(dataset$ipnum)
mymodel <- lmer(ene ~ videocond + choicenum + (1|ipnum/choicenum),data=dataset)

-
 thank you very much for your help. So the syntax you pased treats ipnum as nested within choicenum or the other way round? I ran the code you sent and I don't understand the following: in the fixed effects section it includes choicenum2, choicenum3, choicenum4,choicenum5, choicenum6 as variables, and in the random effects instead of just having ipnum and choicenum it has ipnum and ipnum:choicenum interaction. Did you get the same? – DBR Jul 9 '11 at 20:56 In my syntax, the random effects can be read as ipnum and choicenum within ipnum. Also, I've made changes to the model so that choicenum is added as fixed factor which should give the same results. – Jim M. Jul 9 '11 at 22:18 @ Jim M. Does it make sense to include the grouping variable (choicenum) as a random and a fixed effect? – Bernd Weiss Jul 9 '11 at 22:55 @Bernd Although I don't know the entire details of the study, since there are 6 choices in choicenum I would consider that a fixed effect, and inclusion as a random effect is testing for the interaction between choicenum and ipnum. – Jim M. Jul 9 '11 at 23:15 @Jim M. @Bernd But I would like to have choicenum nested within ipnum as a 3 level model, i'm not interested in the interactions. I'm trying to replicate the model that I've fitted in stata, where its specified as: xtmixed ene videocond ifrelevant videorelevant relcountcen relcountifrel videorelcount videorelcountifrel choicenum || ipnum: – DBR Jul 10 '11 at 3:15