当我们使用传统的逻辑回归并在R中进行预测时,例如:
library(dplyr)
n = 300
xx<-c("r1","r2","r3","r4","r5")
xxx<-c("e1","e2","e3")
p=0.3
df1 <- data_frame(
xx1 = runif(n, min = 0, max = 10),
xx2 = runif(n, min = 0, max = 10),
xx3 = runif(n, min = 0, max = 10),
School = factor(sample(xxx, n,re=TRUE)),
Rank = factor(sample(xx, n,re=TRUE)),
yx = as.factor(rbinom(n, size = 1, prob = p))
)
df1
mm<-glm(yx ~ xx1 + xx2 + xx3 + School + Rank,binomial,df1)
n11 = data.frame(School="e3",Rank="r2",xx1=8.58,xx2=8.75,xx3=7.92)
我们使用:
predict(mm, n11, type="response") #No meu caso especifico
你预测(mm,n11)
取决于我们感兴趣的东西,没有问题。
但是当我们使用GLMM时
library(lme4)
mm2 <- glmer(yx ~ xx1 + xx2 + xx3 + Rank + (Rank | School), data = df1,
family = "binomial",control = glmerControl(calc.derivs = FALSE))
predict(mm2, n11, type="response") #No meu caso especifico
显示错误
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
我试过这样做
predict(m2,n11, re.form=(~Rank|School))
这会显示错误
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "glmmadmb"
R-GLMM中预测的正确形式是什么?
答案 0 :(得分:5)
问题是您的模型规范与您提供的新数据的结构不匹配。更具体地说,(自动转换为因子)变量School和Rank的级别只有一个级别,而模型需要三个级别。它具有三个级别的参数,因此如果找不到这三个级别,则无法使用正确的设计矩阵来计算新的预测。
这就是为什么@Roland在评论中是正确的根本原因,你必须专门创建一个与训练模型的数据中使用的相同级别的变量。
n11 <- data.frame(School=factor("e3", levels = levels(df1$School)),
Rank=factor("r2", levels =levels(df1$Rank)),
xx1=8.58,xx2=8.75,xx3=7.92)