Learn R Programming

sjPlot (version 2.1.0)

sjp.glm: Plot estimates, predictions or effects of generalized linear models

Description

Plot odds or incident rate ratios with confidence intervalls as dot plot. Depending on the type argument, this function may also plot model assumptions for generalized linear models, or marginal effects (predicted probabilities or events).

Usage

sjp.glm(fit, type = "dots", vars = NULL, group.estimates = NULL, remove.estimates = NULL, sort.est = TRUE, title = NULL, legend.title = NULL, axis.labels = NULL, axis.title = "Odds Ratios", geom.size = NULL, geom.colors = "Set1", wrap.title = 50, wrap.labels = 25, axis.lim = NULL, grid.breaks = 0.5, trns.ticks = TRUE, show.intercept = FALSE, show.values = TRUE, show.p = TRUE, show.ci = FALSE, show.legend = FALSE, show.summary = FALSE, digits = 2, vline.type = 2, vline.color = "grey70", coord.flip = TRUE, y.offset = 0.15, facet.grid = TRUE, prnt.plot = TRUE, ...)

Arguments

fit
fitted generalized linear model (glm- or logistf-object).
type
type of plot. Use one of following:
vars
numeric vector with column indices of selected variables or a character vector with variable names of selected variables from the fitted model, which should be used to plot estimates, fixed effects slopes (for lmer) or probability or incidents curves (for glmer) of random intercepts.
group.estimates
numeric or character vector, indicating a group identifier for each estimate. Dots and confidence intervals of estimates are coloured according to their group association. See 'Examples'.
remove.estimates
character vector with coefficient names that indicate which estimates should be removed from the plot. remove.estimates = "est_name" would remove the estimate est_name. Default is NULL, i.e. all estimates are printed.
sort.est
logical, determines whether estimates should be sorted according to their values. If group.estimates is not NULL, estimates are sorted according to their group assignment.
title
character vector, used as plot title. Depending on plot type and function, will be set automatically. If title = "", no title is printed.
legend.title
character vector, used as title for the plot legend. Note that only some plot types have legends (e.g. type = "pred" or when grouping estimates with group.estimates).
axis.labels
character vector with labels used as axis labels. Optional argument, since in most cases, axis labels are set automatically.
axis.title
character vector of length one or two (depending on the plot function and type), used as title(s) for the x and y axis. If not specified, a default labelling is chosen.
geom.size
size resp. width of the geoms (bar width, line thickness or point size, depending on plot type and function). Note that bar and bin widths mostly need smaller values than dot sizes.
geom.colors
user defined color palette for geoms. If group.estimates is not specified, must either be vector with two color values or a specific color palette code (see 'Details' in sjp.grpfrq). Else, if group.estimates is specified, geom.colors must be a vector of same length as groups. See 'Examples'.
wrap.title
numeric, determines how many chars of the plot title are displayed in one line and when a line break is inserted.
wrap.labels
numeric, determines how many chars of the value, variable or axis labels are displayed in one line and when a line break is inserted.
axis.lim
numeric vector of length 2, defining the range of the plot axis. Depending on plot type, may effect either x- or y-axis, or both. For multiple plot outputs (e.g., from type = "eff" or type = "slope" in sjp.glm), axis.lim may also be a list of vectors of length 2, defining axis limits for each plot (only if non-faceted).
grid.breaks
numeric; sets the distance between breaks for the axis, i.e. at every grid.breaks'th position a major grid is being printed.
trns.ticks
logical, if TRUE, the grid lines have exponential distances (equidistant), i.e. they visually have the same distance from one panel grid to the next. If FALSE, grids are plotted on every grid.breaks's position, thus the grid lines become narrower with higher odds ratio values.
show.intercept
logical, if TRUE, the intercept of the fitted model is also plotted. Default is FALSE. For glm's, please note that due to exponential transformation of estimates, the intercept in some cases can not be calculated, thus the function call is interrupted and no plot printed.
show.values
logical, whether values should be plotted or not.
show.p
logical, adds significance levels to values, or value and variable labels.
show.ci
logical, if TRUE, depending on type, a confidence interval or region is added to the plot.
show.legend
logical, if TRUE, and depending on plot type and function, a legend is added to the plot.
show.summary
logical, if TRUE, a summary with model statistics is added to the plot.
digits
numeric, amount of digits after decimal point when rounding estimates and values.
vline.type
linetype of the vertical "zero point" line. Default is 2 (dashed line).
vline.color
color of the vertical "zero point" line. Default value is "grey70".
coord.flip
logical, if TRUE, the x and y axis are swapped.
y.offset
numeric, offset for text labels when their alignment is adjusted to the top/bottom of the geom (see hjust and vjust).
facet.grid
TRUE to arrange the lay out of of multiple plots in a grid of an integrated single plot. This argument calls facet_wrap or facet_grid to arrange plots. Use plot_grid to plot multiple plot-objects as an arranged grid with grid.arrange.
prnt.plot
logical, if TRUE (default), plots the results as graph. Use FALSE if you don't want to plot any graphs. In either case, the ggplot-object will be returned as value.
...
other arguments, passed down to the effect resp. allEffects function when type = "eff".

Value

(Insisibily) returns, depending on the plot type
  • The ggplot-object (plot). For multiple plots and if facet.grid = FALSE) a plot.list is returned.
  • A data frame data with the data used to build the ggplot-object(s), or a list of data frames (data.list).

Details

See Also

sjPlot manual: sjp.glm

Examples

Run this code
# prepare dichotomous dependent variable
swiss$y <- ifelse(swiss$Fertility < median(swiss$Fertility), 0, 1)

# fit model
fitOR <- glm(y ~ Education + Examination + Infant.Mortality + Catholic,
             family = binomial(link = "logit"), data = swiss)

# print Odds Ratios as dots
sjp.glm(fitOR)

# -------------------------------
# Predictors for negative impact of care. Data from 
# the EUROFAMCARE sample dataset
# -------------------------------
library(sjmisc)
data(efc)
# create binary response
y <- ifelse(efc$neg_c_7 < median(na.omit(efc$neg_c_7)), 0, 1)
# create data frame for fitted model
mydf <- data.frame(y = as.factor(y),
                   sex = efc$c161sex,
                   dep = to_factor(efc$e42dep),
                   barthel = efc$barthtot,
                   education = to_factor(efc$c172code))
# fit model
fit <- glm(y ~., data = mydf, family = binomial(link = "logit"))

# plot odds ratios
sjp.glm(fit, title = get_label(efc$neg_c_7))

# plot probability curves (relationship between predictors and response)
sjp.glm(fit, title = get_label(efc$neg_c_7), type = "slope")

# --------------------------
# grouping estimates
# --------------------------
sjp.glm(fit,  group.estimates = c(1, 2, 2, 2, 3, 4, 4))

# --------------------------
# model predictions, with selected model terms.
# 'vars' needs to be a character vector of length 1 or 2
# with names of model terms for x-axis and grouping factor.
# --------------------------
sjp.glm(fit, type = "pred", vars = "barthel")
# faceted, with ci
sjp.glm(fit, type = "pred", vars = c("barthel", "dep"), show.ci = TRUE)
# w/o facets
sjp.glm(fit, type = "pred", vars = c("barthel", "dep"), facet.grid = FALSE)

Run the code above in your browser using DataLab