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 | ||
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.)
sjPlot::tab_model() does not render properly when knitted to PDF, but will render to HTML.broom.mixed::tidy() renders to both PDF and HTML.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 sjPlotlibrary(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.mixedlibrary(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),
statistic = round(statistic, 2),
p.value = ifelse(p.value < .001, '<.001', round(p.value, 3))
) |>
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"
) | term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | 36.16 | 12.84 | 2.82 | 0.012 |
| recall_confidence | 0.90 | 0.19 | 4.68 | <.001 |
| 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.
Bonus: If you want to make this table appear fully publication-ready, then there are a couple ways you can tidy it up (you will see examples of these in the write-up week lab solutions):
t-value must become z-value!)tidy(recall_multi) |>
mutate(
estimate = round(estimate, 2),
std.error = round(std.error, 2),
statistic = round(statistic, 2),
p.value = ifelse(p.value < .001, '<.001', round(p.value, 3))
) |>
# this next bit gives the columns nicer-looking names
rename(
Term = term,
Estimate = estimate,
SE = std.error,
`t-value` = statistic,
`p-value` = p.value
) |>
# this next bit tidies up what's displayed in the "Term" column
mutate(
Term = case_when(
# in general, the syntax here is:
# Term == '<current coef name>' ~ '<the nice name you want to show>'
Term == '(Intercept)' ~ 'Intercept',
Term == 'recall_confidence' ~ 'Recall confidence',
Term == 'age' ~ 'Age'
)
) |>
# back to our regularly scheduled programming (ha ha)
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"
)| Term | Estimate | SE | t-value | p-value |
|---|---|---|---|---|
| Intercept | 36.16 | 12.84 | 2.82 | 0.012 |
| Recall confidence | 0.90 | 0.19 | 4.68 | <.001 |
| Age | -0.34 | 0.15 | -2.21 | 0.041 |
modelsummary() from modelsummarylibrary(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"
)| 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.