6
$\begingroup$

I am learning generalized linear mixed effects model, and today I realized that there are two ways to set random slope in lmer(), when dealing with data where same subjects are repeatedly measured.

fit1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
fit2 <- lmer(Reaction ~ Days + (1|Days:Subject), sleepstudy)

Looking at the results, it seems like lmer() doing different things. How each command differs from each other, and which should I use and when?

Update: Thanks to Matteo Lisi's answer and to the earlier post Difference between (factor|group) and (1|factor:group) specifications in lme4, I now understand the mechanism that the lme4 package is doing. However, I am still having difficulty understanding WHEN should I use WHICH. I would appreciate if you could explain for what types of research designs I should use which syntax.

  1. Is that something I can choose based on AIC? Or, I should choose the syntax based on the design of the research?
  2. Could you provide examples of research designs?
$\endgroup$
8
  • $\begingroup$ Near duplicates: stats.stackexchange.com/questions/302951 stats.stackexchange.com/questions/304374 $\endgroup$
    – amoeba
    Dec 28, 2017 at 13:56
  • $\begingroup$ I still think this is a duplicate (cc @gung). The accepted answer in the stats.stackexchange.com/questions/302951 provides some guidance as to when to use which model and links to a further resource discussing this in more detail. $\endgroup$
    – amoeba
    Dec 29, 2017 at 12:45
  • $\begingroup$ Thank you for pointing out and letting me know the similar answers, @amoeba. Because I am a novice R user and just have learned only the practical aspects of statistics: what to do for which type of analyses without understanding the statistical knowledge deeply. So, what's written in the duplicate makes sense, but, I cannot make up with specific examples of research designs by myself. I would like to revise my question to clarify my intention, if this web site is open for people who is looking for practical suggestions. $\endgroup$ Dec 30, 2017 at 19:06
  • $\begingroup$ I don't quite understand what you still don't understand here. The linked answer stats.stackexchange.com/a/302969/28666 provides guidance and a link for further reading. These two random effect specifications are suitable for THE SAME research design. But one model is more complicated than another. Re AIC, MatteoLisi uses it in his answer here. $\endgroup$
    – amoeba
    Jan 13, 2018 at 9:36
  • 1
    $\begingroup$ Let me try to elaborate (I have also made some edits to the answer in the linked thread; maybe they will help). You should distinguish a situation where Days is continuous vs. categorical. If it's continuous then the models are not really comparable, because fit2 treats it as categorical. But imagine Days were categorical. Then the main point is that fit2 is a restricted version of fit1. If you add (1|Days) to the model, then it's fit1 restricted to "compound symmetry". Whether you want to use a full model or a restricted model, depends primarily on the amount of data you have. $\endgroup$
    – amoeba
    Jan 17, 2018 at 13:02

1 Answer 1

6
$\begingroup$

Your fit2 does not really fit a random slope. Instead, it sets a random intercept for each combination of Days and Subject. This implies that for the computation of the random effects fit2 treats Days as a categorical rather than continuous variable.

You can check the difference by comparing the output of ranef(fit1) and ranef(fit2). For this example the correct model is clearly fit1, you can see it also by comparing the AIC or BIC of the two models.

$\endgroup$
8
  • $\begingroup$ Thank you very much for the clear answer! Could you explain more on when I should use fit1 then? $\endgroup$ Dec 28, 2017 at 9:22
  • 3
    $\begingroup$ More generally, only categorical variables should be put to the right of the | symbol in the formula: these indicate the "observational units" (subjects, etc.) according to which the random effects are grouped. Here fit1 assume that the probability of observing a reaction time longer than the median may depend linearly on the continuous variable Days and that this effect can be decomposed in a fixed effect common to all subjects, and in subject-specific random variations. fit1formula tells R explicitly to take into account these random variations and estimate their variance. $\endgroup$
    – matteo
    Dec 28, 2017 at 9:45
  • 1
    $\begingroup$ So if you want to estimate an effect which may have random fluctuations conditional to the "observational units" on which it is measured, you should use fit1 format. $\endgroup$
    – matteo
    Dec 28, 2017 at 9:47
  • 1
    $\begingroup$ Thank you so much! Now I understand clearly when I should use fit1. And I can select the model based on AIC, as well! I am now wondering, so, when fit2 would be used if fit1 include random slope and potentially explain the variance better. Would you mind teaching me for type of research design I should use fit2? $\endgroup$ Dec 28, 2017 at 16:46
  • 1
    $\begingroup$ that type of formula should be used when you have 2 grouping factors, e.g. g1 and g2, with g2 nested within g1. In that case it makes sense to use a random structure such as (1 | g1) + (1 | g1:g2). To learn more about that I suggest you to read this article (see in particular their Table 2 and related information in the main text). $\endgroup$
    – matteo
    Dec 29, 2017 at 14:21

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.