This document shows examples for using the
sjp.lm function of the sjPlot package.
(back to table of content)
Please refer to this document.
library(sjPlot) library(sjmisc) data(efc) # set basic theme options sjp.setTheme(theme = "forestgrey", axis.title.size = .85, axis.textsize = .85, legend.size = .8, geom.label.size = 3.5)
First, we need to fit a linear model and create a vector with labels for the predictors.
# fit the model fit <- lm(neg_c_7 ~ c12hour + c160age + e42dep + tot_sc_e + c172code, data = efc)
sjp.lm function you can plot the beta coefficients with confidence intervals as so called “forest plots”.
As you can see in the above figure, value labels are set automatically. This works because value and variable labels are added to each variable as additional attributes. You can use e.g.
set_labels to manually add label attributes to vectors. If you import data from SPSS etc., the labels are automatically attached as attributes. See this document for more details.
You can plot the standardized beta coefficients with
type = "std".
sjp.lm(fit, type = "std")
By default, the optimal axis limits are calculated upon the range of lowest and highest confidence interval. If you prefer a symmetrical axis, use the
sjp.lm(fit, breakLabelsAt = 30, axis.lim = c(-1, 1))
Some functions like
sjp.lm offer specific customizations of plots for elements that only apply to these functions (e.g. error bars). This is shown in the following figure.
sjp.setTheme(geom.errorbar.size = 1, geom.errorbar.linetype = 2, axis.title.size = .85, axis.textsize = .85) sjp.lm(fit, geom.size = 5)
Marginal effects plots can be obtained with
type = "eff".
sjp.lm(fit, type = "eff")
type = "pred", prediction from the fitted model can be obtained. The predicted values of the response, related to specific model predictors. This plot type requires the
vars argument to select specific terms that should be used for the x-axis and - optional - as grouping factor. Hence,
vars must be a character vector with the names of one or two model predictors.
sjp.lm(fit, type = "pred", vars = "c12hour")
Predicted values can also be grouped by another predictor. In this case, you have to provide two predictor names for the
sjp.lm(fit, type = "pred", vars = c("c12hour", "e42dep"))
facet.grid argument to avoid faceted plots.
sjp.lm(fit, type = "pred", vars = c("c12hour", "e42dep"), facet.grid = FALSE)
If you are not sure if a predictor might be improper for linear models (because of skewness or whatever), you can investigate each single predictor of the model with the parameter
type = "slope". This call requires at least the fitted model. The result are several figures (one for each predictor) of linear models where each predictor alone is fitted agains the response.
This function plots the linear fitted line, a loess fitted line and the standard error range by default. The better the linear and loess fitted line “match” (overlap), the better is the linear relationship between predictor and response.
sjp.lm(fit, type = "slope", show.loess = TRUE)
You can assess the model assumptions of your fitted model with the parameter
type = "ma". This call produces some information and several plots:
sjp.lm(fit, type = "ma")
## Removed 7 cases during 1 step(s). ## R^2 / adj. R^2 of original model: 0.159176 / 0.154093 ## R^2 / adj. R^2 of updated model: 0.179072 / 0.174067 ## AIC of original model: 4497.751285 ## AIC of updated model: 4353.330494