I recently started using the modelsummary package in R.

By default, the modelsummary regression output displays RMSE. I could not specifically add Residual standard error row to the goodness of fit part of the regression table.

Reproducable example created on 2023-12-13 with reprex v2.0.2:

library(modelsummary)

reg1 <- lm(mpg ~ disp, data = mtcars)
summary(reg1)
#> 
#> Call:
#> lm(formula = mpg ~ disp, data = mtcars)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -4.8922 -2.2022 -0.9631  1.6272  7.2305 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 29.599855   1.229720  24.070  < 2e-16 ***
#> disp        -0.041215   0.004712  -8.747 9.38e-10 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.251 on 30 degrees of freedom
#> Multiple R-squared:  0.7183, Adjusted R-squared:  0.709 
#> F-statistic: 76.51 on 1 and 30 DF,  p-value: 9.38e-10

sqrt(sum(reg1$residuals^2) / reg1$df.residual) # Residual standard error
#> [1] 3.251454
sqrt(sum(reg1$residuals^2) / nobs(reg1))       # RMSE
#> [1] 3.148207

modelsummary(reg1, output = "markdown")
(1)
(Intercept) 29.600
(1.230)
disp -0.041
(0.005)
Num.Obs. 32
R2 0.718
R2 Adj. 0.709
AIC 170.2
BIC 174.6
Log.Lik. -82.105
F 76.513
RMSE 3.15
1

There are 1 answers

0
dufei On BEST ANSWER

Looking at the Modelsummary documentation, this should work:

library(modelsummary)

reg1 <- lm(mpg ~ disp, data = mtcars)

glance_custom.lm <- function(x, ...) {
  rse <- sqrt(sum(residuals(x) ^ 2) / x$df.residual)
  data.frame(RSE = format(rse, digits = 2))
}

modelsummary(reg1) 
(1)
(Intercept) 29.600
(1.230)
disp -0.041
(0.005)
Num.Obs. 32
R2 0.718
R2 Adj. 0.709
AIC 170.2
BIC 174.6
Log.Lik. -82.105
F 76.513
RMSE 3.15
RSE 3.3

Created on 2023-12-13 with reprex v2.0.2