简体   繁体   English

R:与GLMM(lme4)的连续和分类变量的交互图

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

I would like to make an interaction plot to visually display the difference or similarity in slopes of interaction of a categorical variable (4 levels) and a standardized continuous variable from the results of a regression model. 我想制作一个交互作用图,从回归模型的结果中可视地显示分类变量(4个级别)和标准化连续变量的相互作用斜率的差异或相似性。

with(GLMModel, interaction.plot(continuous.var, categorical.var, response.var)) Is not what I am looking for. with(GLMModel, interaction.plot(continuous.var, categorical.var, response.var))不是我要找的。 It produces a plot in which the slope changes for each value of the continuous variable. 它产生一个图,其中斜率随连续变量的每个值而变化。 I'm looking to make a plot with constant slopes as in the following plot: 我正在寻找一个具有恒定斜率的图,如下图所示:

在此输入图像描述

Any ideas? 有任何想法吗?

I fit a model of the form fit<-glmer(resp.var ~ cont.var*cat.var + (1|rand.eff) , data = sample.data , poisson) Here is some sample data: 我适合形式拟合的模型fit<-glmer(resp.var ~ cont.var*cat.var + (1|rand.eff) , data = sample.data , poisson)以下是一些示例数据:

structure(list(cat.var = structure(c(4L, 4L, 1L, 4L, 1L, 2L, 
1L, 1L, 1L, 1L, 4L, 1L, 1L, 3L, 2L, 4L, 1L, 1L, 1L, 2L, 1L, 2L, 
2L, 1L, 3L, 1L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 4L, 1L, 3L, 1L, 3L, 
3L, 4L, 3L, 4L, 1L, 3L, 3L, 1L, 2L, 3L, 4L, 3L, 4L, 2L, 1L, 1L, 
4L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 4L, 4L, 3L, 3L, 1L, 3L, 3L, 
3L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 4L, 1L, 3L, 4L, 1L, 1L, 4L, 
1L, 3L, 1L, 1L, 3L, 2L, 4L, 1L, 4L, 1L, 4L, 4L, 4L, 4L, 2L, 4L, 
4L, 1L, 2L, 1L, 4L, 3L, 1L, 1L, 3L, 2L, 4L, 4L, 1L, 4L, 1L, 3L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, 2L, 3L, 4L, 1L, 
3L, 2L, 2L, 1L, 1L, 1L, 3L, 1L, 2L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 3L, 3L, 2L, 1L, 3L, 2L, 1L, 
1L, 1L, 4L, 1L, 1L, 2L, 3L, 1L, 1L, 2L, 4L, 3L, 2L, 4L, 3L, 2L, 
1L, 3L, 1L, 3L, 1L, 4L, 3L, 1L, 4L, 4L, 2L, 4L, 1L, 1L, 2L, 4L, 
4L, 2L, 3L, 4L, 4L, 3L, 1L, 4L, 1L, 2L, 4L, 1L, 1L, 4L, 1L, 1L, 
1L, 1L, 1L, 3L, 4L, 1L, 4L, 4L, 2L, 2L, 2L, 2L, 3L, 4L, 4L, 1L, 
1L, 4L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 3L, 4L, 2L, 
3L, 1L, 1L, 1L, 4L, 1L, 1L, 4L, 4L, 4L, 1L, 1L, 1L, 1L), .Label = c("A", 
"B", "C", "D"), class = "factor"), cont.var = c(-0.0682900527296927, 
0.546320421837542, -0.273160210918771, -0.887770685486005, 0.136580105459385, 
0.75119058002662, 0.546320421837542, -0.273160210918771, -0.682900527296927, 
0.136580105459385, 0.75119058002662, 0.75119058002662, 0.75119058002662, 
0.341450263648464, 0.75119058002662, 0.546320421837542, 0.546320421837542, 
-0.478030369107849, -0.478030369107849, -0.682900527296927, -0.682900527296927, 
0.546320421837542, -0.478030369107849, -0.0682900527296927, 0.136580105459385, 
0.136580105459385, 0.75119058002662, -0.478030369107849, 0.75119058002662, 
-0.887770685486005, 0.136580105459385, -0.478030369107849, 0.341450263648464, 
-0.682900527296927, -0.478030369107849, 0.341450263648464, -0.478030369107849, 
0.546320421837542, 0.75119058002662, -0.478030369107849, -0.273160210918771, 
0.546320421837542, -0.682900527296927, 0.75119058002662, -0.478030369107849, 
-0.887770685486005, 0.136580105459385, -0.887770685486005, -0.0682900527296927, 
-0.478030369107849, 0.546320421837542, 0.75119058002662, 0.136580105459385, 
-0.273160210918771, -0.273160210918771, 0.75119058002662, -0.682900527296927, 
0.136580105459385, -0.273160210918771, -0.273160210918771, 0.136580105459385, 
0.136580105459385, 0.341450263648464, 0.136580105459385, -0.273160210918771, 
-0.273160210918771, -0.682900527296927, -0.887770685486005, -0.0682900527296927, 
0.136580105459385, -0.0682900527296927, -0.273160210918771, -0.273160210918771, 
0.341450263648464, 0.75119058002662, -0.682900527296927, -0.0682900527296927, 
-0.273160210918771, -0.887770685486005, -0.0682900527296927, 
0.75119058002662, 0.546320421837542, 0.75119058002662, 0.75119058002662, 
-0.887770685486005, 0.341450263648464, 0.75119058002662, -0.887770685486005, 
0.136580105459385, -0.273160210918771, 0.546320421837542, 0.546320421837542, 
-0.682900527296927, 0.75119058002662, 0.136580105459385, -0.0682900527296927, 
-0.478030369107849, 0.75119058002662, -0.478030369107849, 0.341450263648464, 
0.136580105459385, -0.0682900527296927, -0.478030369107849, -0.0682900527296927, 
-0.0682900527296927, 0.546320421837542, -0.273160210918771, 0.75119058002662, 
0.341450263648464, 0.546320421837542, -0.478030369107849, 0.136580105459385, 
-0.887770685486005, -0.273160210918771, -0.273160210918771, -0.478030369107849, 
-0.478030369107849, 0.75119058002662, -0.682900527296927, -0.0682900527296927, 
0.546320421837542, 0.75119058002662, 0.546320421837542, 0.136580105459385, 
-0.478030369107849, 0.136580105459385, 0.546320421837542, -0.478030369107849, 
-0.0682900527296927, -0.0682900527296927, 0.546320421837542, 
-0.273160210918771, 0.136580105459385, -0.0682900527296927, 0.75119058002662, 
-0.0682900527296927, 0.546320421837542, -0.887770685486005, -0.0682900527296927, 
-0.682900527296927, -0.478030369107849, -0.478030369107849, -0.682900527296927, 
0.75119058002662, 0.341450263648464, -0.0682900527296927, 0.341450263648464, 
-0.0682900527296927, -0.887770685486005, -0.887770685486005, 
-0.273160210918771, -0.0682900527296927, 0.546320421837542, -0.0682900527296927, 
-0.0682900527296927, 0.75119058002662, -0.0682900527296927, -0.273160210918771, 
-0.478030369107849, 0.546320421837542, 0.546320421837542, 0.546320421837542, 
0.341450263648464, 0.136580105459385, -0.478030369107849, 0.136580105459385, 
0.136580105459385, 0.136580105459385, -0.478030369107849, -0.273160210918771, 
-0.273160210918771, -0.273160210918771, 0.341450263648464, -0.273160210918771, 
-0.0682900527296927, 0.136580105459385, 0.546320421837542, -0.478030369107849, 
-0.273160210918771, 0.546320421837542, 0.546320421837542, -0.273160210918771, 
-0.0682900527296927, 0.341450263648464, 0.546320421837542, -0.0682900527296927, 
0.136580105459385, -0.478030369107849, 0.75119058002662, -0.478030369107849, 
-0.682900527296927, -0.478030369107849, 0.136580105459385, -0.273160210918771, 
-0.0682900527296927, -0.887770685486005, -0.887770685486005, 
0.546320421837542, -0.273160210918771, 0.546320421837542, -0.478030369107849, 
0.546320421837542, -0.0682900527296927, 0.75119058002662, -0.273160210918771, 
0.546320421837542, 0.341450263648464, -0.0682900527296927, -0.0682900527296927, 
-0.0682900527296927, -0.887770685486005, 0.136580105459385, -0.273160210918771, 
-0.478030369107849, 0.75119058002662, 0.341450263648464, 0.546320421837542, 
-0.273160210918771, 0.546320421837542, 0.75119058002662, -0.273160210918771, 
0.75119058002662, 0.546320421837542, -0.273160210918771, -0.273160210918771, 
0.75119058002662, -0.273160210918771, -0.0682900527296927, 0.136580105459385, 
-0.478030369107849, 0.75119058002662, 0.75119058002662, -0.887770685486005, 
-0.887770685486005, 0.546320421837542, -0.682900527296927, -0.887770685486005, 
0.136580105459385, 0.75119058002662, 0.75119058002662, -0.478030369107849, 
0.136580105459385, 0.75119058002662, -0.273160210918771, -0.682900527296927, 
-0.273160210918771, 0.136580105459385, 0.546320421837542, -0.682900527296927, 
-0.478030369107849, 0.136580105459385, -0.682900527296927, -0.0682900527296927, 
-0.478030369107849, 0.136580105459385, -0.887770685486005, -0.273160210918771, 
-0.0682900527296927, -0.273160210918771, -0.887770685486005, 
0.546320421837542, 0.546320421837542, -0.478030369107849, -0.273160210918771, 
-0.0682900527296927, 0.136580105459385, -0.478030369107849, 0.75119058002662, 
0.341450263648464, 0.136580105459385, 0.136580105459385, 0.75119058002662, 
0.136580105459385, -0.0682900527296927, 0.546320421837542, -0.0682900527296927, 
-0.887770685486005, 0.75119058002662, 0.75119058002662, 0.546320421837542, 
-0.887770685486005, -0.0682900527296927, -0.682900527296927, 
-0.682900527296927, 0.75119058002662, 0.75119058002662, -0.478030369107849, 
0.546320421837542, -0.273160210918771, 0.75119058002662, -0.0682900527296927, 
0.546320421837542, -0.0682900527296927, -0.273160210918771, 0.546320421837542, 
0.75119058002662, -0.0682900527296927, 0.546320421837542, -0.682900527296927, 
-0.273160210918771, -0.0682900527296927, -0.478030369107849, 
-0.478030369107849, 0.136580105459385, -0.273160210918771, 0.136580105459385, 
0.546320421837542, 0.75119058002662, -0.273160210918771, 0.341450263648464, 
-0.273160210918771, 0.136580105459385, 0.546320421837542, 0.546320421837542, 
0.136580105459385, 0.136580105459385, -0.682900527296927, 0.341450263648464, 
0.341450263648464, -0.273160210918771, -0.682900527296927, -0.0682900527296927, 
0.75119058002662, -0.887770685486005, -0.478030369107849, -0.273160210918771, 
-0.478030369107849, -0.478030369107849, 0.136580105459385, -0.478030369107849, 
0.136580105459385, -0.478030369107849, 0.136580105459385, -0.0682900527296927, 
-0.273160210918771, 0.136580105459385, 0.341450263648464, -0.478030369107849, 
0.75119058002662, 0.136580105459385, 0.341450263648464, 0.546320421837542, 
-0.887770685486005, 0.75119058002662, 0.341450263648464, -0.0682900527296927, 
-0.478030369107849, 0.546320421837542, 0.136580105459385, -0.682900527296927, 
-0.0682900527296927, 0.341450263648464, -0.478030369107849, -0.0682900527296927, 
-0.478030369107849, -0.0682900527296927, 0.341450263648464, -0.478030369107849, 
-0.682900527296927, 0.75119058002662, -0.478030369107849, -0.682900527296927, 
0.341450263648464, -0.887770685486005, -0.478030369107849, 0.546320421837542, 
-0.887770685486005, -0.478030369107849, -0.478030369107849, 0.341450263648464, 
0.75119058002662, -0.682900527296927, 0.75119058002662, 0.75119058002662, 
0.341450263648464, -0.0682900527296927, 0.546320421837542, -0.0682900527296927, 
0.136580105459385, 0.136580105459385, 0.136580105459385, 0.136580105459385, 
0.546320421837542, 0.546320421837542, -0.0682900527296927, 0.75119058002662, 
-0.0682900527296927, -0.0682900527296927, -0.682900527296927, 
-0.273160210918771, -0.682900527296927, -0.478030369107849, 0.136580105459385, 
0.75119058002662, 0.546320421837542, 0.341450263648464, -0.887770685486005, 
-0.0682900527296927, 0.136580105459385, 0.75119058002662, -0.273160210918771, 
-0.682900527296927, 0.136580105459385, -0.478030369107849, -0.273160210918771, 
-0.273160210918771, 0.136580105459385, 0.341450263648464, -0.478030369107849, 
-0.0682900527296927, -0.682900527296927, 0.75119058002662, -0.273160210918771, 
-0.478030369107849, -0.0682900527296927, -0.0682900527296927, 
-0.273160210918771, -0.0682900527296927, -0.478030369107849, 
0.75119058002662, -0.0682900527296927, 0.136580105459385, 0.546320421837542, 
0.546320421837542, -0.478030369107849, -0.273160210918771, 0.546320421837542, 
-0.478030369107849, -0.682900527296927, 0.75119058002662, -0.0682900527296927, 
-0.682900527296927, -0.682900527296927, 0.75119058002662, 0.341450263648464, 
-0.478030369107849, 0.75119058002662, 0.136580105459385, -0.887770685486005, 
0.341450263648464, 0.341450263648464, 0.546320421837542, -0.273160210918771, 
0.136580105459385, 0.75119058002662, -0.0682900527296927, -0.682900527296927, 
-0.478030369107849, -0.478030369107849, 0.75119058002662, 0.546320421837542, 
-0.478030369107849, 0.546320421837542, 0.136580105459385, -0.887770685486005, 
0.75119058002662, -0.0682900527296927, 0.75119058002662, 0.75119058002662, 
-0.273160210918771, -0.682900527296927, 0.546320421837542, 0.546320421837542, 
-0.887770685486005, 0.75119058002662, -0.273160210918771, 0.546320421837542, 
-0.0682900527296927, 0.136580105459385, 0.341450263648464, -0.478030369107849, 
0.136580105459385, 0.136580105459385, -0.273160210918771, 0.546320421837542, 
-0.273160210918771, -0.273160210918771, -0.273160210918771, 0.75119058002662, 
-0.887770685486005, -0.887770685486005, -0.0682900527296927, 
-0.478030369107849, -0.0682900527296927, 0.75119058002662, -0.273160210918771, 
0.136580105459385, -0.478030369107849, -0.273160210918771, 0.136580105459385, 
0.75119058002662, 0.546320421837542, -0.478030369107849, -0.273160210918771, 
-0.273160210918771, 0.136580105459385, -0.273160210918771, -0.0682900527296927, 
0.75119058002662, 0.136580105459385), resp.var = c(2L, 1L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 1L, 3L, 1L, 0L, 1L, 0L, 1L, 2L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 2L, 
0L, 3L, 2L, 0L, 2L, 2L, 0L, 0L, 0L, 1L, 1L, 3L, 1L, 2L, 0L, 1L, 
0L, 0L, 1L, 0L, 2L, 0L, 2L, 4L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 
3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 
0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 2L, 0L, 1L, 0L, 4L, 1L, 0L, 
1L, 1L, 0L, 0L, 0L, 1L, 3L, 0L, 2L, 0L, 0L, 2L, 1L, 0L, 0L, 2L, 
0L, 0L, 0L, 2L, 0L, 0L, 3L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 3L, 1L, 
1L, 2L, 0L, 2L, 0L, 2L, 2L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 2L, 2L, 1L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L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 
1L, 2L, 0L, 2L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 
0L, 0L, 3L, 2L, 2L, 0L, 1L, 0L, 5L, 0L, 4L, 2L, 0L, 3L, 0L, 0L, 
1L, 1L, 0L, 0L, 0L, 2L, 0L, 1L, 0L, 3L, 0L, 2L, 0L, 0L, 0L, 2L, 
0L), rand.eff = c(37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 
37L, 37L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 
43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L)), .Names = c("cat.var", 
"cont.var", "resp.var", "rand.eff"), row.names = c(NA, 500L), class = "data.frame")

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 ? (请注意,这是一个cont.var==0模型规范 - 强制所有类别在cont.var==0处具有相同的值。您的意思是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: 现在使用Poisson GLM(但不包含随机效应),并显示数据点:

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: 下一位需要lme4的开发(r- lme4 )版本,它有一个predict方法:

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) 预测人口水平( REform=NA ),线性预测器(logit)量表(这是你在图上获得直线的唯一方法)

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. jtools包( CRAN链接 )可以使这种模型的绘图非常简单。 I'm the developer of that package. 我是那个包的开发者。

We will fit the model like Ben did in his answer: 我们将像Ben在答案中所做的那样适合模型:

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: 使用jtools我们只需使用interact_plot函数:

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" ). 默认情况下,它会在响应比例上绘制,但您可以使用outcome.scale = "link"参数(默认为"response" )将其绘制在线性比例上。

The effects package has support for lme4 models, and should be able to do what you want. 效果包支持lme4模型,应该能够做你想要的。

effects: Effect Displays for Linear, Generalized Linear, and Other Models 效果:线性,广义线性和其他模型的效果显示

Graphical and tabular effect displays, eg, of interactions, for various statistical models with linear predictors. 用于具有线性预测器的各种统计模型的图形和表格效果显示,例如,相互作用。

It also comes with two slightly outdated papers (you can think of them as vignettes). 它还附带了两篇略显过时的论文 (你可以把它们想象成小插曲)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM