Skip to content

GLMM - Prediction

5 messages · Thierry Onkelinx, Ben Bolker, Cleber Iack

#
Dear,

I have n different companies with different characteristics and each
company of this has the same positions
I selected data from several members of each position of each Company, and
there were behavioral variables ranging from 0 to 10, which to facilitate I
will call x1, x2, x3, these variables were collected more than once for
each person.

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

Using

formMod1= fato~  x1+x2+x3+(1 | company / position)
Mod1 <- glmer( formMod1 , data = dadosord , family = binomial,
                     control = glmerControl(optimizer="bobyqa"))

This model I can make a prediction without "problems"

n11 <-  data.frame(company =factor("M1", levels =
levels(dadosord$company ),ordered=FALSE),
                   position=factor("P1", levels =
levels(dadosord$position),ordered=FALSE),
                   x1=1, x2=7,x3=7)
predict( Mod1 , n11, type="response")

But I was worried because I have more than one observation of the
characteristics per individual, if I would not have to put it also as random
or somehow analyze this structure of possible correlation

Thinking about the first case I did

formMod2= fato~  x1+x2+x3+(1 | company / position) + (1 | ID)
Mod2 <- glmer( formMod2 , data = dadosord , family = binomial,
                     control = glmerControl(optimizer="bobyqa"))

But in this case when trying to predict the probability of the fact, the
function "predict" asks me who is the proposed id, which is out of
interest, since I must study for a random person who has the same features
x1, x2, x3, company and position.

And the second form, if any, I do not know how I would do

Thanks for the help of my friends.

Iack
#
Dear Iack,

Look at the re.form argument of ?lme4::predict.merMod. This allows you the
make predictions without the ID random effect.

Best regards,


ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkelinx at inbo.be
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>

2018-03-02 1:09 GMT+01:00 Cleber Iack <profiack at gmail.com>:

  
  
1 day later
#
Dear

First, thank you very much for your time in answering.

I'm sending a code that I tried not to inform the ID and I couldn't,

Displays the following error at the end

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

I'd appreciate it if you could give me some guidance

Thanks
####

library(plyr)

library(dplyr)

library(lme4)

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=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),

  School = School,

  Rank = Rank,

  yx = as.factor(rbinom(n, size = 1, prob = p))

)

df1 = df1[order(df1$ID, decreasing=FALSE),]

library(lme4)

mm2 <- glmer(yx ~ xx1 + xx2 + xx3 + Rank +  (1 | ID) + (1 | School / Rank),
data = df1,

             family = "binomial",control = glmerControl(calc.derivs =
FALSE))



n11 <-  data.frame(School=factor("e1", levels =
levels(df1$School),ordered=FALSE),

                   Rank=factor("r1", levels =
levels(df1$Rank),ordered=FALSE),

                   xx1=8.58, xx2=8.75, xx3=7.92)

predict(mm2, n11, type="response",re.form= ~(1 | School / Rank))

##


2018-03-02 9:46 GMT+00:00 Thierry Onkelinx <thierry.onkelinx at inbo.be>:

  
  
#
This looks like a bug in lme4: https://github.com/lme4/lme4/issues/457
. A simple workaround is to specify some value for `ID`
(which will be ignored).
On Sat, Mar 3, 2018 at 7:06 PM, Cleber Iack <profiack at gmail.com> wrote:
#
Thank you very much,

I tested, really is a bug.

God bless you.

2018-03-04 19:11 GMT+00:00 Ben Bolker <bbolker at gmail.com>: