Nicely-formatted coefficient tables

You have three main options for creating nicely-formatted coefficient tables (or four, if you would like to manually create your own using Markdown tables, but we won’t elaborate on that here.)

  1. sjPlot::tab_model() does not render properly when knitted to PDF, but will render to HTML.
  2. broom.mixed::tidy() renders to both PDF and HTML.
  3. modelsummary::modelsummary() also renders to both PDF and HTML.

Examples of each are below, shown for the model from the Multiple regression flash card.

In reports, it doesn’t matter which of these you use, as long as the key information (the coefficient estimates, some indication of the estimates’ precision like SE or 95% CI, and their significance) is there.

tab_model() from sjPlot

library(sjPlot)

tab_model(recall_multi)
  recall accuracy
Predictors Estimates CI p
(Intercept) 36.16 9.07 – 63.25 0.012
recall confidence 0.90 0.49 – 1.30 <0.001
age -0.34 -0.66 – -0.02 0.041
Observations 20
R2 / R2 adjusted 0.603 / 0.557


Classic and minimal, and works just fine when knitting to HTML, but fails to render properly when knitting a document to PDF. So for your reports, you’ll want to use either one of the next two options (doesn’t matter which).

tidy() from broom.mixed

library(broom.mixed)
library(kableExtra)

The code chunk below has the following header line, which defines the label we’ll use to refer to this table in the text:

```{r FromBroomMixed}


tidy(recall_multi) |>
  mutate(
    estimate = round(estimate, 2),
    std.error = round(std.error, 2)
  ) |>
  kable(
    booktabs = TRUE, 
    caption = 'This is my caption'
    # when you render to PDF (not to HTML as we are doing here), 
    # the caption will appear as "Table 1: This is my caption"
  )  
This is my caption
term estimate std.error statistic p.value
(Intercept) 36.16 12.84 2.82 0.012
recall_confidence 0.90 0.19 4.68 0.000
age -0.34 0.15 -2.21 0.041


To refer to this table in your Rmd file, write:

Here I am cross-referencing Table \@ref(tab:FromBroomMixed).

This will appear as “Here I am cross-referencing Table 1”.

For more detail on table formatting and cross-referencing, see the DAPR1 table formatting resources.

modelsummary() from modelsummary

library(modelsummary)
modelsummary(
  models = list('Recall accuracy' = recall_multi),  
  # ^ in general: `list('<Dependent variable name>' = <modelname>)`
  
  fmt = fmt_statistic('estimate' = 2, 'std.error' = 2, 'conf.int' = 2, 'p.value' = 3),
  estimate = c("b" = "{estimate}"),
  statistic = c("SE" = "{std.error}", "p" = "{p.value}", "CI" = "[{conf.low}, {conf.high}]"),
  shape = term ~ statistic,
  gof_map = 'none',
  output = 'markdown',
  # ^ leave all this as-is
  
  title = "(\\#tab:FromModelSummary) This is my caption"
  # ^ best to only edit the bits outside brackets.
  # when you render to PDF (not to HTML as we are doing here), 
  # the caption will appear as "Table 2: This is my caption"
)
(#tab:FromModelSummary) This is my caption
Recall accuracy
b SE p CI
(Intercept) 36.16 12.84 0.012 [9.07, 63.25]
recall_confidence 0.90 0.19 0.000 [0.49, 1.30]
age -0.34 0.15 0.041 [-0.66, -0.02]


In your Rmd file, write:

Here I am cross-referencing Table \@ref(tab:FromModelSummary).

This will appear as “Here I am cross-referencing Table 2”.

For more detail on table formatting and cross-referencing, see the DAPR1 table formatting resources.