Potential bug in stargazer omit.labels
Asked Answered
T

2

10

There appears to be a bug in version 5.2 of the stargazer package, where the omit.label functionality does not work consistently depending on the order of the included models:

library(stargazer)
library(ggplot2)
as.data.frame(data("midwest"))
fit.1 <- lm(poptotal ~ popadults, data = midwest)
fit.2 <- lm(poptotal ~ popadults + state, data = midwest)

# Works, column listed as "Yes":
stargazer(fit.2, omit = c("state"), omit.labels = c("States"))
# Does not work, both columns listed as "No":
stargazer(fit.1, fit.2, omit = c("state"), omit.labels = c("States"))
# Works, first column "Yes", second "No":
stargazer(fit.2, fit.1, omit = c("state"), omit.labels = c("States"))

Does anyone know of a workaround?

Theotheobald answered 13/7, 2016 at 15:2 Comment(1)
I emailed the package author/maintainer to alert them of this bug.Aquavit
S
5

I just manually specified dummies for each column using the add.lines property. For your example:

stargazer(fit.1, fit.2, omit = c("state"),
    add.lines = list(
        c("States", "No", "Yes")
    )
)
Stoichiometric answered 9/3, 2017 at 17:6 Comment(0)
R
2

Here's one approach, using a wrapper function to generate the add.lines values automatically. This also has (to me) a more natural syntax than having separate "omit" and "omit.labels" arguments. Plus, you can omit variables without having an indicator:

gazer<- function(...,indicate=NULL, staroptions=NULL){
dots <- list(...)

if (is.null(indicate)==FALSE) {
  indicate.lines<-sapply(names(indicate), function(indic)
    ifelse(
      sapply(dots,function(x) length(grep(indic,names(coef(x))))>0
      ) ,"Yes","No"
    )
  )
  indicate.lines<-rbind(unlist(indicate),indicate.lines)

  staroptions$omit <- c(staroptions$omit,names(indicate))
  staroptions$add.lines <- c(split(indicate.lines,rep(1:ncol(indicate.lines), each=nrow(indicate.lines))),staroptions$add.lines)
}
do.call(stargazer,c(dots,staroptions))
}

You provide a list of names and labels in indicate() and all your other stargazer options in a list in staroptions

For your example:

gazer(fit.1,fit.2,indicate=list(state="State"))
Ruthful answered 10/4, 2018 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.