ggstatsplot (version 0.0.11)

ggcoefstats: Model coefficients for fitted models with the model summary as a caption.

Description

Model coefficients for fitted models with the model summary as a caption.

Usage

ggcoefstats(x, output = "plot", statistic = NULL, scales = NULL,
  conf.method = "Wald", conf.type = "Wald", component = "survival",
  bf.message = FALSE, d = "norm", d.par = c(0, 0.3),
  tau = "halfcauchy", tau.par = 0.5, sample = 10000,
  summarize = "integrate", p.kr = TRUE, p.adjust.method = "none",
  coefficient.type = c("beta", "location", "coefficient"),
  by.class = FALSE, effsize = "eta", partial = TRUE, nboot = 500,
  meta.analytic.effect = FALSE, point.color = "blue", point.size = 3,
  point.shape = 16, conf.int = TRUE, conf.level = 0.95,
  se.type = "nid", k = 2, k.caption.summary = 0,
  exclude.intercept = TRUE, exponentiate = FALSE,
  errorbar.color = "black", errorbar.height = 0,
  errorbar.linetype = "solid", errorbar.size = 0.5, vline = TRUE,
  vline.color = "black", vline.linetype = "dashed", vline.size = 1,
  sort = "none", xlab = "regression coefficient", ylab = "term",
  title = NULL, subtitle = NULL, stats.labels = TRUE,
  caption = NULL, caption.summary = TRUE, stats.label.size = 3,
  stats.label.fontface = "bold", stats.label.color = NULL,
  label.r = 0.15, label.size = 0.25, label.box.padding = 1,
  label.label.padding = 0.25, label.point.padding = 0.5,
  label.segment.color = "grey50", label.segment.size = 0.5,
  label.segment.alpha = NULL, label.min.segment.length = 0.5,
  label.force = 1, label.max.iter = 2000, label.nudge.x = 0,
  label.nudge.y = 0, label.xlim = c(NA, NA), label.ylim = c(NA, NA),
  label.direction = "y", package = "RColorBrewer", palette = "Dark2",
  direction = 1, ggtheme = ggplot2::theme_bw(),
  ggstatsplot.layer = TRUE, messages = FALSE, return = NULL, ...)

Arguments

x

A model object to be tidied with broom::tidy, or a tidy data frame containing results. If a data frame is to be plotted, it must contain columns named term (names of predictors), or estimate (corresponding estimates of coefficients or other quantities of interest). Other optional columns are conf.low and conf.high (for confidence intervals); p.value. It is important that all term names should be unique.

output, return

Character describing the expected output from this function: "plot" (visualization of regression coefficients) or "tidy" (tidy dataframe of results from broom::tidy) or "glance" (object from broom::glance) or "augment" (object from broom::augment).

statistic

Which statistic is to be displayed (either "t" or "f"or "z") in the label. This is especially important if the x argument in ggcoefstats is a dataframe in which case the function wouldn't know what kind of model it is dealing with.

scales

scales on which to report the variables: for random effects, the choices are ‘"sdcor"’ (standard deviations and correlations: the default if scales is NULL) or ‘"vcov"’ (variances and covariances). NA means no transformation, appropriate e.g. for fixed effects.

conf.method

Character describing method for computing confidence intervals (for more, see ?lme4::confint.merMod and ?broom.mixed::tidy.brmsfit). This argument has different defaults depending on the model object. For the merMod class model objects (lmer, glmer, nlmer, etc.), the default is "Wald" (other options are: "profile", "boot"). For MCMC or brms fit model objects (Stan, JAGS, etc.), the default is "quantile", while the only other options is "HPDinterval".

conf.type

Whether to use "profile" or "Wald" confidendence intervals, passed to the type argument of ordinal::confint.clm(). Defaults to "profile".

component

Character specifying whether to tidy the survival or the longitudinal component of the model. Must be either "survival" or "longitudinal". Defaults to "survival".

bf.message

Logical that decides whether results from running a Bayesian meta-analysis assuming that the effect size d varies across studies with standard deviation t (i.e., a random-effects analysis) should be displayed in caption. Defaults to FALSE.

d

type of prior for mean effect \(d\) (see prior)

d.par

prior parameters for \(d\)

tau

type of prior for standard deviation of study effects \(\tau\) in random-effects meta-analysis (i.e., the SD of d across studies; see prior)

tau.par

prior parameters for \(\tau\)

sample

number of samples in JAGS after burn-in and thinning (see run.jags). Samples are used to get posterior estimates for each study effect (which will show shrinkage). Only works for priors defined in prior.

summarize

whether and to compute parameter summaries (mean, median, SD, 95% quantile interval, HPD interval). If summarize = "integrate", numerical integration is used (which is precise but can require some seconds of computing time), summarize = "jags" summarizes the JAGS samples, and summarize = "none" suppresses parameter summaries.

p.kr

Logical, if TRUE, the computation of p-values for lmer is based on conditional F-tests with Kenward-Roger approximation for the df. For details, see ?sjstats::p_value.

p.adjust.method

Adjustment method for p-values for multiple comparisons. Possible methods are: "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none". Default is no correction ("none"). This argument is relevant for multiplicity correction for multiway ANOVA designs (see, Cramer et al., 2015).

coefficient.type

Relevant only for ordinal regression models (clm , clmm, "svyolr", and polr), this argument decides which parameters are display in the plot. Available parameters are: parameter that measures the intercept, i.e. the log-odds distance between response values ("alpha"); effects on the location ("beta"); or effects on the scale ("zeta"). For clm and clmm models, by default, only "beta" (a vector of regression parameters) parameters will be show. Other options are "alpha" (a vector of threshold parameters) or "both". For polr models, by default, only "coefficient" will be shown. Other option is to show "zeta" parameters. Note that, from broom 0.7.0 onward, coefficients will be renamed and "intercept" type coefficients will correspond to "alpha" parameters, "location" type coefficients will correspond to "beta" parameters, and "scale" type coefficients will correspond to "zeta" parameters.

by.class

A logical indicating whether or not to show performance measures broken down by class. Defaults to FALSE. When by.class = FALSE only returns a tibble with accuracy and kappa statistics. Mostly relevant for an object of class "confusionMatrix".

effsize

Character describing the effect size to be displayed: "eta" (default) or "omega". This argument is relevant only for models objects of class aov, anova, and aovlist.

partial

Logical that decides if partial eta-squared or omega-squared are returned (Default: TRUE). If FALSE, eta-squared or omega-squared will be returned. Valid only for objects of class aov, anova, or aovlist.

nboot

Number of bootstrap samples for confidence intervals for partial eta-squared and omega-squared (Default: 500). This argument is relevant only for models objects of class aov, anova, and aovlist.

meta.analytic.effect

Logical that decides whether subtitle for meta-analysis via linear (mixed-effects) models - as implemented in the metafor package - is to be displayed (default: FALSE). If TRUE, input to argument subtitle will be ignored. This will be mostly relevant if a data frame with estimates and their standard errors is entered as input to x argument.

point.color

Character describing color for the point (Default: "blue").

point.size

Numeric specifying size for the point (Default: 3).

point.shape

Numeric specifying shape to draw the points (Default: 16 (a dot)).

conf.int

Logical. Decides whether to display confidence intervals as error bars (Default: TRUE).

conf.level

Numeric deciding level of confidence intervals (Default: 0.95). For MCMC model objects (Stan, JAGS, etc.), this will be probability level for CI.

se.type

Character specifying the method used to compute standard standard errors for quantile regression (Default: "nid"). To see all available methods, see quantreg::summary.rq().

k

Number of decimal places expected for results displayed in labels (Default : k = 2).

k.caption.summary

Number of decimal places expected for results displayed in captions (Default : k.caption.summary = 0).

exclude.intercept

Logical that decides whether the intercept should be excluded from the plot (Default: TRUE).

exponentiate

If TRUE, the x-axis will be logarithmic (Default: FALSE).

errorbar.color

Character deciding color of the error bars (Default: "black").

errorbar.height

Numeric specifying the height of the error bars (Default: 0).

errorbar.linetype

Line type of the error bars (Default: "solid").

errorbar.size

Numeric specifying the size of the error bars (Default: 0.5).

vline

Decides whether to display a vertical line (Default: "TRUE").

vline.color

Character specifying color of the vertical line (Default: "black").

vline.linetype

Character specifying line type of the vertical line (Default: "dashed").

vline.size

Numeric specifying the size of the vertical line (Default: 1).

sort

If "none" (default) do not sort, "ascending" sort by increasing coefficient value, or "descending" sort by decreasing coefficient value.

xlab

Label for x axis variable (Default: "estimate").

ylab

Label for y axis variable (Default: "term").

title

The text for the plot title.

subtitle

The text for the plot subtitle. The input to this argument will be ignored if meta.analytic.effect is set to TRUE.

stats.labels

Logical. Decides whether the statistic and p-values for each coefficient are to be attached to each dot as a text label using ggrepel (Default: TRUE).

caption

The text for the plot caption.

caption.summary

Logical. Decides whether the model summary should be displayed as a cation to the plot (Default: TRUE). Color of the line segment. Defaults to the same color as the text.

stats.label.size, stats.label.fontface, stats.label.color

Aesthetics for the labels. Defaults: 3, "bold",NULL, resp. If stats.label.color is NULL, colors will be chosen from the specified package (Default: "RColorBrewer") and palette (Default: "Dark2").

label.r,

Radius of rounded corners, as unit or number. Defaults to 0.15. (Default unit is lines).

label.size

Size of label border, in mm. Defaults to 0.25.

label.box.padding

Amount of padding around bounding box, as number. Defaults to 1. (Default unit is lines).

label.label.padding

Amount of padding around label, as number. Defaults to 0.25. (Default unit is lines).

label.point.padding

Amount of padding around labeled point, as number. Defaults to 0. (Default unit is lines).

label.segment.color

Color of the line segment (Default: "grey50").

label.segment.size

Width of line segment connecting the data point to the text label, in mm. Defaults to 0.5.

label.segment.alpha

Transparency of the line segment. Defaults to the same transparency as the text.

label.min.segment.length

Skip drawing segments shorter than this. Defaults to 0.5. (Default unit is lines).

label.force

Force of repulsion between overlapping text labels. Defaults to 1.

label.max.iter

Maximum number of iterations to try to resolve overlaps. Defaults to 2000.

label.nudge.x, label.nudge.y

Horizontal and vertical adjustments to nudge the starting position of each text label. Defaults to 0.

label.xlim, label.ylim

Limits for the x and y axes. Text labels will be constrained to these limits. By default, text labels are constrained to the entire plot area. Defaults to c(NA, NA).

label.direction

Character ("both", "x", or "y") -- direction in which to adjust position of labels (Default: "y").

package

Name of package from which the palette is desired as string or symbol.

palette

Name of palette as string or symbol.

direction

Either 1 or -1. If -1 the palette will be reversed.

ggtheme

A function, ggplot2 theme name. Default value is ggplot2::theme_bw(). Any of the ggplot2 themes, or themes from extension packages are allowed (e.g., ggthemes::theme_fivethirtyeight(), hrbrthemes::theme_ipsum_ps(), etc.).

ggstatsplot.layer

Logical that decides whether theme_ggstatsplot theme elements are to be displayed along with the selected ggtheme (Default: TRUE).

messages

Decides whether messages references, notes, and warnings are to be displayed (Default: TRUE).

...

Additional arguments to tidying method.

Value

Plot with the regression coefficients' point estimates as dots with confidence interval whiskers.

References

https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggcoefstats.html

Examples

Run this code
# NOT RUN {
# for reproducibility
set.seed(123)

# -------------- with model object --------------------------------------

# model object
mod <- lm(formula = mpg ~ cyl * am, data = mtcars)

# to get a plot
ggstatsplot::ggcoefstats(x = mod, output = "plot")

# to get a tidy dataframe
ggstatsplot::ggcoefstats(x = mod, output = "tidy")

# to get a glance summary
ggstatsplot::ggcoefstats(x = mod, output = "glance")

# to get augmented dataframe
ggstatsplot::ggcoefstats(x = mod, output = "augment")

# -------------- with custom dataframe -----------------------------------
# }
# NOT RUN {
# creating a dataframe
df <-
  structure(
    list(
      term = structure(
        c(3L, 4L, 1L, 2L, 5L),
        .Label = c(
          "Africa",
          "Americas", "Asia", "Europe", "Oceania"
        ),
        class = "factor"
      ),
      estimate = c(
        0.382047603321706,
        0.780783111514665,
        0.425607573765058,
        0.558365541235078,
        0.956473848429961
      ),
      std.error = c(
        0.0465576338644502,
        0.0330218199731529,
        0.0362834986178494,
        0.0480571500648261,
        0.062215818388157
      ),
      statistic = c(
        8.20590677855356,
        23.6444603038067,
        11.7300588415607,
        11.6187818146078,
        15.3734833553524
      ),
      conf.low = c(
        0.290515146096969,
        0.715841986960399,
        0.354354575031406,
        0.46379116008131,
        0.827446138277154
      ),
      conf.high = c(
        0.473580060546444,
        0.845724236068931,
        0.496860572498711,
        0.652939922388847,
        1.08550155858277
      ),
      p.value = c(
        3.28679518728519e-15,
        4.04778497135963e-75,
        7.59757330804449e-29,
        5.45155840151592e-26,
        2.99171217913312e-13
      ),
      df.residual = c(
        394L, 358L, 622L,
        298L, 22L
      )
    ),
    row.names = c(NA, -5L),
    class = c(
      "tbl_df",
      "tbl", "data.frame"
    )
  )

# plotting the dataframe
ggstatsplot::ggcoefstats(
  x = df,
  statistic = "t",
  meta.analytic.effect = TRUE,
  bf.message = TRUE,
  k = 3
)
# }
# NOT RUN {
# -------------- getting model summary ------------------------------

# model
library(lme4)
lmm1 <- lme4::lmer(
  formula = Reaction ~ Days + (Days | Subject),
  data = sleepstudy
)

# dataframe with model summary
ggstatsplot::ggcoefstats(x = lmm1, output = "glance")

# -------------- getting augmented dataframe ------------------------------

# setup
set.seed(123)
library(survival)

# fit
cfit <-
  survival::coxph(formula = Surv(time, status) ~ age + sex, data = lung)

# augmented dataframe
ggstatsplot::ggcoefstats(
  x = cfit,
  data = lung,
  output = "augment",
  type.predict = "risk"
)
# }

Run the code above in your browser using DataCamp Workspace