Preparing regression tables with estimatr is possible with all of the major r-to-LaTeX packages, including texreg, modelsummary, stargazer, xtable, and huxtable.

Setup

First we'll load both estimatr and magrittr (for pipes).

library(estimatr)
library(magrittr)

Texreg

Texreg operates directly on an lm_robust object. If you would like standard errors instead of confidence intervals, use include.ci = FALSE.

library(texreg)
fit <- lm_robust(mpg ~ disp, data = mtcars)
texreg(fit, include.ci = FALSE)

modelsummary

modelsummary operates directly on lm_robust objects:

library(modelsummary)
fit1 <- lm(mpg ~ disp, data = mtcars)
fit2 <- lm(mpg ~ hp, data = mtcars)
modelsummary(list(fit1, fit2))

To learn how to customize the output table and/or statistics, please consult the modelsummary README file on Github: https://github.com/vincentarelbundock/modelsummary

Stargazer

Stargazer has to be tricked -- there's unfortunately no way around this unless something in the stargazer package changes. The maintainer has indicated that he has no active plans to update stargazer in the future. First, run the regression using lm. Then you can pass the lm fits to starprep a function which transforms the lm fits into lm_robust fits and then prepare the appropriate statistic you requested. The starprep function defaults to returning standard errors and uses lm_robust defaults for standard errors (robust HC2 SEs are the default in both lm_robust). Then you pass the lm fit to stargazer and then pass starprep(fit) to the se argument in stargazer. This process works great with multiple fits.

library(stargazer)
fit_1 <- lm(mpg ~ disp, data = mtcars)
fit_2 <- lm(mpg ~ hp, data = mtcars)
stargazer(fit_1, fit_2, se = starprep(fit_1, fit_2))
library(stargazer)
fit_1 <- lm(mpg ~ disp, data = mtcars)
fit_2 <- lm(mpg ~ hp, data = mtcars)
stargazer(fit_1, fit_2, se = starprep(fit_1, fit_2), header = FALSE)

Below are some more examples, although we hide the output for brevity.

# Can also specify clusters and standard error type
stargazer(
  fit_1, fit_2,
  se = starprep(fit_1, fit_2, clusters = mtcars$cyl, se_type = "stata")
)

# Can also precompute robust objects to save computation time
# using `commarobust`
fit_1_r <- commarobust(fit_1)
fit_2_r <- commarobust(fit_2)
stargazer(fit_1, fit_2,
          se = starprep(fit_1_r, fit_2_r),
          p = starprep(fit_1_r, fit_2_r, stat = "p.value"))

# can also easily get robust confidence intervals
stargazer(fit_1, fit_2,
          ci.custom = starprep(fit_1_r, fit_2_r, stat = "ci"))

xtable

xtable works directly on a data.frame, so we just have to prep the lm_robust output with the tidy function.

library(xtable)
fit <- lm_robust(mpg ~ disp, data = mtcars)
fit %>% tidy %>% xtable()

huxtable

huxtable, too, works on a data.frame, so all we have to do is prep with tidy.

library(huxtable)
fit <- lm_robust(mpg ~ disp, data = mtcars)
fit %>% tidy %>% hux() %>% print_latex()


DeclareDesign/estimatr documentation built on March 31, 2024, 10:06 p.m.