Predict function in GLMM

4

Edited

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

or     predict (mm, n11)

depending on what interests us, no problem.

But when working with GLMM, for example

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

displays error

 Error in 'contrasts<-'('*tmp*', value = contr.funs[1 + isOF[nn]]) : 
  contrastes podem ser aplicados apenas a fatores com 2 ou mais níveis

I tried to do so

 predict(m2,n11, re.form=(~Rank|School))

and displays the error

 Error in UseMethod("predict") : 
   no applicable method for 'predict' applied to an object of class "glmmadmb"

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

    
asked by anonymous 26.01.2018 / 12:17

1 answer

4

The problem is with the declaration of the new dataset to be predicted. In some cases (which unfortunately I can not tell you exactly what they are), the lme4 package requires that factors be used to make the prediction. So I created a new df1 , taking this into account:

n = 300
xx<-c("r1", "r2", "r3", "r4", "r5")
xxx<-c("e1", "e2", "e3")
p=0.3
School = factor(sample(xxx, n, replace=TRUE), levels=xxx, ordered=FALSE)
Rank = factor(sample(xx, n, replace=TRUE), levels=xx, ordered=TRUE)

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 = School,
  Rank = Rank,
  yx = as.factor(rbinom(n, size = 1, prob = p))
)

df1

See that my code looks a lot like your original. However, I have forced School and Rank to have specified factors ( xxx and xx , respectively), in addition to determining that School is not ordered and Rank is. Also, I have created objects called School and Rank out of df1 . This will be important in the future.

So far, there's not much difference in what you've done. Now, notice how to define n11 , the dataset where the forecast will be made:

mm<-glm(yx ~ xx1 + xx2 + xx3 + School + Rank,binomial,df1)

n11 = data.frame(School=sort(unique(School))[3], 
  Rank=sort(unique(Rank))[2], xx1=8.58, xx2=8.75, xx3=7.92)

Note that I have determined the values of School and Rank based on the School and Rank objects I created earlier. Thus, sort(unique(School))[3] is the third value of School . A similar idea holds for sort(unique(Rank))[2] . Now just make the predictions:

predict(mm, n11, type="response")
        1 
0.3715539

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
        1 
0.4048813 

I recognize that writing the levels to be predicted in the form School=sort(unique(School))[3] and Rank=sort(unique(Rank))[2] is a bit ugly, but I only know how to make it work this way.

    
26.01.2018 / 15:22