GLMM - Prediction with ID

Asked

Viewed 43 times

2

Friends,

I have different companies with different characteristics and each company has the same positions I selected the data of several members of each position of each company and noted behavioral variables, ranging from 0 to 10, to facilitate call X1, x2, X3, these variables were collected more than once for each person (ID).

The objective is to predict the probability of occurrence of a fact, and each person also observed whether this fact occurred or not. (0 or 1)

I am sending a code R that simulates the situation and at the time of predicting the probability of occurrence of the fact I tried not to inform the ID because the goal is to be able to predict for any individual and not for a specific,

Only at the end of the code displays the following error at the end

 "Error in eval (predvars, date, env): object ID not found"

(The code in R is only to facilitate the help of the lords in understanding the error, not being strictly faithful to the situation studied)

I appreciate your help

 library(plyr)
 library(dplyr)
 library(lme4)
 n = 300
 xx<-c("r1", "r2", "r3", "r4", "r5")
 xxx<-c("e1", "e2", "e3")
 p=0.3
 Empresa = factor(sample(xxx, n, replace=TRUE), levels=xxx, ordered=FALSE)
 Cargo = factor(sample(xx, n, replace=TRUE), levels=xx, ordered=FALSE)

df1 <- data_frame(
   ID = as.integer(runif(n, min = 1, max = n/7)),
   xx1 = runif(n, min = 0, max = 10),
   xx2 = runif(n, min = 0, max = 10),
   xx3 = runif(n, min = 0, max = 10),
   Empresa = Empresa,
   Cargo = Cargo,
   Fato = as.factor(rbinom(n, size = 1, prob = p))
)
 df1 = df1[order(df1$ID, decreasing=FALSE),]
 library(lme4)
 mm2 <- glmer(Fato ~ xx1 + xx2 + xx3 + Cargo +  (1 | ID) + (1 | Empresa / 
 Cargo), data = df1, 
         family = "binomial",control = glmerControl(calc.derivs = FALSE))

 n11 <-  data.frame(Empresa=factor("e1", levels = 
 levels(df1$Empresa),ordered=FALSE),
               Cargo=factor("r1", levels = levels(df1$Cargo),ordered=FALSE),
               xx1=8.58, xx2=8.75, xx3=7.92)
predict(mm2, n11, type="response",re.form= ~(1 | Empresa / Cargo))
##

1 answer

1

For you to make the predictions without using the random effect, that is, without particularizing for your "ID", do:

predict(mm2, n11, type="response", re.form = NA)

or

predict(mm2, n11, type="response", re.form = 0)

The Error "Error in eval (predvars, date, env): object ID not found" happened because you were probably using re.form = ~ (1|ID) and had not informed ID in his n11.

More information: ?predict.merMod

Browser other questions tagged

You are not signed in. Login or sign up in order to post.