R中的预测-GLMM [英] Prediction in R - GLMM

查看:509
本文介绍了R中的预测-GLMM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当我们使用传统的逻辑回归并在R中进行预测时,例如:

When we use a traditional logistic regression and make a prediction in R, for example:

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

您的预测(毫米,n11)

ou predict(mm, n11)

取决于我们的兴趣,没问题.

depending on what interests us, no Problem.

但是当我们使用GLMM时

But when we work with 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中预测的正确形式是什么?

What would be the correct form of the prediction in R - GLMM?

推荐答案

问题是您的模型规范与您提供的新数据的结构不匹配.更具体地说,(自动转换为因子的)变量School和Rank的级别只有一个级别,而模型则预期为三个级别.它具有三个级别的参数,因此,如果找不到这三个级别,则无法使用适当的设计矩阵来计算新的预测.

The problem is that your model specification doesn't match the structure of the new data you provide. More specifically, the levels of the (automatically converted to factor) variables School and Rank only have a single level, whereas the model expects three levels. It has parameters for three levels, so if those three levels can't be found, you can't use the proper design matrix to calculate the new predictions.

这就是为什么@Roland在注释中正确的根本原因,并且您必须专门创建一个变量,该变量的级别与训练模型的数据中使用的级别相同.

That's the underlying reason as to why @Roland is right in the comments and that you have to specifically create a variable with the same levels as used in the data that trained the model.

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)

这篇关于R中的预测-GLMM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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