I have a bit of a struggle trying to use the lme4 prediction function on my mixed models. When you make predicates, I want some of my explanatory variables to be set at a certain level, but on average for others.
Here are some compiled data that are a simplified, meaningless version of my original dataset:
a <- data.frame(
TLR4=factor(rep(1:3, each=4, times=4)),
repro.state=factor(rep(c("a","j"),each=6,times=8)),
month=factor(rep(1:2,each=8,times=6)),
sex=factor(rep(1:2, each=4, times=12)),
year=factor(rep(1:3, each =32)),
mwalkeri=(sample(0:15, 96, replace=TRUE)),
AvM=(seq(1:96))
)
The AvM number is a water vole identification number. The response variable ( mwalkeri) is the number of fleas per vole. The main variable that interests me is Tlr4, which is a gene with 3 different genotypes (encoded 1, 2, and 3). Other explanatory variables include reproductive status (adult or minor), month (1 or 2), gender (1 or 2) and year (1, 2 or 3). My model looks like this (of course, this model is not suitable for compiled data now, but it does not matter):
install.packages("lme4")
library(lme4)
mm <- glmer(mwalkeri~TLR4+repro.state+month+sex+year+(1|AvM), data=a,
family=poisson,control=glmerControl(optimizer="bobyqa"))`
summary(mm)
I want to make predictions regarding the parasite load for each other Tlr4 genotype, taking into account all other covariates. To do this, I created a new dataset to indicate the level at which I wanted to set each of the explanatory variables and use the prediction function:
b <- data.frame(
TLR4=factor(1:3),
repro.state=factor(c("a","a","a")),
month=factor(rep(1, times=3)),
sex=factor(rep(1, times=3)),
year=factor(rep(1, times=3))
)
predict(mm, newdata=b, re.form=NA, type="response")
, , . , , :
model.frame.default(delete.response(Terms), newdata, na.action = na.action,: factor year
, , ? , , , . - lsmeans() ( lsmeans):
c <- lsmeans(mm, "TLR4", type="response")
summary(c, type="response")
. . , , , , predict(), . - Tlr4 x y, , .