invalid grouping factor specification in lmer model

萝らか妹 提交于 2019-12-10 20:38:36

问题


I am attempting to run a mixed effects model using the lmer function. My experiment included metabolic rates at different temperatures using some of the same individuals (some missing data). The structure of the textfile looks like this:

> str(data.by.animal)
'data.frame':   18 obs. of  17 variables:
 $ animal: Factor w/ 18 levels "08_03","08_07",..: 17 6 5 10 15 14 11 12 16 9 ...
 $ temp  : int  2 0 -2 -4 -6 -8 -10 -12 -14 -16 ...
 $ X2    : num  0.0129 0.0176 0.0132 NA 0.0144 0.0133 0.0101

When I run the script [model_1 <- lmer(X2 + X0 + X.2 + X.4 + X.6 + X.8 + X.10 + X.12 + X.14 + X.16 + X.18 + X.20 + X.22 + X.24 + X.26 ~ temp + (1 | animal), data.by.animal)] I get the following: [Error in FUN(X[[1L]], ...) : Invalid grouping factor specification, animal] and despite consulting "The R Book" and other answers here, I'm still at a loss as to where I'm going wrong.


回答1:


As stated in my comment, this model specification doesn't make sense -- lmer requires a single response variable on the left-hand side of the ~. It's not tested for, because it's not a mistake we thought of anyone making ... (What were you trying to do ?? Did you want to run separate analyses for each of the X* variables?)

I can more or less reproduce this, as long as one of the elements on the left-hand side is a factor ...

library(lme4)

This does something, but I'm not sure what ...

lmer(Reaction + Days ~ (1| Subject), sleepstudy)

it's close to lmer(Reaction ~ (1|Subject), sleepstudy) (which I might have expected -- silently ignoring the second term on the LHS?), but not identical ...

Factors on the LHS are allowed, although they don't make much sense (they're probably just being converted to numeric):

lmer(factor(Days) ~ (1| Subject), sleepstudy)

If I put them both in I can get the error:

lmer(Reaction + factor(Days) ~ (1| Subject), sleepstudy)
## Error in FUN(X[[1L]], ...) : 
##   Invalid grouping factor specification, Subject
## In addition: Warning message:
## In Ops.factor(Reaction, factor(Days)) : + not meaningful for factors



回答2:


I also encountered with this problem when using lmer. When I check my data, I found values of several variables were NA (resulted from rescaling). After excluding those variables, the problem was solved.




回答3:


Syntactically, this is correct. You want to fit a random intercepts model with a random effects for animal. You have 18 observations and animal is a factor with 17 levels, so basically there is only at most 1 repeated measure. Mixed models won't converge in those kinds of circumstances. I urge you to rethink the analysis you have here.

As far as the error, the lme4 package source throws the error here:

    ## convert grouping variables to factors as necessary
    for (i in all.vars(x[[3]])) {
        frloc[[i]] <- factor(frloc[[i]])
    }
    ff <- eval(substitute(factor(fac), list(fac = x[[3]])), frloc)
    if (all(is.na(ff)))
        stop("Invalid grouping factor specification, ",
             deparse(x[[3]]))

you might want to consider how animal is coded. Try recoding to some other value.

Can you print the output of traceback() after your shell throws the error?



来源:https://stackoverflow.com/questions/19015943/invalid-grouping-factor-specification-in-lmer-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!