Home > Mobile >  Modelsummary: Different formats for estimates and statistics
Modelsummary: Different formats for estimates and statistics

Time:12-18

First of all thanks to the creator of the modelsummary package -- very useful!

I have a question about different fmt for statistic and estimates? Here is a reprex:

    url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url)
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)
modelsummary(models, output = "flextable", estimate=glue_col("{estimate}{stars}"),
             statistic = 'statistic', stars = c('*' = .1, '**' = .05, '***'=0.01))

How can I make the t stats under the coefficients have 2 significant digits and the estimates 3? So that the first coefficient-stat pair would be: 7948.667 (2078.27)

I have perused the documentation but haven't found the answer. Thank you in advance!

CodePudding user response:

As of version 0.9.4 there is not a direct way to achieve this with just he fmt argument or glue strings.

However, it is quite easy to leverage the tidy_custom and glance_custom mechanisms described on the modelsummary website to do just about any post-processing on your estimates and statistics. This gives users infinite possibilities to customize the output format.

For example,

library(modelsummary)
library(broom)

tidy_custom.lm <- function(x) {
    out <- broom::tidy(x)
    out$statistic <- sprintf("%.2f", out$statistic)
    out$estimate <- sprintf("%.3f", out$estimate)
    return(out)
}

mod <- lm(mpg ~ hp   drat, mtcars)

modelsummary(mod, statistic = "statistic", stars = TRUE)
Model 1
(Intercept) 10.790*
(2.12)
hp -0.052***
(-5.57)
drat 4.698***
(3.94)
Num.Obs. 32
R2 0.741
R2 Adj. 0.723
AIC 169.5
BIC 175.4
Log.Lik. -80.752
F 41.522

Note: ^^ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

I opened an issue on Github. Feel free to post there if you want to contribute to the conversation.

  • Related