report_lm_with_std: Create a summary table comparing standardized and...

View source: R/mod_tables.R

report_lm_with_stdR Documentation

Create a summary table comparing standardized and non-standardized linear models

Description

This function creates a summary table for lm models (including mice::mira objects containing lm-models) that shows a standardized and non-standardized version of the model side-by-side. Several pairs of such models can be compared side-by-side.

Usage

report_lm_with_std(
  mod,
  mod_std,
  conf_level = 0.95,
  coef_renames = NULL,
  fmt = "%.2f",
  statistic_vertical = FALSE,
  filename = NULL,
  model_names = NULL,
  show_nimp = FALSE,
  R2_change = FALSE,
  notes = list(NULL),
  apa_style = TRUE,
  stars = std_stars_pad,
  ...
)

Arguments

mod

A lm-model/mira object of lm models, with variables not standardized (or a list of such models)

mod_std

A lm-model/mira object of lm models, with standardized variables. Can be created with lm_std() (or a list of such models)

conf_level

Confidence level to use for confidence intervals, defaults to .95

coef_renames

A named character vector with new names for the coefficients or a tibble as provided by get_coef_rename_tribble() for variables. If NULL, then the coefficients are not renamed.

fmt

how to format numeric values: integer, user-supplied function, or modelsummary function.

  • Integer: Number of decimal digits

  • User-supplied functions:

    • Any function which accepts a numeric vector and returns a character vector of the same length.

  • modelsummary functions:

    • fmt = fmt_significant(2): Two significant digits (at the term-level)

    • fmt = fmt_decimal(digits = 2, pdigits = 3): Decimal digits for estimate and p values

    • fmt = fmt_sprintf("%.3f"): See ?sprintf

    • fmt = fmt_term("(Intercept)" = 1, "X" = 2): Format terms differently

    • fmt = fmt_statistic("estimate" = 1, "r.squared" = 6): Format statistics differently.

    • fmt = fmt_identity(): unformatted raw values

  • string:

  • Note on LaTeX output: To ensure proper typography, all numeric entries are enclosed in the ⁠\num{}⁠ command, which requires the siunitx package to be loaded in the LaTeX preamble. This behavior can be altered with global options. See the 'Details' section.

statistic_vertical

Should standard errors and CIs be shown below coefficients? Defaults to horizontal layout

filename

the file name to create a HTML file on disk.

model_names

If several pairs of models are to be plotted side by side, indicate the label for each pair here

show_nimp

Logical - DEFUNCT. If mira objects are passed, this determines whether the number of imputations will be reported as a model statistic

R2_change

Logical. Report R2 change and F-test to compare models.

notes

List of notes to append to bottom of table. An explanation of significance stars is automatically added. If the std models were run with a helper function in this package, a note regarding the standardization is also automatically added.

apa_style

Logical, should APA-style formatting be applied

stars

Named vector of significance stars and their thresholds, check timesaveR:::std_stars_pad for default.

...

Arguments passed on to modelsummary::modelsummary

coef_map

character vector. Subset, rename, and reorder coefficients. Coefficients omitted from this vector are omitted from the table. The order of the vector determines the order of the table. coef_map can be a named or an unnamed character vector. If coef_map is a named vector, its values define the labels that must appear in the table, and its names identify the original term names stored in the model object: c("hp:mpg"="HPxM/G"). See Examples section below.

coef_omit

integer vector or regular expression to identify which coefficients to omit (or keep) from the table. Positive integers determine which coefficients to omit. Negative integers determine which coefficients to keep. A regular expression can be used to omit coefficients, and perl-compatible "negative lookaheads" can be used to specify which coefficients to keep in the table. Examples:

  • c(2, 3, 5): omits the second, third, and fifth coefficients.

  • c(-2, -3, -5): negative values keep the second, third, and fifth coefficients.

  • "ei": omit coefficients matching the "ei" substring.

  • "^Volume$": omit the "Volume" coefficient.

  • "ei|rc": omit coefficients matching either the "ei" or the "rc" substrings.

  • "^(?!Vol)": keep coefficients starting with "Vol" (inverse match using a negative lookahead).

  • "^(?!.*ei)": keep coefficients matching the "ei" substring.

  • "^(?!.*ei|.*pt)": keep coefficients matching either the "ei" or the "pt" substrings.

  • See the Examples section below for complete code.

coef_rename

logical, named or unnamed character vector, or function

  • Logical: TRUE renames variables based on the "label" attribute of each column. See the Example section below.

  • Unnamed character vector of length equal to the number of coefficients in the final table, after coef_omit is applied.

  • Named character vector: Values refer to the variable names that will appear in the table. Names refer to the original term names stored in the model object. Ex: c("hp:mpg"="hp X mpg")

  • Function: Accepts a character vector of the model's term names and returns a named vector like the one described above. The modelsummary package supplies a coef_rename() function which can do common cleaning tasks: modelsummary(model, coef_rename = coef_rename)

Value

A list with gt_tab (the gt-table object including the parts of the table that can be created with gt. This can be post-processed and formatted with functions in the gt-package, but does not include the lower part with model statistics, e.g., R^2.) and html_code (the code that creates the full table, and is used to render it in the Viewer).

Examples


# Standard lm model
mod1 <- lm(mpg ~ hp + wt, mtcars)

# Model with standardized coefficients

mod2 <- lm_std(mpg ~ hp + wt, mtcars)

report_lm_with_std(mod1, mod2)

LukasWallrich/timesaveR documentation built on Nov. 29, 2024, 4:47 a.m.