Create beautiful and customizable tables to summarize several statistical
models side-by-side. This function supports dozens of statistical models,
and it can produce tables in HTML, LaTeX, Word, Markdown, Typst, PDF, PowerPoint,
Excel, RTF, JPG, or PNG. The appearance of the tables can be customized
extensively by specifying the output argument, and by using functions from
one of the supported table customization packages: tinytable, kableExtra, gt,
flextable, huxtable, DT. For more information, see the Details and Examples
sections below, and the vignettes on the modelsummary website:
https://modelsummary.com/
Usage
modelsummary(
models,
output = getOption("modelsummary_output", default = "default"),
fmt = getOption("modelsummary_fmt", default = 3),
estimate = getOption("modelsummary_estimate", default = "estimate"),
statistic = getOption("modelsummary_statistic", default = "std.error"),
vcov = getOption("modelsummary_vcov", default = NULL),
conf_level = getOption("modelsummary_conf_level", default = 0.95),
exponentiate = getOption("modelsummary_exponentiate", default = FALSE),
stars = getOption("modelsummary_stars", default = FALSE),
shape = getOption("modelsummary_shape", default = term + statistic ~ model),
coef_map = getOption("modelsummary_coef_map", default = NULL),
coef_omit = getOption("modelsummary_coef_omit", default = NULL),
coef_rename = getOption("modelsummary_coef_rename", default = FALSE),
gof_map = getOption("modelsummary_gof_map", default = NULL),
gof_omit = getOption("modelsummary_gof_omit", default = NULL),
gof_function = getOption("modelsummary_gof_function", default = NULL),
group_map = getOption("modelsummary_group_map", default = NULL),
add_columns = getOption("modelsummary_add_columns", default = NULL),
add_rows = getOption("modelsummary_add_rows", default = NULL),
align = getOption("modelsummary_align", default = NULL),
notes = getOption("modelsummary_notes", default = NULL),
title = getOption("modelsummary_title", default = NULL),
escape = getOption("modelsummary_escape", default = TRUE),
...
)Arguments
- models
a model, (named) list of models, or nested list of models.
Single model:
modelsummary(model)Unnamed list of models:
modelsummary(list(model1, model2))Models are labelled automatically. The default label style can be altered by setting a global option. See below.
Named list of models:
modelsummary(list("A"=model1, "B"=model2))Models are labelled using the list names.
Nested list of models:
When using the
shapeargument with "rbind", "rcollapse", or "cbind" values,modelscan be a nested list of models to display "panels" or "stacks" of regression models. See theshapeargument documentation and examples below.
- output
filename or object type (character string)
Supported filename extensions: .docx, .html, .tex, .md, .txt, .csv, .xlsx, .png, .jpg
Supported object types: "default", "html", "markdown", "latex", "latex_tabular", "typst", "data.frame", "tinytable", "gt", "kableExtra", "huxtable", "flextable", "DT", "jupyter". The "modelsummary_list" value produces a lightweight object which can be saved and fed back to the
modelsummaryfunction.The "default" output format can be set to "tinytable", "kableExtra", "gt", "flextable", "huxtable", "DT", or "markdown"
If the user does not choose a default value, the packages listed above are tried in sequence.
Session-specific configuration:
options("modelsummary_factory_default" = "gt")Persistent configuration:
config_modelsummary(output = "markdown")
Warning: Users should not supply a file name to the
outputargument if they intend to customize the table with external packages. See the 'Details' section.LaTeX compilation requires the
booktabsandsiunitxpackages, butsiunitxcan be disabled or replaced with global options. See the 'Details' section.
- fmt
how to format numeric values: integer, user-supplied function, or
modelsummaryfunction.Integer: Number of decimal digits
User-supplied functions:
Any function which accepts a numeric vector and returns a character vector of the same length.
modelsummaryfunctions:fmt = fmt_significant(2): Two significant digits (at the term-level)fmt = fmt_decimal(digits = 2, pdigits = 3): Decimal digits for estimate and p valuesfmt = fmt_sprintf("%.3f"): See?sprintffmt = fmt_term("(Intercept)" = 1, "X" = 2): Format terms differentlyfmt = fmt_statistic("estimate" = 1, "r.squared" = 6): Format statistics differently.fmt = fmt_identity(): unformatted raw values
string: Passing the string
sis equivalent to passingfmt_sprintf(s)Note on LaTeX output: To ensure proper typography, all numeric entries are enclosed in the
\num{}command, which requires thesiunitxpackage to be loaded in the LaTeX preamble. This behavior can be altered with global options. See the 'Details' section.
- estimate
a single string or a character vector of length equal to the number of models. Valid entries include any column name of the data.frame produced by
get_estimates(model), and strings with curly braces compatible with thegluepackage format. Examples:"estimate""{estimate} ({std.error}){stars}""{estimate} [{conf.low}, {conf.high}]"Numbers are automatically rounded and converted to strings. To let glue apply functions to numeric values, users must set
fmt=NULL. For more complex formatting, users are encouraged to use thefmtargument, which accepts custom functions.
- statistic
vector of strings or
gluestrings which select uncertainty statistics to report vertically below the estimate (ex: standard errors, confidence intervals, p values). NULL omits all uncertainty statistics."conf.int", "std.error", "statistic", "p.value", "conf.low", "conf.high", or any column name produced by
get_estimates(model)gluepackage strings with braces, with or without R functions, such as:"{p.value} [{conf.low}, {conf.high}]""Std.Error: {std.error}""{exp(estimate) * std.error}"
Notes:
The names of the
statisticare used a column names when using theshapeargument to display statistics as columns:statistic=c("p"="p.value", "["="conf.low", "]"="conf.high")
Some statistics are not supported for all models. See column names in
get_estimates(model), and visit the website to learn how to add custom statistics.Parentheses are added automatically unless the string includes
gluecurly braces{}.
- vcov
robust standard errors and other manual statistics. The
vcovargument accepts six types of input (see the 'Details' and 'Examples' sections below):NULL returns the default uncertainty estimates of the model object
string, vector, or (named) list of strings. "iid", "classical", and "constant" are aliases for
NULL, which returns the model's default uncertainty estimates. The strings "HC", "HC0", "HC1" (alias: "stata"), "HC2", "HC3" (alias: "robust"), "HC4", "HC4m", "HC5", "HAC", "NeweyWest", "Andrews", "panel-corrected", "outer-product", and "weave" use variance-covariance matrices computed using functions from thesandwichpackage, or equivalent method. "BS", "bootstrap", "residual", "mammen", "webb", "xy", "wild" use thesandwich::vcovBS(). The behavior of those functions can (and sometimes must) be altered by passing arguments tosandwichdirectly frommodelsummarythrough the ellipsis (...), but it is safer to define your own custom functions as described in the next bullet.function or (named) list of functions which return variance-covariance matrices with row and column names equal to the names of your coefficient estimates (e.g.,
stats::vcov,sandwich::vcovHC,function(x) vcovPC(x, cluster="country")).formula or (named) list of formulas with the cluster variable(s) on the right-hand side (e.g., ~clusterid).
named list of
length(models)variance-covariance matrices with row and column names equal to the names of your coefficient estimates.a named list of length(models) vectors with names equal to the names of your coefficient estimates. See 'Examples' section below. Warning: since this list of vectors can include arbitrary strings or numbers,
modelsummarycannot automatically calculate p values. Thestarsargument may thus use incorrect significance thresholds whenvcovis a list of vectors.
- conf_level
numeric value between 0 and 1. confidence level to use for confidence intervals. Setting this argument to
NULLdoes not extract confidence intervals, which can be faster for some models.- exponentiate
TRUE, FALSE, or logical vector of length equal to the number of models. If TRUE, the
estimate,conf.low, andconf.highstatistics are exponentiated, and thestd.erroris transformed toexp(estimate)*std.error. Theexponentiateargument is ignored for distributional random effects parameters (SD and Cor) and dispersions parameters.- stars
to indicate statistical significance
FALSE (default): no significance stars.
TRUE:
c("+" = .1, "*" = .05, "**" = .01, "***" = 0.001)Named numeric vector for custom stars such as
c('*' = .1, '+' = .05)Note: a legend will not be inserted at the bottom of the table when the
estimateorstatisticarguments use "glue strings" with{stars}.
- shape
NULL, formula, or string which determines the shape of a table.NULL: Default shape with terms in rows and models in columns.Formula: The left side determines what appears on rows, and the right side determines what appears on columns. The formula can include one or more group identifier(s) to display related terms together, which can be useful for models with multivariate outcomes or grouped coefficients (See examples section below). The group identifier(s) must be column names produced by:
get_estimates(model). The group identifier(s) can be combined with the term identifier in a single column by using the colon to represent an interaction. If an incomplete formula is supplied (e.g.,~statistic),modelsummarytries to complete it automatically. Goodness-of-fit statistics are only appended to the bottom of the table whenmodelis on the right hand side of the formula (i.e., columns). Potentialshapevalues include:term + statistic ~ model: defaultterm ~ model + statistic: statistics in separate columnsmodel + statistic ~ term: models in rows and terms in columnsterm + response + statistic ~ model: term and group id in separate columnsterm : response + statistic ~ model: term and group id in a single columnterm ~ response
String: "cbind", "rbind", "rcollapse"
"cbind": side-by-side models with autmoatic spanning column headers to group models (
tinytableonly feature)."rbind" or "rcollapse": "panels" or "stacks" of regression models.
the
modelsargument must be a (potentially named) nested list of models.
Unnamed nested list with 2 panels:
list(list(model1, model2), list(model3, model4))Named nested list with 2 panels:
list("Panel A" = list(model1, model2), "Panel B" = list(model3, model4))Named panels and named models:
list("Panel A" = list("(I)" = model1, "(II)" = model2), "Panel B" = list("(I)" = model3, "(II)" = model4))
"rbind": Bind the rows of independent regression tables
"rcollapse": Bind the rows of regression tables and create a panel at the bottom where we "collapse" goodness-of-fit statistics which are identical across models.
- 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_mapcan be a named or an unnamed character vector. Ifcoef_mapis 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"). Ifcoef_mapis an unnamed vector, its values must be raw variable names ifcoef_rename=FALSEand variable labels ifcoef_rename=TRUE. Seemodelsummary::get_estimatesto get the coefficient out of a model. 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. Note: renaming is done by the
parameterspackage at the extraction stage, before other arguments are applied likecoef_omit. Therefore, this only works for models with builtin support and not for custom models.Unnamed character vector of length equal to the number of coefficients in the final table, after
coef_omitis 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
modelsummarypackage supplies acoef_rename()function which can do common cleaning tasks:modelsummary(model, coef_rename = coef_rename)
- gof_map
rename, reorder, and omit goodness-of-fit statistics and other model information. This argument accepts 4 types of values:
NULL (default): the
modelsummary::gof_mapdictionary is used for formatting, and all unknown statistic are included.character vector: "all", "none", or a vector of statistics such as
c("rmse", "nobs", "r.squared"). Elements correspond to colnames in the data.frame produced byget_gof(model). Themodelsummary::gof_mapdefault dictionary is used to format and rename statistics.NA: excludes all statistics from the bottom part of the table.
data.frame with 3 columns named "raw", "clean", "fmt". Unknown statistics are omitted. See the 'Examples' section below. The
fmtcolumn in this data frame only accepts integers. For more flexibility, use a list of lists, as described in the next bullet.list of lists, each of which includes 3 elements named "raw", "clean", "fmt". Unknown statistics are omitted. The
fmtelement can be a string (?fmt_sprintf), numeric value (?fmt_decimal), or function which will be used to round/format the string in question. See the 'Examples section below'.
- gof_omit
string regular expression (perl-compatible) used to determine which statistics to omit from the bottom section of the table. A "negative lookahead" can be used to specify which statistics to keep in the table. Examples:
"IC": omit statistics matching the "IC" substring."BIC|AIC": omit statistics matching the "AIC" or "BIC" substrings."^(?!.*IC)": keep statistics matching the "IC" substring.
- gof_function
function which accepts a model object in the
modelargument and returns a 1-rowdata.framewith one custom goodness-of-fit statistic per column.- group_map
named or unnamed character vector. Subset, rename, and reorder coefficient groups specified a grouping variable specified in the
shapeargument formula. This argument behaves likecoef_map.- add_columns
a data.frame (or tibble) with the same number of rows as #' your main table. By default, rows are appended to the bottom of the table. You can define a "position" attribute of integers to set the columns positions. See Examples section below.
- add_rows
a data.frame (or tibble) with the same number of columns as your main table. By default, rows are appended to the bottom of the table. Positions can be defined using integers. In the
modelsummary()function (only), you can also use string shortcuts: "coef_start", "coef_end", "gof_start", "gof_end"attr(new_rows, 1:2)attr(new_rows, "gof_start")See Examples section below.
- align
A string with a number of characters equal to the number of columns in the table (e.g.,
align = "lcc"). Valid characters: l, c, r, d."l": left-aligned column
"c": centered column
"r": right-aligned column
"d": dot-aligned column. For LaTeX/PDF output, this option requires at least version 3.0.25 of the siunitx LaTeX package. See the LaTeX preamble help section below for commands to insert in your LaTeX preamble.
- notes
list or vector of notes to append to the bottom of the table.
- title
string. Cross-reference labels should be added with Quarto or Rmarkdown chunk options when applicable. When saving standalone LaTeX files, users can add a label such as
\\label{tab:mytable}directly to the title string, while also specifyingescape=FALSE.- escape
boolean TRUE escapes or substitutes LaTeX/HTML characters which could prevent the file from compiling/displaying.
TRUEescapes all cells, captions, and notes. Users can have more fine-grained control by settingescape=FALSEand using an external command such as:modelsummary(model, "latex") |> tinytable::format_tt(tab, j=1:5, escape=TRUE)- ...
all other arguments are passed through to three functions. See the documentation of these functions for lists of available arguments.
parameters::model_parameters extracts parameter estimates. Available arguments depend on model type, but include:
standardize,include_reference,centrality,dispersion,test,ci_method,prior,diagnostic,rope_range,power,cluster, etc.
performance::model_performance extracts goodness-of-fit statistics. Available arguments depend on model type, but include:
metrics,estimator, etc.
tinytable::tt, kableExtra::kbl or gt::gt draw tables, depending on the value of the
outputargument. For example, by defaultmodelsummarycreates tables with tinytable::tt, which accepts awidthandthemearguments.
Value
a regression table in a format determined by the output argument.
The backend attribute includes the backend used to extract estimates and goodness-of-fit measure.
Details
output
The modelsummary_list output is a lightweight format which can be used to save model results, so they can be fed back to modelsummary later to avoid extracting results again.
When a file name with a valid extension is supplied to the output argument,
the table is written immediately to file. If you want to customize your table
by post-processing it with an external package, you need to choose a
different output format and saving mechanism. Unfortunately, the approach
differs from package to package:
tinytable: setoutput="tinytable", post-process your table, and use thetinytable::save_ttfunction.gt: setoutput="gt", post-process your table, and use thegt::gtsavefunction.kableExtra: setoutputto your destination format (e.g., "latex", "html", "markdown"), post-process your table, and usekableExtra::save_kablefunction.
vcov
To use a string such as "robust" or "HC0", your model must be supported
by the sandwich package. This includes objects such as: lm, glm,
survreg, coxph, mlogit, polr, hurdle, zeroinfl, and more.
NULL, "classical", "iid", and "constant" are aliases which do not modify uncertainty estimates and simply report the default standard errors stored in the model object.
One-sided formulas such as ~clusterid are passed to the sandwich::vcovCL
function.
Matrices and functions producing variance-covariance matrices are first
passed to lmtest. If this does not work, modelsummary attempts to take
the square root of the diagonal to adjust "std.error", but the other
uncertainty estimates are not be adjusted.
Numeric vectors are formatted according to fmt and placed in brackets.
Character vectors printed as given, without parentheses.
If your model type is supported by the lmtest package, the
vcov argument will try to use that package to adjust all the
uncertainty estimates, including "std.error", "statistic", "p.value", and
"conf.int". If your model is not supported by lmtest, only the "std.error"
will be adjusted by, for example, taking the square root of the matrix's
diagonal.
Version 2.0.0, kableExtra, and tinytable
Since version 2.0.0, modelsummary uses tinytable as its default table-drawing backend.
Learn more at: https://vincentarelbundock.github.io/tinytable/",
Revert to kableExtra for one session:
options(modelsummary_factory_default = 'kableExtra')
options(modelsummary_factory_latex = 'kableExtra')
options(modelsummary_factory_html = 'kableExtra')
Global Options
The behavior of modelsummary can be modified by setting global options. In particular, most of the arguments for most of the package's functions cna be set using global options. For example:
options(modelsummary_output = "modelsummary_list")options(modelsummary_statistic = '({conf.low}, {conf.high})')options(modelsummary_stars = TRUE)
Options not specific to given arguments are listed below.
Balance tables: summary functions
You can customize the summary statistics shown in datasummary_balance():
options(modelsummary_balance_fn = list(Mean = Mean, "Std. Dev." = SD))
The list must be named, and each function should accept a numeric vector and
return a single value. When this option is set and a weights column is
present, weights are ignored with a warning.
Model labels: default column names
These global option changes the style of the default column headers:
options(modelsummary_model_labels = "roman")options(modelsummary_model_labels_term = "Parameter")options(modelsummary_model_labels_group = "Group")options(modelsummary_model_labels_model = "Model")
The options above control the label shown in the stub columns for terms, groups, and model names. By default, those headers are blank. Setting any of these options to a character string inserts that label as the column header.
The supported styles are: "model", "arabic", "letters", "roman", "(arabic)", "(letters)", "(roman)"
Table-making packages
modelsummary supports 6 table-making packages: tinytable, kableExtra, gt,
flextable, huxtable, and DT. Some of these packages have overlapping
functionalities. To change the default backend used for a specific file
format, you can use ' the options function:
options(modelsummary_factory_html = 'kableExtra')
options(modelsummary_factory_word = 'huxtable')
options(modelsummary_factory_png = 'gt')
options(modelsummary_factory_latex = 'gt')
options(modelsummary_factory_latex_tabular = 'kableExtra')
Table themes
Change the look of tables in an automated and replicable way, using the modelsummary theming functionality. See the vignette: https://modelsummary.com/vignettes/appearance.html
modelsummary_theme_gtmodelsummary_theme_kableExtramodelsummary_theme_huxtablemodelsummary_theme_flextablemodelsummary_theme_dataframe
Model extraction functions
modelsummary can use two sets of packages to extract information from
statistical models: the easystats family (performance and parameters)
and broom. By default, it uses easystats first and then falls back on
broom in case of failure. You can change the order of priorities or include
goodness-of-fit extracted by both packages by setting:
options(modelsummary_get = "easystats")
options(modelsummary_get = "broom")
options(modelsummary_get = "all")
The "all" option (default) means easystats then broom.
Formatting numeric entries
By default, LaTeX tables enclose all numeric entries in the \num{} command
from the siunitx package. To prevent this behavior, or to enclose numbers
in dollar signs (for LaTeX math mode), users can call:
options(modelsummary_format_numeric_latex = "plain")
options(modelsummary_format_numeric_latex = "mathmode")
A similar option can be used to display numerical entries using MathJax in HTML tables:
options(modelsummary_format_numeric_html = "mathjax")
LaTeX preamble
When creating LaTeX via the tinytable backend (default in version 2.0.0 and later), it is useful to include the following commands in the LaTeX preamble of your documents. These commands are automatically added to the preamble when compiling Rmarkdown or Quarto documents, except when the modelsummary() calls are cached.
\usepackage{tabularray}
\usepackage{float}
\usepackage{graphicx}
\usepackage[normalem]{ulem}
\UseTblrLibrary{booktabs}
\UseTblrLibrary{siunitx}
\newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}}
\newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}
\NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}Parallel computation
It can take a long time to compute and extract summary statistics from
certain models (e.g., Bayesian). In those cases, users can parallelize the
process. Since parallelization occurs at the model level, no speedup is
available for tables with a single model. Users on mac or linux can launch
parallel computation using the built-in parallel package. All they need to
do is supply a mc.cores argument which will be pushed forward to the
parallel::mclapply function:
modelsummary(model_list, mc.cores = 5)All users can also use the future.apply package to parallelize model summaries.
For example, to use 4 cores to extract results:
library(future.apply)
plan(multicore, workers = 4)
options("modelsummary_future" = TRUE)
modelsummary(model_list)Note that the "multicore" plan only parallelizes under mac or linux. Windows
users can use plan(multisession) instead. However, note that the first
time modelsummary() is called under multisession can be a fair bit longer,
because of extra costs in passing data to and loading required packages on
to workers. Subsequent calls to modelsummary() will often be much faster.
Some users have reported difficult to reproduce errors when using the
future package with some packages. The future parallelization in
modelsummary can be disabled by calling:
options("modelsummary_future" = FALSE)
References
Arel-Bundock V (2022). “modelsummary: Data and Model Summaries in R.” Journal of Statistical Software, 103(1), 1-23. doi:10.18637/jss.v103.i01 .'
Examples
if (FALSE) { # isTRUE(Sys.getenv("R_NOT_CRAN") == "true")
# The `modelsummary` website includes \emph{many} examples and tutorials:
# https://modelsummary.com
library(modelsummary)
# load data and estimate models
utils::data(trees)
models <- list()
models[["Bivariate"]] <- lm(Girth ~ Height, data = trees)
models[["Multivariate"]] <- lm(Girth ~ Height + Volume, data = trees)
# simple table
modelsummary(models)
# statistic
modelsummary(models, statistic = NULL)
modelsummary(models, statistic = "p.value")
modelsummary(models, statistic = "statistic")
modelsummary(models, statistic = "conf.int", conf_level = 0.99)
modelsummary(models, statistic = c(
"t = {statistic}",
"se = {std.error}",
"conf.int"))
# estimate
modelsummary(models,
statistic = NULL,
estimate = "{estimate} [{conf.low}, {conf.high}]")
modelsummary(models,
estimate = c(
"{estimate}{stars}",
"{estimate} ({std.error})"))
# vcov
modelsummary(models, vcov = "robust")
modelsummary(models, vcov = list("classical", "stata"))
modelsummary(models, vcov = sandwich::vcovHC)
modelsummary(models,
vcov = list(stats::vcov, sandwich::vcovHC))
modelsummary(models,
vcov = list(
c("(Intercept)" = "", "Height" = "!"),
c("(Intercept)" = "", "Height" = "!", "Volume" = "!!")))
# vcov with custom names
modelsummary(
models,
vcov = list(
"Stata Corp" = "stata",
"Newey Lewis & the News" = "NeweyWest"))
# fmt
mod <- lm(mpg ~ hp + drat + qsec, data = mtcars)
modelsummary(mod, fmt = 3)
modelsummary(mod, fmt = fmt_significant(3))
modelsummary(mod, fmt = NULL)
modelsummary(mod, fmt = fmt_decimal(4))
modelsummary(mod, fmt = fmt_sprintf("%.5f"))
modelsummary(mod, fmt = fmt_statistic(estimate = 4, conf.int = 1), statistic = "conf.int")
modelsummary(mod, fmt = fmt_term(hp = 4, drat = 1, default = 2))
m <- lm(mpg ~ I(hp * 1000) + drat, data = mtcars)
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE, trim = TRUE)
modelsummary(m, fmt = f, gof_map = NA)
# coef_rename
modelsummary(models, coef_rename = c("Volume" = "Large", "Height" = "Tall"))
modelsummary(models, coef_rename = toupper)
modelsummary(models, coef_rename = coef_rename)
# coef_rename = TRUE for variable labels
datlab <- mtcars
datlab$cyl <- factor(datlab$cyl)
attr(datlab$hp, "label") <- "Horsepower"
attr(datlab$cyl, "label") <- "Cylinders"
modlab <- lm(mpg ~ hp * drat + cyl, data = datlab)
modelsummary(modlab, coef_rename = TRUE)
# coef_rename: unnamed vector of length equal to the number of terms in the final table
m <- lm(hp ~ mpg + factor(cyl), data = mtcars)
modelsummary(m, coef_omit = -(3:4), coef_rename = c("Cyl 6", "Cyl 8"))
# coef_map
modelsummary(models, coef_map = c("Volume" = "Large", "Height" = "Tall"))
modelsummary(models, coef_map = c("Volume", "Height"))
# coef_omit: omit the first and second coefficients
modelsummary(models, coef_omit = 1:2)
# coef_omit: omit coefficients matching one substring
modelsummary(models, coef_omit = "ei", gof_omit = ".*")
# coef_omit: omit a specific coefficient
modelsummary(models, coef_omit = "^Volume$", gof_omit = ".*")
# coef_omit: omit coefficients matching either one of two substring
# modelsummary(models, coef_omit = "ei|rc", gof_omit = ".*")
# coef_omit: keep coefficients starting with a substring (using a negative lookahead)
# modelsummary(models, coef_omit = "^(?!Vol)", gof_omit = ".*")
# coef_omit: keep coefficients matching a substring
modelsummary(models, coef_omit = "^(?!.*ei|.*pt)", gof_omit = ".*")
# shape: multinomial model
library(nnet)
multi <- multinom(factor(cyl) ~ mpg + hp, data = mtcars, trace = FALSE)
# shape: term names and group ids in rows, models in columns
modelsummary(multi, shape = response ~ model)
# shape: term names and group ids in rows in a single column
modelsummary(multi, shape = term:response ~ model)
# shape: term names in rows and group ids in columns
modelsummary(multi, shape = term ~ response:model)
# shape = "rcollapse"
panels <- list(
"Panel A: MPG" = list(
"A" = lm(mpg ~ hp, data = mtcars),
"B" = lm(mpg ~ hp + factor(gear), data = mtcars)),
"Panel B: Displacement" = list(
"A" = lm(disp ~ hp, data = mtcars),
"C" = lm(disp ~ hp + factor(gear), data = mtcars))
)
# shape = "cbind"
modelsummary(panels, shape = "cbind")
modelsummary(
panels,
shape = "rbind",
gof_map = c("nobs", "r.squared"))
# title
modelsummary(models, title = "This is the title")
# title with LaTeX label (for numbering and referencing)
modelsummary(models, title = "This is the title \\label{tab:description}", escape = FALSE)
# add_rows
rows <- tibble::tribble(
~term, ~Bivariate, ~Multivariate,
"Empty row", "-", "-",
"Another empty row", "?", "?")
attr(rows, "position") <- c(1, 3)
modelsummary(models, add_rows = rows)
attr(rows, "position") <- "gof_start"
modelsummary(models, add_rows = rows)
# notes
modelsummary(models, notes = list("A first note", "A second note"))
# gof_map: tribble
library(tibble)
gm <- tribble(
~raw, ~clean, ~fmt,
"r.squared", "R Squared", 5)
modelsummary(models, gof_map = gm)
# gof_map: list of lists
f <- function(x) format(round(x, 3), big.mark = ",")
gm <- list(
list("raw" = "nobs", "clean" = "N", "fmt" = f),
list("raw" = "AIC", "clean" = "aic", "fmt" = f))
modelsummary(models, gof_map = gm)
}