Removing model names in modelsummary - r

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)

Related

Removing table components in stargazer

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)

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

Append two regressions in one simple column with stargazer

I would like to use stargazer adding a new regression but I'm not want add a new column, but that were in the same column. I tried to do some replicable example:
reg1 <- lm(mpg ~ wt + factor(am), data = mtcars)
reg2 <- lm(mpg ~ wt + factor(gear), data = mtcars)
stargazer::stargazer(reg1,reg2,
title="Results", align=TRUE , digits = 3,out="table2.tex",append=T,
keep = c("am","gear"))
Current output
Desired output:
It is not possible to do this with any specific parameter with stargazer, so you will have to modify your LaTeX code manually.

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.

Stargazer: omit stars for constant only

Sometimes it's tacky to include statistical significance stars for the constant term when reporting the results of a regression. Is it possible to configure stargazer to keep stars for the regressors, but not for the constant term?
fit <- lm(rating ~ complaints, data=attitude)
stargazer(fit)
Basically, the answer turned out to be using stargazer's p argument. From there, I just needed to write a (series of) function(s) that took a list of regression fits and returned a list of vectors of p-values. I then manually changed the p-value of the intercepts to be 1, and presto, no tacky stars on the intercept. Plus it's reproducible with no manual LaTeX editing!
commarobust <- function(fit){
require(sandwich)
require(lmtest)
coeftest(fit,vcovHC(fit, type="HC2"))
}
getrobustps <- function(fit){
robustfit <- commarobust(fit)
ps <- robustfit[,4]
ps["(Intercept)"] <- 1
return(ps)
}
makerobustpslist <- function(fitlist){
return(lapply(fitlist, FUN=getrobustps) )
}
Then in the stargazer call:
stargazer(fit_1, fit_2, fit_3, fit_4, fit_5,
p=makerobustpslist(list(fit_1, fit_2, fit_3, fit_4, fit_5)))
Works like a charm.
You could alternatively use the broom package to convert the fit results to a data frame, and then add stars to your heart's content:
library("broom")
mod <- lm(mpg ~ wt + qsec, data = mtcars)
DF <- tidy(mod)
DF$stars <- c("", "***", "***") # inspect and add manually, or automate
And the xtable package could be used to format it for LaTeX or whatever.

Resources