Potential bug in stargazer omit.labels

ぃ、小莉子 提交于 2020-06-10 12:58:07

问题


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?


回答1:


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")
    )
)



回答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"))


来源:https://stackoverflow.com/questions/38355337/potential-bug-in-stargazer-omit-labels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!