Removing table components in stargazer - r

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)

Related

Formatting regression results in R for latex

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)

Removing model names in modelsummary

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)

Is there a way to change the capitalization in modelsummary for fixed effects without renaming the columns?

I want to have my fixed effects capitalized in the modelsummary output. While I know I could rename the columns before estimating the regression, I wanted to know if there is an easier way to do this within the modelsummary::modelsummary function. I have tried the coef_map argument, but it doesn't seem to affect the table.
library(tidyverse)
library(modelsummary)
library(fixest)
mtcars %>%
feols(mpg ~hp | gear, data = .) %>%
modelsummary(stars = T, coef_map = c("hp" = "Horsepower",
"mpg" = 'MPG',
"gear" = "Gear"))
As shown in the example, I want the "gear" fixed effect to be capitalized without having to rename in my data (if possible).
A modelsummary table is essentially composed of two parts. The top contains estimates and is controlled by the coef_* arguments. The bottom contains “goodness-of-fit” and other model characteristics, and is controlled by the gof_* arguments.
You can rename and reformat and reorder the GOF info with the gof_map argument. In this example, I use the tribble function to create a data.frame with the information needed to rename and capitalize. You can also use a list, or a plain data.frame. Please refer to the documentation.
library(tidyverse)
library(modelsummary)
library(fixest)
gm <- tribble(~raw, ~clean, ~fmt,
"FE: gear", "FE: Gear", ~fmt)
mtcars %>%
feols(mpg ~hp | gear, data = .) %>%
modelsummary(output = "markdown",
stars = T, coef_map = c("hp" = "Horsepower",
"mpg" = 'MPG',
"gear" = "Gear"),
gof_map = gm)
Model 1
Horsepower
-0.067*
(0.016)
FE: Gear
X
Note:
^^ + p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

Why does stargazer produce different dependent variable labels for these two models?

I am wondering if someone could tell me why stargazer producers different dependent variable labels for the two models below:
j <- "hp"
i <- "cyl"
lm1 <- lm(paste(j, "~", i), mtcars)
stargazer(lm1, header = FALSE, type = "text")
produces dependent variable j
whereas:
model <- paste(j, "~", i)
lm2 <- lm(model, mtcars)
stargazer(lm2, header = FALSE, type = "text")
produces the correct dependent variable hp
It seems stargazer picks the dependent variable labels from the call attribute of the model, so when line 51 from the internal code (https://github.com/cran/stargazer/blob/master/R/stargazer-internal.R) is executed, it reads j instead of hp
You can see that if you copy the call atribute from lm2 to lm1 now you get it:
lm1$call <- lm2$call
stargazer(lm1, header = FALSE, type = "text")

Custom model names in Stargazer package for R

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.

Resources