8

I understand that having a continuous or numeric variable as a random effect in a mixed effects model doesn't make much sense (e.g., see here).

But what I'm wondering is if lme4::lmer or nlme::lme in R purposefully prevent you from doing so...

Specifically, what I'm asking is: if I supply lmer (or lme) any non-factor (non-categorical) variable as a random effect, does the function automatically treat it as a factor?

Inserting factor() directly into lmer (as is the usual method when using lm) produces the following error:

lmer(y ~ z + (1|factor(x)), data = dat)
Error: couldn't evaluate grouping factor factor(x) within model frame: try adding grouping factor to data frame explicitly if possible

Although the above error mentions adding a grouping factor directly to the data, it doesn't specify whether said grouping factor needs to be a factor (or is that perhaps implicit from the word choice)?

I understand it's fairly simple to just create a new factor class variable directly from my data, but I'm just curious if it's actually necessary when using lmer (or lme).

Community
  • 1
  • 1
theforestecologist
  • 4,667
  • 5
  • 54
  • 91

1 Answers1

6

It doesn't seem to matter.

library(lme4)

sl <- sleepstudy
sl$Subject <- as.numeric(levels(sl$Subject))[sl$Subject]

## subject as factor
m1 <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)

## subject as numeric
m2 <- update(m1, data = sl)

all.equal(VarCorr(m1), VarCorr(m2))
# TRUE

Checking the rest of the object, the call is different (which makes sense, I called the data frame something different), and the frame is different (due to the numeric vs. factor difference in Subject). Everything else is identical.

all.equal(m1, m2)
#[1] "Attributes: < Component “call”: target, current do not match when deparsed >"     
#[2] "Attributes: < Component “frame”: Component “Subject”: 'current' is not a factor >"

Grouping factors are subjected to factorize() within mkBlist(), which is called within mkReTrms(), which creates the model matrix for the random effects. factorize() is a helper that eventually calls factor(x) on right hand side terms in the random effects formula (conditional on whether it's already a factor, etc.)

alexforrence
  • 2,724
  • 2
  • 27
  • 30
  • Thanks for the feedback! It is a bit of a superficial answer (answering the bolded question "does the function automatically treat it as a factor?" rather than *why* calling a function within the RE part of the formula doesn't work). It's not something I understand well enough to comment on, would you want to add an answer about that? – alexforrence Oct 10 '16 at 18:00
  • @alexforrence thanks, Alex this is helpful. Where/how exactly did you determine your final paragraph ("Grouping factors...etc.)"? – theforestecologist Oct 10 '16 at 18:03
  • 1
    @theforestecologist generally from the source at https://github.com/lme4/lme4/blob/master/R/utilities.R, is there a specific bit that doesn't make sense? – alexforrence Oct 10 '16 at 18:17
  • @alexforrence No I just didn't know how to do that on my own :p. – theforestecologist Oct 10 '16 at 18:20