# Predict function with glmer

I am trying to use the predict function to estimate a dependent variable in my mixed model given value of the two independent variables that are in the model. The estimable function in lmer for some reasons doesn't work with glmer. Can someone help with how I could estimate this? see my code. I got error that my object is not a matrix.

model1=glmer(response~treatment+month+treatment*month +
(1|id),
data=data1,
nAGQ=100)
summary(model1)

nedff = data.frame(treatment =1, month =12)
predict(model1, nedff, se.fit=T, interval ="confidence")

Hello @abasbabatunde ,

It would be much easier to help you if your example was reproducible. The fact that we don't have your data (or a piece of it) makes it hard to debug your code on your end. If you want to know more about asking a reproducible question, please check this:

Poor me! I went through the link you shared and I still didn't know how to use the reprex. I am finding it difficult to get my code chunk as it is and post here or my data. Any other means to help?

You could copy and paste the output of:

`dput(data1)`

Then we should be able to use it to recreate your dataset. If the data is too big, then you could only share the first 20 lines (for exemple). The code for that would be:

`dput(head(data1, 20))`

Thank you so much. I will reach out later to know how to do reprex because R is going to be my main language soon. here is the output from dput(head(data1,20).

structure(list(V1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), V2 = c(1L, 1L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L
), V3 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), V4 = c(0, 0.8571428571, 3.5357142857,
4.5357142857, 7.5357142857, 10.035714286, 13.071428571, 0, 0.9642857143,
2, 3.0357142857, 6.5, 9, 0, 1.25, 2.1071428571, 3.25, 6.75, 9.0357142857,
11.5), V5 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L,
6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)), row.names = c(NA, 20L), class = "data.frame")

Here is my glmer code
model1=glmer(response~treatment+month+treatment*month +
(1|id),
data=data1,
nAGQ=100)
summary(model1)

I am trying to estimate response when treatment is 1 and month is 12. This is not working.

nedff = data.frame(treatment =1, month =12)
predict(model1, nedff, se.fit=T, interval ="confidence")

the glmer code you wrote with respect to data1 could not work with the data1 example you gave us; what you gave us does not have the named columns response, treatment etc. it has V1,V2,V3, etc..

Apoligies, here is the clean data

structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), response = c(1L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L), treatment = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), month = c(0, 0.8571428571,
3.5357142857, 4.5357142857, 7.5357142857, 10.035714286, 13.071428571,
0, 0.9642857143, 2, 3.0357142857, 6.5, 9, 0, 1.25, 2.1071428571,
3.25, 6.75, 9.0357142857, 11.5), visit = c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)), row.names = c(NA,
20L), class = "data.frame")

so, so the error about your data not being a matrix is unfortunately unclear; but its caused by your model formula involving id; which means predict wants to see an id column

That is unfortunate because the ID is needed as the random effect in the model.
Which other way do you think I can predict or get estimates based on the values of the predictors?

I dont think theres harm simply adding an id field to your new data. But maybe someone more knowledgeable than me will chime in.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.