Plotting Random Effects of Mixed Models

Daniel Lüdecke


This document shows examples for sjp.lmer(), especially the plot-types for plotting random effects. For other plot-types like effect-plots or predictions, see this vignette.

# load packages

# load sample data set.

Plotting random effects of linear mixed effects models

sjp.lmer() plots effects of merMod objects, which were fitted using the lmer() function of the lme4 package.

First, we need fit a sample model.

# fit model
fit <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)

# prepare group variable
efc$grp = as.factor(efc$e15relat)
levels(x = efc$grp) <- get_labels(efc$e15relat)
# data frame for fitted model
mydf <- data.frame(neg_c_7 = efc$neg_c_7,
                   sex = to_factor(efc$c161sex),
                   c12hour = efc$c12hour,
                   barthel = efc$barthtot,
                   grp = efc$grp)
# fit 2nd model
fit2 <- lmer(neg_c_7 ~ sex + c12hour + barthel + (1 | grp), data = mydf)

By default, random effects are plotted. In this example, the random effects of random intercept and random coefficient(s) are plotted as an integrated (faceted plot.) Note that the y.offset argument is used to adjust the value label position. Depending on text size and screen resolution, the default position of text labels may vary.

sjp.lmer(fit, y.offset = .4)

With the sort.est argument you can specify the random intercept or any random coefficient in order to sort the effects accordingly. Since the grouping levels in a faceted plot define the x-axis for each facet, sorting can only be applied to one coefficient or the intercept. If all random intercepts and random coefficients should be sorted, turn faceting off and use sort.est = "sort.all".

# sort all predictors
         facet.grid = FALSE,
         sort.est = "sort.all",
         y.offset = .4)

Plotting fixed effects slopes for each random intercept (group levels)

To get a better picture of the linear relationship between fixed effects and response depending on the grouping levels (random intercepts), you can plot straight slope lines (ablines) for each coefficient with varying random intercepts.

Basically, the formula is b0 + b0[r1-rn] + bi * xi (where xi is the estimate of fixed effects, b0 is the intercept of the fixed effects and b0[r1-rn] are all random intercepts).

Use type = "ri.slope" for this kind of plots. You can select specific grouping levels by their names (or index number) using the vars-argument. In this case, only fixed effects indicates in vars will be plotted.

# random intercepts
#> $grp
#>                         (Intercept)
#> spouse/partner           0.62318882
#> child                    0.42390010
#> sibling                 -0.05435509
#> daughter or son -in-law  0.05759517
#> ancle/aunt              -0.10111187
#> nephew/niece            -0.55060420
#> cousin                  -0.11383605
#> other, specify          -0.28477688
# fixed effects
#>  (Intercept)         sex2      c12hour      barthel 
#> 14.135947301  0.478500190  0.003365667 -0.047946653
# plot fixed effects depending on group levels
sjp.lmer(fit2, vars = "c12hour", type = "ri.slope")

In non-faceted plots, grouping levels might be difficult to distinguish. However, you can emphasize specific groups with emph.grp. Note that emph.grp only works in non-faceted plots (i.e. facet.grid = FALSE)! Remaining (non-emphasized) groups have a light grey color.

# plot fixed effects depending on group levels
# emphasize group levels 1, 2 and 5
         type = "ri.slope", 
         vars = "c12hour", 
         emph.grp = c(1, 2, 5), 
         facet.grid = FALSE)

Plotting random slopes depending on random intercepts

Use this plot type to visualize the random parts of random slope-intercept (or repeated measure) models. When having too many groups, use the sample.n argument to randomly select a specific amount of subjects.

# plot random-slope-intercept
sjp.lmer(fit, type = "rs.ri", vars = "c12hour", sample.n = 15)

If sample.n is a vector of length greater than one, the specifc “subjects” indicated by sample.n are plotted.

# plot random-slope-intercept, plot subjects 1, 5 and 7.
sjp.lmer(fit, type = "rs.ri", 
         sample.n = c(1, 5, 7),
         show.legend = TRUE)

qq-plot of random effects

Another diagnostic plot is the qq-plot for random effects. Use type = "re.qq" to plot random against standard quantiles. The dots should be plotted along the line.

# plot qq-plot of random effects
sjp.lmer(fit2, type = "re.qq")

If you have other random effects, like random coefficients, qq-plots for these effects are plotted as well. We refer to the first model to demonstrate this.

# plot qq-plot of random effects
sjp.lmer(fit, type = "re.qq")