GLMM - Prediction with ID

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 calling x1, x2, x3, these variables were collected more than once for each person (ID) p> The goal is to predict the probability of occurrence of a fact, and each person also observed this fact occurred or not. (0 or 1)

I am sending an R code 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 objective is to be able to predict for any individual and not for a specific,

But at the end of the code it 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 masters in the understanding of the error, not being strictly faithful to the situation studied)

I thank you for 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))
##
    
asked by anonymous 04.03.2018 / 01:30

1 answer

1

For you to make predictions without using the random effect, ie without specifying for your "ID", do:

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

or

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

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

More information: ?predict.merMod

    
09.11.2018 / 13:36