# NOT RUN {
# 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)
library(sjlabelled)
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)
sjp.lmer(fit, type = "fe.std", sort.est = TRUE)
# 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")
# }
# NOT RUN {
# 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, show.scatter = 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 <- sjmisc::std(mydf$barthel)
# 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)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab