lmer 中的 predict() 无法识别 id 变量

predict() in lmer not recognizing id variable

this tutorial 之后,我想使用 predict() 以便我可以弹出所有预测变量的唯一值,然后获取这些唯一值的预测值。

但我不断收到奇怪的错误(见下文)。有可能解决这个问题吗?

library(lme4)

dat3 <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/dat3.csv')

m4 <- lmer(math~pc1+pc2+discon+(pc1+pc2+discon|id), data=dat3)

newdata <- with(dat3, expand.grid(pc1=unique(pc1), pc2=unique(pc2), discon=unique(discon)))

predict(m4, newdata=newdata)

#### ERROR:
Error in model.frame.default(tt, newdata.NA, na.action = na.pass, xlev = orig.random.levs) : 
  invalid type (closure) for variable 'id'

您尚未在预测数据框中指定随机效应的值。要获得人口水平的预测(忽略随机效应),请使用

predict(m4, newdata=newdata, re.form=NA)

您收到一条奇怪的错误消息,因为您加载了一个包(可能是 dplyr),其中定义了一个 id() 函数:如果没有,您会得到更易于解释的错误信息

Error in eval(predvars, data, env) : object 'id' not found