Getting started

To begin, load the modelsummary package and download data from the Rdatasets archive:

library(modelsummary)
Version 2.0.0 of `modelsummary`, to be released soon, will introduce a
  breaking change: The default table-drawing package will be `tinytable`
  instead of `kableExtra`. All currently supported table-drawing packages
  will continue to be supported for the foreseeable future, including
  `kableExtra`, `gt`, `huxtable`, `flextable, and `DT`.
  
  You can always call the `config_modelsummary()` function to change the
  default table-drawing package in persistent fashion. To try `tinytable`
  now:
  
  config_modelsummary(factory_default = 'tinytable')
  
  To set the default back to `kableExtra`:
  
  config_modelsummary(factory_default = 'kableExtra')
url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url) 
dat$Small <- dat$Pop1831 > median(dat$Pop1831)
dat <- dat[, 
  c("Donations", "Literacy", "Commerce", "Crime_pers", "Crime_prop", "Clergy", "Small")
]

Data Summaries

Quick overview of the data:

datasummary_skim(dat)
tinytable_qh3ershcxm4rhwko0ap3
Unique Missing Pct. Mean SD Min Median Max
Donations 85 0 7075.5 5834.6 1246.0 5020.0 37015.0
Literacy 50 0 39.3 17.4 12.0 38.0 74.0
Commerce 84 0 42.8 25.0 1.0 42.5 86.0
Crime_pers 85 0 19754.4 7504.7 2199.0 18748.5 37014.0
Crime_prop 86 0 7843.1 3051.4 1368.0 7595.0 20235.0
Clergy 85 0 43.4 25.0 1.0 43.5 86.0

Balance table (aka “Table 1”) with differences in means by subgroups:

datasummary_balance(~Small, dat)
tinytable_3rt685nhvm6jottreg8g
FALSE (N=43) TRUE (N=43)
Mean Std. Dev. Mean Std. Dev. Diff. in Means Std. Error
Donations 7258.5 6194.1 6892.6 5519.0 -365.9 1265.2
Literacy 37.9 19.1 40.6 15.6 2.7 3.8
Commerce 42.7 24.6 43.0 25.7 0.3 5.4
Crime_pers 18040.6 7638.4 21468.2 7044.3 3427.7 1584.6
Crime_prop 8422.5 3406.7 7263.7 2559.3 -1158.8 649.8
Clergy 39.1 26.7 47.7 22.7 8.6 5.3

Correlation table:

datasummary_correlation(dat)
tinytable_uhszpgzz88abacy2xytn
Donations Literacy Commerce Crime_pers Crime_prop Clergy
Donations 1 . . . . .
Literacy -.13 1 . . . .
Commerce .30 -.58 1 . . .
Crime_pers -.04 -.04 .05 1 . .
Crime_prop -.13 -.37 .41 .27 1 .
Clergy .09 -.17 -.12 .26 -.07 1

Two variables and two statistics, nested in subgroups:

datasummary(Literacy + Commerce ~ Small * (mean + sd), dat)
tinytable_y2k4xe46zdw5qa48t74n
FALSE TRUE
mean sd mean sd
Literacy 37.88 19.08 40.63 15.57
Commerce 42.65 24.59 42.95 25.75

Model Summaries

Estimate a linear model and display the results:

mod <- lm(Donations ~ Crime_prop, data = dat)

modelsummary(mod)
tinytable_o9tilw760a7ap6yyjnvl
(1)
(Intercept) 9065.287
(1738.926)
Crime_prop -0.254
(0.207)
Num.Obs. 86
R2 0.018
R2 Adj. 0.006
AIC 1739.0
BIC 1746.4
Log.Lik. -866.516
F 1.505
RMSE 5749.29

Estimate five regression models, display the results side-by-side, and display the table:

models <- list(
  "OLS 1"     = lm(Donations ~ Literacy + Clergy, data = dat),
  "Poisson 1" = glm(Donations ~ Literacy + Commerce, family = poisson, data = dat),
  "OLS 2"     = lm(Crime_pers ~ Literacy + Clergy, data = dat),
  "Poisson 2" = glm(Crime_pers ~ Literacy + Commerce, family = poisson, data = dat),
  "OLS 3"     = lm(Crime_prop ~ Literacy + Clergy, data = dat)
)

modelsummary(models, stars = TRUE, gof_omit = "IC|Adj|F|RMSE|Log")
tinytable_u7nnu8h9kouaom414uyj
OLS 1 Poisson 1 OLS 2 Poisson 2 OLS 3
(Intercept) 7948.667*** 8.241*** 16259.384*** 9.876*** 11243.544***
(2078.276) (0.006) (2611.140) (0.003) (1011.240)
Literacy -39.121 0.003*** 3.680 0.000*** -68.507***
(37.052) (0.000) (46.552) (0.000) (18.029)
Clergy 15.257 77.148* -16.376
(25.735) (32.334) (12.522)
Commerce 0.011*** 0.001***
(0.000) (0.000)
Num.Obs. 86 86 86 86 86
R2 0.020 0.065 0.152

Now, save it to a Microsoft Word document:

modelsummary(models, output = "table.docx")

And draw a coefficient plot:

# OLS models only
ols <- models[grepl("OLS", names(models))]
modelplot(ols, coef_omit = "Intercept")