I'm wondering how to get custom model names in the stargazer package for R.
There is an option for model.names which can be set to TRUEor FALSE, but it does not support a vector or names such as model.names = c('OLS','2SLS','GLS').
Is there any way to override the function to use custom names passed as parameters instead of reading the model names from the objects passed?
Stargazer optionally includes the object names, so if you models are
m1 = lm(mpg ~ wt, data = mtcars)
m2 = lm(mpg ~ wt + disp, data = mtcars)
You can do
stargazer(m1, m2, object.names = TRUE,
column.labels = c("lab 1", "lab 2e"))
to get both custom labels and the object names, m1 and m2. This can be usefully abused by using non-standard names matching the extra model names that you want
OLS = m1
`2SLS` = m2
stargazer(OLS, `2SLS`, object.names = TRUE,
column.labels = c("lab 1", "lab 2e"))
Though, unfortunately, the backticks are included in the output. (As an additional hack you could capture.output() and remove them with gsub).
The model names used by stargazer are not part of the model object, rather stargazer examines the model object and attempts to extract them. You can see the .model.identify function on github. You could attempt to fixInNamespace to adjust this, but I think a post-hoc hack is easier.
Related
I'm attempting to report the model summary from mgcv::gam() using the modelsummary package. The flextable package provides a summary that is consistent with the summary output in R and what is often presented in publications. It separates out reporting for the fixed/parametric effects and the smooth terms.
Although flextable works well, I'd like to use modelsummary (mainly for it's ability to output to gt, kable, etc.). My plan was to produce two separate tables and report the appropriate data for parametric and smooth terms separately (there might be a better way?). However, I get hung up trying to omit coefficients in modelsummary().
Flextable example:
library(mgcv)
library(flextable)
library(modelsummary)
dat <- gamSim(1, n = 4000, dist = "normal", scale = 2)
mod <- gam(y ~ s(x0) + s(x1) + s(x2), data = dat)
flextable::as_flextable(mod)
My first step at getting the summary for parametric terms using modelsummary():
modelsummary(mod,
estimate = "estimate",
statistic = c("Std.Error" = "std.error",
"t-value" = "statistic",
"p-value" = "p.value"),
shape = term ~ model + statistic,
gof_map = NA)
I want to drop the smooth terms and include those in a different table or group, so I tried the coef_omit argument:
modelsummary(mod,
estimate = "estimate",
statistic = c("Std.Error" = "std.error",
"t-value" = "statistic",
"p-value" = "p.value"),
coef_omit = "^(?!.*Intercept)", #this should retain the intercept term
omit = ".*",
shape = term ~ model + statistic,
gof_map = NA)
Error in if (dat$part[i] == "estimates" && dat[[column]][i - 1] == dat[[column]][i]) { :
missing value where TRUE/FALSE needed
Interestingly, if I remove the shape argument to report statistics in "long format" the error goes away. I might be approaching formatting this summary completely wrong and am open to suggestions.
I am trying to create a regression results table in R for latex. I would like this table to have two separate columns: one for the estimates and one for the standard error. The following code
library(fixest)
library(car)
library(pander)
##Using the built-in CO2 data frame, run regression
i<- feols(conc ~ uptake + Treatment | Type, CO2, vcov = "hetero")
summary(i)
##Create regression table for latex
etable(i, postprocess.df = pandoc.table.return, style = "rmarkdown")
my_style = style.df(depvar.title = "", fixef.title = "",
fixef.suffix = " fixed effect", yesNo = "yes", default = TRUE)
setFixest_etable(style.df = my_style, postprocess.df = pandoc.table.return)
etable(i, style = "rmarkdown", caption = "New default values", se.below = NULL )
etable(i, tex = TRUE)
print(etable(i, tex = TRUE), file = "filename2.tex")
When put into Latex document on overleaf.com the following image is produced.
How can I alter my above code to have estimates and standard error in different columns in my table?
You can try the following:
modelsummary(your_regression, fmt=2,
estimate = c("{estimate}{stars} (std.error)"),
statistic = c(),
output = "latex")
What I like with modelsummary too, is that it enable to put many different model as a list to compare them.
Thanks to #léo-henry for suggesting modelsummary. I just wanted to point out that in the latest version of the package you can use the shape argument to display the estimates and standard errors (or other statitics) side-by-side. You will find details here: https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#shape
Here is a minimal example:
library(modelsummary)
mod <- lm(mpg ~ hp + drat, data = mtcars)
modelsummary(mod, shape = term ~ model + statistic)
I am putting together tables using stargazer and presenting them in a HTML file using RMarkdown. Reproducible code to create the image is pasted below.
I would like to remove the stars and standard errors associated with the constant ("Alpha" in the image below).
I understand that I can manually overwrite the table components but is there a way that I can automatically retain stars and SE for everything but the alpha row? To re-write everything into character vectors seems a little cumbersome.
Code:
library(tidyverse)
library(stargazer)
mdl1 <- lm(mpg~wt, mtcars)
mdl2 <- lm(mpg~disp, mtcars)
mdls <- list(mdl1,mdl2)
column.labels <- c('model 1',
'model 2')
covariate.labels <- c('Beta 1',
'Beta 2',
'Alpha')
keep.stat <- c('n')
stargazer(mdls, type = 'html',
column.labels = column.labels,
covariate.labels = covariate.labels,
column.sep.width = "10pt",
dep.var.labels.include = F,
keep.stat = keep.stat)
In a comment you asked if an alternative to stargazer could help you
with this. The answer is “yes”, the modelsummary
package can do this
relatively easily. (Disclaimer: I am the maintainer.)
I say “relatively” because what you are asking is very idiosyncratic, so
I don’t think you should expect it to work out of the box in any
package. But here’s an example.
First, we start with a basic table with nice labels:
library(modelsummary)
library(broom)
models <- list(
lm(mpg ~ wt + hp, mtcars),
lm(mpg ~ disp + hp, mtcars))
coef_map <- c(
"wt" = "Weight",
"disp" = "Displacement",
"hp" = "Horse Power")
modelsummary(models, stars = TRUE, coef_map = coef_map)
The Customizing Existing Models
section
of the documentation explains that modelsummary allows you to
overwrite any estimate (coef, standard error, p value, etc.) by defining
a new function called tidy_custom.CLASSNAME, where CLASSNAME refers
to the type of model object you are trying to summarize.
In our example above, we summarize lm models:
class(models[[1]])
## [1] "lm"
Therefore, our customizing function will be called tidy_custom.lm. Say
your goal is to remove the standard errors, stars, and p values from the
table, but only for the variable hp. What we can do is overwrite the
estimates with NA (please refer to the docs linked above for a
detailed explanation):
tidy_custom.lm <- function(model) {
out <- tidy(model)
out$p.value[out$term == "hp"] <- NA
out$std.error[out$term == "hp"] <- NA
return(out)
}
modelsummary(models, stars = TRUE, coef_map = coef_map)
I wonder is it possible to remove model names entirely (and delete the row in the table). I tried setting them to NULL but that does not seem to work.
library(modelsummary)
x<-rnorm(5)
y<-rnorm(5)
models<-list(lm(y~x),lm(y~x))
names(models)<-NULL
#This still produces models with names
modelsummary(models)
An option may be to set the names to blank ("")
names(models) <- rep("", length(models))
modelsummary(models)
-output
Deleting row - if it is coefficient, use `coef_omit
modelsummary(models, coef_omit = "x")
and if there are other parameters to be removed, can also use a regex in gof_omit
modelsummary(models, gof_omit = "AIC|BIC")
In the development version of modelsummary (version >0.9.4), all the extra arguments that you pass to modelsummary will be pushed through the ellipsis (...) automatically to kableExtra::kbl(). This means that you can use the col.names=NULL argument to get this:
library(remotes)
install_github("vincentarelbundock/modelsummary")
library(modelsummary)
mod <- list(
lm(mpg ~ hp, mtcars),
lm(mpg ~ hp + drat, mtcars)
)
modelsummary(mod, col.names = NULL)
I would like to create a table like the tables from the stargazer Package.
But using the Gravity Package for creating gravity models, this package isn´t supported by the stargazer package yet.
Do you have an idea, how to create a similar table with 3-5 models side by side for better comparison?
Output should look like this, just with gravity models from the gravity package in r:
Desired Output Style:
Please provide an example of model object created by gravity package.
Alternatively,
I will show one approach that can be used: stargazer is really nice and you CAN even create table like above even with the model objects that are not yet supported, e.g. lets say that quantile regression model is not supported by stargazer (even thought is is):
Trick is, you need to be able to obtain coefficients and standart error e.g. as vector. Then supply stargazer with model object that is suppoerted e.g. lm as a template and then mechanically specify which coefficients and standart errors should be used:
library(stargazer)
library(tidyverse)
library(quantreg)
df <- mtcars
model1 <- lm(hp ~ factor(gear) + qsec + disp, data = df)
quantreg <- rq(hp ~ factor(gear) + qsec + disp, data = df)
summary_qr <- summary(quantreg, se = "boot")
# Standart Error for quant reg
se_qr = c(211.78266, 29.17307, 58.61105, 9.70908, 0.12090)
stargazer(model1, model1,
coef = list(NULL, summary_qr$coefficients),
se = list(NULL, se_qr),
type = "text")