Learn R Programming

sjPlot (version 2.0.0)

sjp.lmer: Plot estimates, predictions or effects of linear mixed effects models

Description

By default, this function plots estimates (coefficients) with confidence intervalls of either fixed effects or random effects of linear mixed effects models (that have been fitted with the lmer-function of the lme4-package). Furhermore, this function also plot predicted values or diagnostic plots.

Usage

sjp.lmer(fit, type = "re", vars = NULL, ri.nr = NULL,
  remove.estimates = NULL, emph.grp = NULL, sample.n = NULL,
  poly.term = NULL, sort.est = NULL, title = NULL, axis.labels = NULL,
  axis.title = NULL, geom.size = NULL, geom.colors = "Set1",
  show.values = TRUE, show.p = TRUE, show.ci = FALSE,
  show.legend = FALSE, show.loess = FALSE, show.loess.ci = FALSE,
  show.intercept = TRUE, string.interc = "(Intercept)", p.kr = TRUE,
  point.alpha = 0.2, scatter.plot = TRUE, fade.ns = FALSE,
  axis.lim = NULL, digits = 2, vline.type = 2, vline.color = "grey70",
  facet.grid = TRUE, free.scale = FALSE, y.offset = 0.1,
  prnt.plot = TRUE, ...)

Arguments

Value

(Insisibily) returns
  • the ggplot-object (plot), iftype = "fe"or iftype = "re"andfacet.grid = TRUE). Multiple plots (type = "re"and iffacet.grid = FALSE) are returned in the objectplot.list.
  • a list of ggplot-objects (plot.list). seeplotfor details.
  • a data framedatawith the data used to build the ggplot-object(s).

Details

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

See Also

http://www.strengejacke.de/sjPlot/sjp.lmer/{sjPlot manual: sjp.lmer}

Examples

Run this code
# fit model
library(lme4)
fit <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)

# simple plot
sjp.lmer(fit)

# plot fixed effects
sjp.lmer(fit, type = "fe")

# sort by predictor Days
sjp.lmer(fit, sort.est = "Days")

# plot each predictor as own plot
# sort each plot
sjp.lmer(fit, facet.grid = FALSE, sort.est = "sort.all")

library(sjmisc)
data(efc)
# 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 = as.numeric(efc$neg_c_7),
                   sex = as.factor(efc$c161sex),
                   c12hour = as.numeric(efc$c12hour),
                   barthel = as.numeric(efc$barthtot),
                   grp = efc$grp)
# fit lmer
fit <- lmer(neg_c_7 ~ sex + c12hour + barthel + (1|grp), data = mydf)

# plot fixed effects
sjp.lmer(fit, type = "fe")

sjp.lmer(fit, type = "fe.std", sort.est = TRUE)

# plot fixed effects slopes for each random intercept, 
# but only for coefficient "c12hour"
sjp.lmer(fit, type = "ri.slope", vars = "c12hour")

# highlight specific grouping levels, in this case we compare 
# spouses, children and children-in-law
sjp.lmer(fit, type = "ri.slope", emph.grp = c(1, 2, 4), vars = "c12hour")

# plotting polynomial terms
# check linear relation between predictors and response
sjp.lmer(fit, type = "fe.slope", show.loess = TRUE)

# "barthel" does not seem to be linear correlated to response
# try to find appropiate polynomial. Grey line (loess smoothed)
# indicates best fit. Looks like x^4 has the best fit,
# however, x^2 seems to be suitable according to p-values.
sjp.poly(fit, "barthel", 2:4, scatter.plot = FALSE)

# fit new model
fit <- lmer(neg_c_7 ~ sex + c12hour + barthel +
            I(barthel^2) + (1|grp), data = mydf)

# plot marginal effects of polynomial term
sjp.lmer(fit, type = "poly", poly.term = "barthel")

# lme4 complaints about scale of polynomial term, so
# try centering this predictor
mydf$barthel_s <- scale(mydf$barthel, center = TRUE, scale = TRUE)

# re-fit model
fit_s <- lmer(neg_c_7 ~ sex + c12hour + barthel_s +
              I(barthel_s^2) + (1|grp), data = mydf)

# plot marginal effects of centered, scaled polynomial term
sjp.lmer(fit_s, type = "poly", poly.term = "barthel_s")

# scaling also improved p-values
sjt.lmer(fit, fit_s)

# plotting predicted values for response
# conditioned on random effects
sjp.lmer(fit, type = "pred", vars = "c12hour")

# grouped, for fixed effects only
sjp.lmer(fit, type = "pred.fe", vars = c("c12hour", "sex"))

# grouped, for fixed effects only, non-facted
sjp.lmer(fit, type = "pred.fe", vars = c("c12hour", "sex"), 
         facet.grid = FALSE, show.ci = FALSE)

Run the code above in your browser using DataLab