R Stargazer:如何在notes中将动态截断与固定字符向量相结合

R Stargazer:如何在notes中将动态截断与固定字符向量相结合,r,stargazer,R,Stargazer,一段时间以来,我一直被以下问题困扰着,慢慢地我变得绝望,因为我无法找到解决问题的方法。我面临以下问题: 使用Stargazer生成HTML回归结果表时,注释部分显示了显著性截止值,如下所示: *p**p***p<0.01 我想通过动态提取截止值并结合固定的字符向量来实现这一点。星探手册上说: > a character vector containing notes to be included below the table. > The character strings

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

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

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


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

这是一个有趣的问题。检查代码后,我认为问题出现了,因为在应用自定义
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.(
函数(注意末尾的点):


这是一个有趣的问题。检查代码后,我认为问题出现了,因为在应用自定义
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.(
函数(注意末尾的点):

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.(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