customize numbers in modelsummary estimate

65 Views Asked by At

I can use the following code

library(fixest)
library(modelsummary)

reg1 <- feols(mpg ~ cyl , data = mtcars)
reg2 <- feols(mpg ~ cyl + wt , data = mtcars)



modelsummary(
  list(reg1, reg2),
  fmt = NULL,
  estimate  = c("{round(estimate, 3)} [my number]"),
  statistic = "{round(std.error, 3)}",
  coef_omit = "Intercept",
  gof_omit = 'RMSE|AIC|BIC|R2|Std.Errors')

to display numbers in square brackets next to my estimates. However I want different numbers for each of the coefficient in each of the regressions. Can I do this? the estimate argument does not seem to accept vector or list inputs.

1

There are 1 best solutions below

1
Vincent On BEST ANSWER

There is no built-in way to round numbers differently on a per-model basis. I don’t know any regression table package that can do that, but perhaps I’m missing something. That said, it is reasonably easy to do it semi-manually:

  1. Export each model to a modelsummary_list object.
  2. Round the numbers as you want.
  3. Feed the modelsummary_list objects back to the modelsummary() function.
library(modelsummary)
mod1 <- lm(mpg ~ cyl + disp, mtcars)
mod2 <- lm(mpg ~ cyl + disp + hp, mtcars)

extract_and_round <- function(model, significant_digits = 2) {
    out <- modelsummary(model, output = "modelsummary_list")
    out$tidy$estimate <- format(out$tidy$estimate, digits = significant_digits)
    out$tidy$std.error <- format(out$tidy$std.error, digits = significant_digits)
    return(out)
}

models <- list(
    extract_and_round(mod1, 2),
    extract_and_round(mod2, 5)
)

# Feed the modelsummary_list objects back to modelsummary()
modelsummary(models, output = "markdown")
(1) (2)
(Intercept) 34.661 34.184919
(2.55) (2.590778)
cyl -1.587 -1.227420
(0.71) (0.797276)
disp -0.021 -0.018838
(0.01) (0.010404)
hp -0.014679
(0.014651)
Num.Obs. 32 32
R2 0.760 0.768
R2 Adj. 0.743 0.743
AIC 167.1 168.0
BIC 173.0 175.3
Log.Lik. -79.573 -79.009
F 45.808 30.877
RMSE 2.91 2.86