Having problems using the lme4 prediction function on my mixed models

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, , .

+4
1

merTools, , . README :

, . -, :

data(VerbAgg)
fmVA <- glmer(r2 ~ (Anger + Gender + btype + situ)^2 +
       (1|id) + (1|item), family = binomial, 
       data = VerbAgg)

, draw merTools. . wiggle , , , var. btype, situ Anger.

# Select the average case
newData <- draw(fmVA, type = "average")
newData <- wiggle(newData, var = "btype", values = unique(VerbAgg$btype))
newData <- wiggle(newData, var = "situ", values = unique(VerbAgg$situ))
newData <- wiggle(newData, var = "Anger", values = unique(VerbAgg$Anger))

head(newData, 10)

#>    r2 Anger Gender btype  situ id        item
#> 1   N    20      F curse other  5 S3WantCurse
#> 2   N    20      F scold other  5 S3WantCurse
#> 3   N    20      F shout other  5 S3WantCurse
#> 4   N    20      F curse  self  5 S3WantCurse
#> 5   N    20      F scold  self  5 S3WantCurse
#> 6   N    20      F shout  self  5 S3WantCurse
#> 7   N    11      F curse other  5 S3WantCurse
#> 8   N    11      F scold other  5 S3WantCurse
#> 9   N    11      F shout other  5 S3WantCurse
#> 10  N    11      F curse  self  5 S3WantCurse

predictInterval, . Anger, situ btype .

plotdf <- predictInterval(fmVA, newdata = newData, type = "probability", 
        stat = "median", n.sims = 1000)
plotdf <- cbind(plotdf, newData)
ggplot(plotdf, aes(y = fit, x = Anger, color = btype, group = btype)) + 
  geom_point() + geom_smooth(aes(color = btype), method = "lm") + 
  facet_wrap(~situ) + theme_bw() +
  labs(y = "Predicted Probability")

enter image description here

+1

All Articles