merTools ForecastInterval()用于具有嵌套随机效应的模型 [英] merTools predictInterval() for model with nested random effect

查看:180
本文介绍了merTools ForecastInterval()用于具有嵌套随机效应的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

merTools包中的predictInterval()是否不像嵌套随机效果?例如,使用ggplot2包中的msleep数据集:

Does predictInterval() from the merTools package not like nested random effects? For example, using the msleep dataset from the ggplot2 package:

library("lme4")
library("merTools")
library("ggplot2")
mod <- lmer(sleep_total ~ bodywt + (1|vore/order), data=msleep)
predInt <- predictInterval(merMod=mod, newdata=msleep) 

返回错误:

Error in '[.data.frame'(newdata, , j) : undefined columns selected

运行正常没问题:

mod <- lmer(sleep_total ~ bodywt + (1|vore) + (1|order), data=msleep)
predInt <- predictInterval(merMod=mod, newdata=msleep)

(实际上,它会在随机效应变量中给出有关NA水平的警告,但我对此并不担心)

(Well actually it gives a warning about NA levels in the random effect variables, but I'm not concerned about that)

如下面的本·博克尔(Ben Bolker)的答案评论中所述,merTools的新版本说明了嵌套随机效应.但是,当我尝试预测包含嵌套随机效应新水平的数据时,会出错.

As discussed in the comments of Ben Bolker's answer below, a new version of merTools accounts for nested random effects. However, when I try to predict for data that contains new levels of the nested random effect, I get errors.

这有效:

mod <- lmer(sleep_total ~ bodywt + (1|vore/order), data=msleep)
predInt <- predictInterval(merMod=mod, newdata=msleep) 

这是可行的,尽管有几个警告(有关警告的其他问题,请参见下文*):

And this works, albeit with a couple of warnings (see below for additional Q about the warnings*):

mod <- lmer(sleep_total ~ bodywt + (1|vore) + (1|order), data=msleep)
msleep2 <- msleep %>% mutate(vore = "omni")
predInt <- predictInterval(merMod=mod, newdata=msleep2) 

但这不起作用:

mod <- lmer(sleep_total ~ bodywt + (1|vore/order), data=msleep)
msleep2 <- msleep %>% mutate(vore = "omni")
predInt <- predictInterval(merMod=mod, newdata=msleep2) 

出现以下错误:

Error in `[.data.frame`(tmp, alllvl) : undefined columns selected
In addition: Warning message:
In predictInterval(merMod = mod, newdata = msleep3) :
  newdata is tbl_df or tbl object from dplyr package and has been
              coerced to a data.frame

在这里,"omni"实际上不是vore的新级别,但是与order结合使用时,它会创建变量的新嵌套组合.

And here, "omni" isn't actually a new level of vore, but when combined with order, it creates new nested combinations of the variables.

如果我使用"new"或不是观察到的vore级别的其他任何东西,则会得到类似的结果:它适用于模型的非嵌套版本,但不适用于嵌套版本.

If I use "new" or anything else that isn't an observed level of vore, I get similar results: It works for the non-nested version of the model, but not for the nested version.

*此外,我应该关注上面第二个模型块给出的警告:

*Also, should I be concerned about the warning given by the second model chunk above:

> mod <- lmer(sleep_total ~ bodywt + (1|vore) + (1|order), data=msleep)
> msleep2 <- msleep %>% mutate(vore = "omni")
> predInt <- predictInterval(merMod=mod, newdata=msleep2)
Warning messages:
  1: In predictInterval(merMod = mod, newdata = msleep2) :
     newdata is tbl_df or tbl object from dplyr package and has been
       coerced to a data.frame
  2: In chol.default(sigma, pivot = TRUE) :
     the matrix is either rank-deficient or indefinite

我猜第二个是vore对每个观测值取相同值的结果,但这不应该成为预测的问题,对吗?如果我在拟合模型时变量具有相同的值,那我会看到一个问题,但是在预测新的观测值时,它不应该成为问题吗?

I'm going to guess the second one is a result of vore taking on the same value for each observation, but that shouldn't be an issue for prediction, should it? I could see it being an issue if the variable took on the same value when I was fitting the model, but don't think it should be an issue when predicting new observations?

推荐答案

一个人可以(显然)通过明确写出交互项来解决此问题. 警告:我实际上并没有检查以确保得出的预测是正确的,只是看到没有错误产生,并且得出的物体近似合理...

One can (apparently) work around this by writing out the interaction term explicitly. Warning: I haven't actually checked to make sure the resulting predictions are correct, just seen that no error is produced and the resulting object is approximately sensible ...

msleep <- transform(msleep,voreOrder=interaction(vore,order,drop=TRUE))
mod2 <- lmer(sleep_total ~ bodywt + (1|vore)+(1|voreOrder), data=msleep)
predInt <- predictInterval(merMod=mod2, newdata=msleep) 

这确实会生成警告消息,但显然是由于vore变量中的<NA>值引起的(我不知道此数据集...)

This does generate warning messages, but apparently they're due to <NA> values in the vore variable (I don't know this data set ...)

这篇关于merTools ForecastInterval()用于具有嵌套随机效应的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆