R Stargazer:如何在音符中将动态截止值与固定字符向量组合起来

时间:2016-11-22 00:19:45

标签: r stargazer

已经有一段时间了,我遇到了以下问题,慢慢地我变得绝望,因为我无法找到问题的解决方案。我面临以下问题:

使用Stargazer生成HTML回归结果表时,注释部分显示了重要性截止值,如下所示:

*p**p***p<0.01

但是,我更喜欢类似于以下的布局:

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’

我希望通过动态提取截止值并结合固定的字符向量来实现此目的。 Stargazer手册说:

> a character vector containing notes to be included below the table.
> The character strings can include special substrings that will be
> replaced by the corresponding cutoffs for statistical significance
> 'stars': [*], [**], and [***] will be replaced by the cutoffs, in
> percentage terms, for one, two and three 'stars,' respectively (e.g.,
> 10, 5, and 1). Similarly, [0.*], [0.**] and [0.***] will be replaced
> by the numeric value of cutoffs for one, two and three 'stars' (e.g.,
> 0.1, 0.05, and 0.01). [.*], [.**] and [.***] will omit the leading zeros (e.g., .1, .05, .01).

我现在尝试了所有可能的组合,但我总是失败。我总是以R输出,例如,在HTML文件中输出[***]或者输出错误。

你能帮我找出合适的代码,将笔记中的固定字符串值与动态截止值结合起来吗?

1 个答案:

答案 0 :(得分:0)

这是一个有趣的问题。在检查代码之后,我认为问题出现是因为使用适当的截止值替换[***][**]等的代码来自之前自定义notes向量是应用(当它应该到来之后)。因此,解决方案是重新安排代码,使其按正确的顺序排列。这需要一些代码手术。以下为我工作;我正在运行stargazer_5.2

library(stargazer)

## Create new stargazer.wrap() to rearrange order of blocks
x <- capture.output(stargazer:::.stargazer.wrap)
idx <- c(grep("for \\(i in 1:length\\(\\.format\\.cutoffs\\)\\)", x)[2],
  grep("if \\(!is\\.null\\(notes\\)\\)", x),
  grep("if \\(!is\\.null\\(notes\\.align\\)\\)", x)[2])
eval(parse(text = paste0("stargazer.wrap <- ", paste(x[c(1:(idx[1] - 1), 
    (idx[2]):(idx[3] - 1), 
    idx[1]:(idx[2] - 1), 
    idx[3]:(length(x) - 1))], collapse = "\n"))))

## Create a new stargazer.() that uses our modified stargazer.wrap() function
x <- capture.output(stargazer)
x <- gsub(".stargazer.wrap", "stargazer.wrap", x)
eval(parse(text = paste0("stargazer. <- ", paste(x[-length(x)], collapse = "\n"))))

结果:首先,之前:

stargazer(lm(mpg ~ wt, mtcars), 
  type = "text",
  notes.append = FALSE,
  notes =  c("Signif. codes: 0 $***$ [.***] $**$ [.**] $*$ [.*]"))
# ===============================================================
#                                 Dependent variable:            
#                     -------------------------------------------
#                                         mpg                    
# ---------------------------------------------------------------
# wt                                   -5.344***                 
#                                       (0.559)                  

# Constant                             37.285***                 
#                                       (1.878)                  

# ---------------------------------------------------------------
# Observations                            32                     
# R2                                     0.753                   
# Adjusted R2                            0.745                   
# Residual Std. Error               3.046 (df = 30)              
# F Statistic                   91.375*** (df = 1; 30)           
# ===============================================================
# Note:               Signif. codes: 0 *** [.***] ** [.**] * [.*]

正如你所指出的那样,我们遇到了问题。现在,使用我们修改后的stargazer.()函数(注意末尾的点):

stargazer.(lm(mpg ~ wt, mtcars), 
  type = "text",
  notes.append = FALSE,
  notes =  c("Signif. codes: 0 $***$ [.***] $**$ [.**] $*$ [.*]"))
# ========================================================
#                             Dependent variable:         
#                     ------------------------------------
#                                     mpg                 
# --------------------------------------------------------
# wt                               -5.344***              
#                                   (0.559)               

# Constant                         37.285***              
#                                   (1.878)               

# --------------------------------------------------------
# Observations                         32                 
# R2                                 0.753                
# Adjusted R2                        0.745                
# Residual Std. Error           3.046 (df = 30)           
# F Statistic                91.375*** (df = 1; 30)       
# ========================================================
# Note:               Signif. codes: 0 *** .01 ** .05 * .1