R: Interaction Plot with a continuous and a categorical variable for a GLMM (lme4)

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 07:32:08

Here's an answer of sorts (by the way, you had some missing quotation marks in your data frame above, which had to be fixed manually ...)

Fit the model:

library(lme4)
fit <- glmer(resp.var ~ cont.var:cat.var + (1|rand.eff) ,
           data = sample.data , poisson)

(Note that this is a slightly weird model specification -- forces all categories to have the same value at cont.var==0. Did you mean cont.var*cat.var?

library(ggplot2)
theme_update(theme_bw())  ## set white rather than gray background

Quick and dirty linear regressions:

ggplot(sample.data,aes(cont.var,resp.var,linetype=cat.var))+
    geom_smooth(method="lm",se=FALSE)

Now with a Poisson GLM (but not incorporating the random effect), and showing the data points:

ggplot(sample.data,aes(cont.var,resp.var,colour=cat.var))+
    stat_sum(aes(size=..n..),alpha=0.5)+
    geom_smooth(method="glm",family="poisson")

The next bit requires the development (r-forge) version of lme4, which has a predict method:

Set up data frame for prediction:

predframe <- with(sample.data,
                  expand.grid(cat.var=levels(cat.var),
                              cont.var=seq(min(cont.var),
                              max(cont.var),length=51)))

Predict at population level (REform=NA), on the linear predictor (logit) scale (this is the only way you will get straight lines on the plot)

predframe$pred.logit <- predict(fit,newdata=predframe,REform=NA)

minmaxvals <- range(sample.data$cont.var)

ggplot(predframe,aes(cont.var,pred.logit,linetype=cat.var))+geom_line()+
    geom_point(data=subset(predframe,cont.var %in% minmaxvals),
               aes(shape=cat.var))

Now on the response scale:
predframe$pred <- predict(fit,newdata=predframe,REform=NA,type="response")
ggplot(predframe,aes(cont.var,pred,linetype=cat.var))+geom_line()+
    geom_point(data=subset(predframe,cont.var %in% minmaxvals),
               aes(shape=cat.var))

The jtools package (CRAN link) can make the plotting of this sort of model pretty straightforward. I'm the developer of that package.

We will fit the model like Ben did in his answer:

library(lme4)
fit <- glmer(resp.var ~ cont.var:cat.var + (1 | rand.eff),
             data = sample.data, family = poisson)

And with jtools we just use the interact_plot function like this:

library(jtools)
interact_plot(fit, pred = cont.var, modx = cat.var)

The result:

By default it plots on the response scale, but you can have it plotted on the linear scale with the outcome.scale = "link" argument (default is "response").

The effects package has support for lme4 models, and should be able to do what you want.

effects: Effect Displays for Linear, Generalized Linear, and Other Models

Graphical and tabular effect displays, e.g., of interactions, for various statistical models with linear predictors.

It also comes with two slightly outdated papers (you can think of them as vignettes).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!