Alex Coppock
Alex Coppock

Reputation: 2252

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)

Upvotes: 0

Views: 977

Answers (2)

Alex Coppock
Alex Coppock

Reputation: 2252

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.

Upvotes: 1

Bryan Hanson
Bryan Hanson

Reputation: 6223

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.

Upvotes: 0

Related Questions