R Stargazer:如何在笔记中将动态截止与固定字符向量结合起来

R Stargazer: How to combine dynamic cutoffs with fixed character vector in notes

一段时间以来,我一直被以下问题困扰,慢慢地我变得绝望,因为我无法找到解决问题的方法。我面临以下问题:

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

*p**p***p<0.01

不过,我更喜欢类似于以下的布局:

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

我想通过动态提取截断值并结合固定字符向量来获得它。观星者手册说:

> 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 文件中的 [***] 或抛出错误。

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

这是一个有趣的问题。检查代码后,我认为出现问题是因为用适当的截止值替换 [***][**] 等的代码出现在 before 自定义 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