Balance table: Summary statistics for different subsets of the data (e.g., control and treatment groups)
Description
Creates balance tables with summary statistics for different subsets of the data (e.g., control and treatment groups). It can also be used to create summary tables for full data sets. See the Details and Examples sections below, and the vignettes on the modelsummary website:
one-sided formula: with the "condition" or "column" variable on the right-hand side.
two-side formula: with the subset of variables to summarize on the left-hand side and the condition variable on the right-hand side.
data
A data.frame (or tibble). If this data includes columns called "blocks", "clusters", and/or "weights", the "estimatr" package will consider them when calculating the difference in means. If there is a weights column, the reported mean and standard errors will also be weighted.
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 modelsummary function.
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.
Warning: Users should not supply a file name to the output argument if they intend to customize the table with external packages. See the ‘Details’ section.
LaTeX compilation requires the booktabs and siunitx packages, but siunitx can be disabled or replaced with global options. See the ‘Details’ section.
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_sprintf(“%.3f”): See ?sprintf
fmt = fmt_identity(): unformatted raw values
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 specifying escape=FALSE.
notes
list or vector of notes to append to the bottom of the table.
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.
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 estimate or statistic arguments use "glue strings" with {stars}.
add_columns
a data.frame (or tibble) with the same number of rows as your main table.
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. You can define a "position" attribute of integers to set the row positions. See Examples section below.
dinm
TRUE calculates a difference in means with uncertainty estimates. This option is only available if the estimatr package is installed. If data includes columns named "blocks", "clusters", or "weights", this information will be taken into account automatically by estimatr::difference_in_means.
dinm_statistic
string: "std.error" or "p.value"
escape
boolean TRUE escapes or substitutes LaTeX/HTML characters which could prevent the file from compiling/displaying. TRUE escapes all cells, captions, and notes. Users can have more fine-grained control by setting escape=FALSE and 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 the table-making functions tinytable::tt, kableExtra::kbl, gt::gt, DT::datatable, etc. depending on the output argument. This allows users to pass arguments directly to datasummary in order to affect the behavior of other functions behind the scenes.
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/",
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:
The panel-specific option is only used when shape=“rbind”
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:
Change the look of tables in an automated and replicable way, using the modelsummary theming functionality. See the vignette: https://modelsummary.com/articles/appearance.html
modelsummary_theme_gt
modelsummary_theme_kableExtra
modelsummary_theme_huxtable
modelsummary_theme_flextable
modelsummary_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”)
Formatting numeric entries
By default, LaTeX tables enclose all numeric entries in the command from the siunitx package. To prevent this behavior, or to enclose numbers in dollar signs (for LaTeX math mode), users can call:
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. Note that they are added automatically when compiling Rmarkdown or Quarto documents (except when the modelsummary() calls are cached).