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.

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)
#> <!DOCTYPE html>
#> <html lang="en">
#> <head>
#> <meta charset="utf-8"/>
#> <style>body{background-color:white;}</style>
#> 
#> 
#> </head>
#> <body>
#> <div id="zsibanzibg" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
#>   <style>#zsibanzibg table {
#>   font-family: times, system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
#>   -webkit-font-smoothing: antialiased;
#>   -moz-osx-font-smoothing: grayscale;
#> }
#> 
#> #zsibanzibg thead, #zsibanzibg tbody, #zsibanzibg tfoot, #zsibanzibg tr, #zsibanzibg td, #zsibanzibg th {
#>   border-style: none;
#> }
#> 
#> #zsibanzibg p {
#>   margin: 0;
#>   padding: 0;
#> }
#> 
#> #zsibanzibg .gt_table {
#>   display: table;
#>   border-collapse: collapse;
#>   line-height: normal;
#>   margin-left: auto;
#>   margin-right: auto;
#>   color: #333333;
#>   font-size: 12px;
#>   font-weight: normal;
#>   font-style: none;
#>   background-color: #FFFFFF;
#>   width: auto;
#>   border-top-style: none;
#>   border-top-width: 2px;
#>   border-top-color: #FFFFFF;
#>   border-right-style: none;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#>   border-bottom-style: none;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #A8A8A8;
#>   border-left-style: none;
#>   border-left-width: 2px;
#>   border-left-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_caption {
#>   padding-top: 4px;
#>   padding-bottom: 4px;
#> }
#> 
#> #zsibanzibg .gt_title {
#>   color: #333333;
#>   font-size: 12px;
#>   font-weight: initial;
#>   padding-top: 4px;
#>   padding-bottom: 4px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-bottom-color: #FFFFFF;
#>   border-bottom-width: 0;
#> }
#> 
#> #zsibanzibg .gt_subtitle {
#>   color: #333333;
#>   font-size: 12px;
#>   font-weight: initial;
#>   padding-top: 3px;
#>   padding-bottom: 5px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-top-color: #FFFFFF;
#>   border-top-width: 0;
#> }
#> 
#> #zsibanzibg .gt_heading {
#>   background-color: #FFFFFF;
#>   text-align: center;
#>   border-bottom-color: #FFFFFF;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_bottom_border {
#>   border-bottom-style: solid;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #000000;
#> }
#> 
#> #zsibanzibg .gt_col_headings {
#>   border-top-style: none;
#>   border-top-width: 2px;
#>   border-top-color: #D3D3D3;
#>   border-bottom-style: solid;
#>   border-bottom-width: 1px;
#>   border-bottom-color: #000000;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_col_heading {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: normal;
#>   text-transform: inherit;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#>   vertical-align: bottom;
#>   padding-top: 5px;
#>   padding-bottom: 6px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   overflow-x: hidden;
#> }
#> 
#> #zsibanzibg .gt_column_spanner_outer {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: normal;
#>   text-transform: inherit;
#>   padding-top: 0;
#>   padding-bottom: 0;
#>   padding-left: 4px;
#>   padding-right: 4px;
#> }
#> 
#> #zsibanzibg .gt_column_spanner_outer:first-child {
#>   padding-left: 0;
#> }
#> 
#> #zsibanzibg .gt_column_spanner_outer:last-child {
#>   padding-right: 0;
#> }
#> 
#> #zsibanzibg .gt_column_spanner {
#>   border-bottom-style: solid;
#>   border-bottom-width: 1px;
#>   border-bottom-color: #000000;
#>   vertical-align: bottom;
#>   padding-top: 5px;
#>   padding-bottom: 5px;
#>   overflow-x: hidden;
#>   display: inline-block;
#>   width: 100%;
#> }
#> 
#> #zsibanzibg .gt_spanner_row {
#>   border-bottom-style: hidden;
#> }
#> 
#> #zsibanzibg .gt_group_heading {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: initial;
#>   text-transform: inherit;
#>   border-top-style: none;
#>   border-top-width: 2px;
#>   border-top-color: #D3D3D3;
#>   border-bottom-style: none;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#>   vertical-align: middle;
#>   text-align: left;
#> }
#> 
#> #zsibanzibg .gt_empty_group_heading {
#>   padding: 0.5px;
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: initial;
#>   border-top-style: none;
#>   border-top-width: 2px;
#>   border-top-color: #D3D3D3;
#>   border-bottom-style: none;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   vertical-align: middle;
#> }
#> 
#> #zsibanzibg .gt_from_md > :first-child {
#>   margin-top: 0;
#> }
#> 
#> #zsibanzibg .gt_from_md > :last-child {
#>   margin-bottom: 0;
#> }
#> 
#> #zsibanzibg .gt_row {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   margin: 10px;
#>   border-top-style: none;
#>   border-top-width: 1px;
#>   border-top-color: #FFFFFF;
#>   border-left-style: none;
#>   border-left-width: 1px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 1px;
#>   border-right-color: #D3D3D3;
#>   vertical-align: middle;
#>   overflow-x: hidden;
#> }
#> 
#> #zsibanzibg .gt_stub {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: initial;
#>   text-transform: inherit;
#>   border-right-style: none;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #zsibanzibg .gt_stub_row_group {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   font-size: 100%;
#>   font-weight: initial;
#>   text-transform: inherit;
#>   border-right-style: none;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   vertical-align: top;
#> }
#> 
#> #zsibanzibg .gt_row_group_first td {
#>   border-top-width: 2px;
#> }
#> 
#> #zsibanzibg .gt_row_group_first th {
#>   border-top-width: 2px;
#> }
#> 
#> #zsibanzibg .gt_summary_row {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   text-transform: inherit;
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #zsibanzibg .gt_first_summary_row {
#>   border-top-style: none;
#>   border-top-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_first_summary_row.thick {
#>   border-top-width: 2px;
#> }
#> 
#> #zsibanzibg .gt_last_summary_row {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-bottom-style: none;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_grand_summary_row {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   text-transform: inherit;
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #zsibanzibg .gt_first_grand_summary_row {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-top-style: none;
#>   border-top-width: 6px;
#>   border-top-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_last_grand_summary_row_top {
#>   padding-top: 8px;
#>   padding-bottom: 8px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#>   border-bottom-style: none;
#>   border-bottom-width: 6px;
#>   border-bottom-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_striped {
#>   background-color: rgba(128, 128, 128, 0.05);
#> }
#> 
#> #zsibanzibg .gt_table_body {
#>   border-top-style: solid;
#>   border-top-width: 1px;
#>   border-top-color: #000000;
#>   border-bottom-style: solid;
#>   border-bottom-width: 1px;
#>   border-bottom-color: #000000;
#> }
#> 
#> #zsibanzibg .gt_footnotes {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   border-bottom-style: none;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   border-left-style: none;
#>   border-left-width: 2px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_footnote {
#>   margin: 0px;
#>   font-size: 90%;
#>   padding-top: 4px;
#>   padding-bottom: 4px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #zsibanzibg .gt_sourcenotes {
#>   color: #333333;
#>   background-color: #FFFFFF;
#>   border-bottom-style: none;
#>   border-bottom-width: 2px;
#>   border-bottom-color: #D3D3D3;
#>   border-left-style: none;
#>   border-left-width: 2px;
#>   border-left-color: #D3D3D3;
#>   border-right-style: none;
#>   border-right-width: 2px;
#>   border-right-color: #D3D3D3;
#> }
#> 
#> #zsibanzibg .gt_sourcenote {
#>   font-size: 90%;
#>   padding-top: 4px;
#>   padding-bottom: 4px;
#>   padding-left: 5px;
#>   padding-right: 5px;
#> }
#> 
#> #zsibanzibg .gt_left {
#>   text-align: left;
#> }
#> 
#> #zsibanzibg .gt_center {
#>   text-align: center;
#> }
#> 
#> #zsibanzibg .gt_right {
#>   text-align: right;
#>   font-variant-numeric: tabular-nums;
#> }
#> 
#> #zsibanzibg .gt_font_normal {
#>   font-weight: normal;
#> }
#> 
#> #zsibanzibg .gt_font_bold {
#>   font-weight: bold;
#> }
#> 
#> #zsibanzibg .gt_font_italic {
#>   font-style: italic;
#> }
#> 
#> #zsibanzibg .gt_super {
#>   font-size: 65%;
#> }
#> 
#> #zsibanzibg .gt_footnote_marks {
#>   font-size: 75%;
#>   vertical-align: 0.4em;
#>   position: initial;
#> }
#> 
#> #zsibanzibg .gt_asterisk {
#>   font-size: 100%;
#>   vertical-align: 0;
#> }
#> 
#> #zsibanzibg .gt_indent_1 {
#>   text-indent: 5px;
#> }
#> 
#> #zsibanzibg .gt_indent_2 {
#>   text-indent: 10px;
#> }
#> 
#> #zsibanzibg .gt_indent_3 {
#>   text-indent: 15px;
#> }
#> 
#> #zsibanzibg .gt_indent_4 {
#>   text-indent: 20px;
#> }
#> 
#> #zsibanzibg .gt_indent_5 {
#>   text-indent: 25px;
#> }
#> 
#> #zsibanzibg .katex-display {
#>   display: inline-flex !important;
#>   margin-bottom: 0.75em !important;
#> }
#> 
#> #zsibanzibg div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
#>   height: 0px !important;
#> }
#> </style>
#>   <table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
#>   <thead>
#>     <tr class="gt_col_headings">
#>       <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="&lt;span class='gt_from_md'&gt;&lt;/span&gt;"><span class='gt_from_md'></span></th>
#>       <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="&lt;span class='gt_from_md'&gt;&lt;em&gt;&lt;center&gt;B (SE)&lt;/center&gt;&lt;/em&gt;&lt;/span&gt;"><span class='gt_from_md'><em><center>B (SE)</center></em></span></th>
#>       <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="&lt;span class='gt_from_md'&gt;&lt;em&gt;&lt;center&gt;&beta; [95% CI]&lt;/center&gt;&lt;/em&gt;&lt;/span&gt;"><span class='gt_from_md'><em><center>&beta; [95% CI]</center></em></span></th>
#>     </tr>
#>   </thead>
#>   <tbody class="gt_table_body">
#>     <tr><td headers=" " class="gt_row gt_left"><span class='gt_from_md'>(Intercept)</span></td>
#> <td headers="Model1" class="gt_row gt_right"><span class='gt_from_md'>37.23 (1.60)***</span></td>
#> <td headers="Model2" class="gt_row gt_right"><span class='gt_from_md'>0.00 [-0.16, 0.16]</span></td></tr>
#>     <tr><td headers=" " class="gt_row gt_left"><span class='gt_from_md'>hp</span></td>
#> <td headers="Model1" class="gt_row gt_right"><span class='gt_from_md'>-0.03 (0.01)**&nbsp;</span></td>
#> <td headers="Model2" class="gt_row gt_right"><span class='gt_from_md'>-0.36 [-0.57, -0.15]</span></td></tr>
#>     <tr><td headers=" " class="gt_row gt_left"><span class='gt_from_md'>wt</span></td>
#> <td headers="Model1" class="gt_row gt_right"><span class='gt_from_md'>-3.88 (0.63)***</span></td>
#> <td headers="Model2" class="gt_row gt_right"><span class='gt_from_md'>-0.63 [-0.84, -0.42]</span></td></tr>
#>    <tr style="border-top-style: solid; border-top-width: 2px;">
#>     <td class="gt_row gt_left" rowspan="1" colspan="1">  <em>N     </td> <td class="gt_row gt_center" rowspan="1" colspan="2"> 32   </td> </tr> <tr>
#>     <td class="gt_row gt_left" rowspan="1" colspan="1">  <em>R<sup>2</sup>     </td> <td class="gt_row gt_center" rowspan="1" colspan="2"> .83   </td> </tr> <tr>
#>     <td class="gt_row gt_left" rowspan="1" colspan="1"><em>F</em>-tests</td> <td class="gt_row gt_center" rowspan="1" colspan="2"> <p><em>F</em>(2, 29) = 69.21, <em>p</em> &lt; .001</p>
#>    </td> </tr> </tbody>
#>   <tfoot class="gt_sourcenotes">
#>     <tr>
#>       <td class="gt_sourcenote" colspan="3"><span class='gt_from_md'>Given that dummy variables lose their interpretability when standardized (Fox, 2015), &beta; for dummy variables are semi-standardized, indicating the impact of that dummy on the standardized outcome variable.</span></td>
#>     </tr>
#>     <tr>
#>       <td class="gt_sourcenote" colspan="3"><span class='gt_from_md'>&dagger; <em>p</em> &lt; .1, * <em>p</em> &lt; .05, ** <em>p</em> &lt; .01, *** <em>p</em> &lt; .001</span></td>
#>     </tr>
#>   </tfoot>
#>   
#> </table>
#> </div>
#> </body>
#> </html>
#> 



B (SE)
β [95% CI]
(Intercept) 37.23 (1.60)*** 0.00 [-0.16, 0.16]
hp -0.03 (0.01)**  -0.36 [-0.57, -0.15]
wt -3.88 (0.63)*** -0.63 [-0.84, -0.42]
N 32
R2 .83
F-tests

F(2, 29) = 69.21, p < .001

Given that dummy variables lose their interpretability when standardized (Fox, 2015), β for dummy variables are semi-standardized, indicating the impact of that dummy on the standardized outcome variable.
p < .1, * p < .05, ** p < .01, *** p < .001