How to transpose a regression output with modelsummary package? - r

I JUST found out about this amazing R package, modelsummary.
It doesn't seem like it offers an ability to transpose regression outputs.
I know that you cannot do a tranposition within kable-extra, which is my go-to for ordinary table outputs in R. Since modelsummary relies on kable-extra for post-processing, I'm wondering if this is possible. Has anyone else figured it out?
Ideally I'd like to preserve the stars of my regression output.
This is available in STATA (below):
Thanks in advance!

You can flip the order of the terms in the group argument formula. See documentation here and also here for many examples.
library(modelsummary)
mod <- list(
lm(mpg ~ hp, mtcars),
lm(mpg ~ hp + drat, mtcars))
modelsummary(mod, group = model ~ term)
(Intercept)
hp
drat
Model 1
30.099
-0.068
(1.634)
(0.010)
Model 2
10.790
-0.052
4.698
(5.078)
(0.009)
(1.192)
The main problem with this strategy is that there is not (yet) an automatic way to append goodness of fit statistics. So you would probably have to rig something up by creating a data.frame and feeding it to the add_columns argument. For example:
N <- sapply(mod, function(x) get_gof(x)$nobs)
N <- data.frame(N = c(N[1], "", N[2], ""))
modelsummary(mod,
group = model ~ term,
add_columns = N,
align = "lcccc")
(Intercept)
hp
drat
N
Model 1
30.099
-0.068
32
(1.634)
(0.010)
Model 2
10.790
-0.052
4.698
32
(5.078)
(0.009)
(1.192)
If you have ideas about the best default behavior for goodness of fit statistics, please file a feature request on Github.

Related

Adding coefficient group titles to modelsummary output

I'm trying to group coefficients together in a modelsummary output table and add row titles for these groups :
library(modelsummary)
ols1 <- lm(mpg ~ cyl + disp + hp,
data = mtcars,
na.action = na.omit
)
modelsummary(ols1,
title = "Table 1",
stars = TRUE
)
The modelsummary documentation (https://cran.r-project.org/web/packages/modelsummary/modelsummary.pdf) suggests this might be something to do with the shape and group_map arguments, but I can't really figure out how to use them.
Any guidance would be very helpful, thanks!
When the documentation mentions “groups”, it refers to models like multinomial logits where each predictor has one coefficient per outcome level. In this example, the “group” column is called “response”:
library(nnet)
library(modelsummary)
mod <- multinom(cyl ~ mpg + hp, data = mtcars, trace = FALSE)
modelsummary(
mod,
output = "markdown",
shape = response ~ model)
Model 1
(Intercept)
6
0.500
(41.760)
8
8.400
(0.502)
mpg
6
-83.069
(416.777)
8
-120.167
(508.775)
hp
6
16.230
(81.808)
8
20.307
(87.777)
Num.Obs.
32
R2
1.000
R2 Adj.
0.971
AIC
12.0
BIC
20.8
RMSE
0.00
What you probably mean is something different: Adding manual labels to sets of coefficients. This is easy to achieve because modelsummary() produces a kableExtra or a gt table which can be customized in infinite ways.
https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html
For example, you may want to look at the group_rows function from kableExtra:
library(kableExtra)
mod <- lm(mpg ~ cyl + disp + hp, data = mtcars)
modelsummary(mod) |>
group_rows(index = c("Uninteresting" = 4,
"Interesting" = 4,
"Other" = 7))

how to select coefficient in texreg in an easier way

I am analysing a panel dataset with dummy variable fixed effects.
For example, my model looks like
model1 <- lm_robust(y ~ x1 + x2 + factor(x3):factor(x3), clusters = clusters, data = data)
While there are a lot of categories of x3 and x4 generating hundreds of dummies in the table which I do not want to show.
Is there an easier way with texreg to keep only the estimates of x1 and x2 in the output table?
As I am using lm_robust for clustering, so stargazer is not the option here.
Thank you very much in advance.
I do not know how to do this in texreg, but here is one option using
the modelsummary package for
R (disclaimer: I
am the author).
You can use the coef_map argument to select the coefficients to
display. You may also want to look at the coef_omit argument.
library(estimatr)
library(modelsummary)
mod <- lm_robust(hp ~ ., data = mtcars)
modelsummary(mod, coef_map = c("cyl", "disp"))
Model 1
cyl
8.204
(10.203)
disp
0.439
(0.179)
Num.Obs.
32
R2
0.903
R2 Adj.
0.857
Std.Errors
HC2

How to hide certain lines from modelsummary of regression analysis

I have a regression output as follows:
regression1 <- lm(cnt ~ temperature + weathersit + humidity + windvelocity, data=captialbikedata)
modelsummary(regression1)
I am using modelsummary in order to display it in a table in markdown.
I want to hide the following rows of the regression output:
AIC
BIC
Log.Lik.
F
How would I do that?
The gof_omit allows you to omit goodness-of-fit statistics, which is what modelsummary calls all the statistics reported in the bottom section of the table.
This argument accepts "regular expressions", which allows you to use partial matches and a variety of other tricks. One nice trick is to use the vertical bar (|, meaning "OR") to say that you want to omit any one of many patterns.
library("modelsummary")
mod <- lm(hp ~ mpg, mtcars)
modelsummary(mod, gof_omit = "AIC|BIC|Log|F")
Model 1
(Intercept)
324.082
(27.433)
mpg
-8.830
(1.310)
Num.Obs.
32
R2
0.602
R2 Adj.
0.589
In addition, you can omit coefficients using the coef_omit argument in a similar way. Finally, you can omit the standard errors in parentheses by setting statistic=NULL.

make R report adjusted R squared and F-test in output with robust standard errors

I have estimated a linear regression model using lm(x~y1 + y1 + ... + yn) and to counter the present heteroscedasticity I had R estimate the robust standard errors with
coeftest(model, vcov = vcovHC(model, type = "HC0"))
I know that (robust) R squared and F statistic from the "normal" model are still valid, but how do I get R to report them in the output? I want to fuse several regression output from different specifications together with stargazer and it would become very chaotic if I had to enter the non-robust model along just to get these statistics. Ideally I want to enter a regression output into stargazer that contains these statistics, thus importing it to their framework.
Thanks in advance for all answers
I don't have a solution with stargarzer, but I do have a couple of viable alternatives for regression tables with robust standard errors:
Option 1
Use the modelsummary package to make your tables.
it has a statistic_override argument which allows you to supply a function that calculates a robust variance covariance matrix (e.g., sandwich::vcovHC.
library(modelsummary)
library(sandwich)
mod1 <- lm(drat ~ mpg, mtcars)
mod2 <- lm(drat ~ mpg + vs, mtcars)
mod3 <- lm(drat ~ mpg + vs + hp, mtcars)
models <- list(mod1, mod2, mod3)
modelsummary(models, statistic_override = vcovHC)
Note 1: The screenshot above is from an HTML table, but the modelsummary package can also save Word, LaTeX or markdown tables.
Note 2: I am the author of this package, so please treat this as a potentially biased view.
Option 2
Use the estimatr::lm_robust function, which automatically includes robust standard errors. I believe that estimatr is supported by stargazer, but I know that it is supported by modelsummary.
library(estimatr)
mod1 <- lm_robust(drat ~ mpg, mtcars)
mod2 <- lm_robust(drat ~ mpg + vs, mtcars)
mod3 <- lm_robust(drat ~ mpg + vs + hp, mtcars)
models <- list(mod1, mod2, mod3)
modelsummary(models)
This is how to go about it. You need to use model object that is supported by stargazer as a template and then you can provide a list with standard errors to be used:
library(dplyr)
library(lmtest)
library(stargazer)
# Basic Model ---------------------------------------------------------------------------------
model1 <- lm(hp ~ factor(gear) + qsec + cyl + factor(am), data = mtcars)
summary(model1)
# Robust standard Errors ----------------------------------------------------------------------
model_robust <- coeftest(model1, vcov = vcovHC(model1, type = "HC0"))
# Get robust standard Errors (sqrt of diagonal element of variance-covariance matrix)
se = vcovHC(model1, type = "HC0") %>% diag() %>% sqrt()
stargazer(model1, model1,
se = list(NULL, se), type = 'text')
Using this approach you can use stargazer even for model objects that are not supported. You only need coefficients, standard errors and p-values as vectors. Then you can 'mechanically insert' even unsupported models.
One last Note. You are correct that once heteroskedasticity is present, Rsquared can still be used. However, overall F-test as well as t-tests are NOT valid anymore.

Linear regression output "Call Formula" can't be exported to csv file

I'm working on a for function that uses the input data to generate several linear regression models, a~c. This data generate works perfectly fine, but, I need to export the data resulting to a csv file. I'm using the tydy() and glance () functions to obtain p values, intercepts, r2 etc. That part of the code works fine, but, the output file does not provide me with the "Call Formula:" of the linear regresion, so I'm having problem to interpret the out... Can someone tell me, please, how to make the call formula become the header of the csv file?.
I don't think that csv files have a singular header that you can save the call to but if you simply want to capture the call to lm with the results then (using mtcars data since you didn't provide much context):
library(broom)
m = lm(disp ~ hp + cyl, data = mtcars)
xxx<-tidy(m)
xxx$call<-toString(m$call)
xxx
produces...
term estimate std.error statistic p.value call
1 (Intercept) -144.5694333 37.6522356 -3.8395976 6.173165e-04 lm, disp ~ hp + cyl, mtcars
2 hp 0.2358181 0.2578106 0.9146953 3.678948e-01 lm, disp ~ hp + cyl, mtcars
3 cyl 55.0625843 9.8975401 5.5632595 5.310020e-06 lm, disp ~ hp + cyl, mtcars

Resources