简体   繁体   English

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

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

For some time already, I am stuck with the following problem, and slowly I am getting desperate because I am unable to find a solution to my problem. 已经有一段时间了,我遇到了以下问题,慢慢地我变得绝望,因为我无法找到问题的解决方案。 I am facing the following issue: 我面临以下问题:

When producing HTML regression result tables with Stargazer, the notes section shows the significance cutoffs as follows: 使用Stargazer生成HTML回归结果表时,注释部分显示了重要性截止值,如下所示:

*p**p***p<0.01

However, I would prefer a layout similar to the following: 但是,我更喜欢类似于以下的布局:

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

I want to get this by extracting the cutoffs dynamically and combining with fixed character vectors. 我想通过动态提取截止值并结合固定的字符向量来实现这一点。 The Stargazer manual says: 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).

I now tried all possible combinations, but I always failed. 我现在尝试了所有可能的组合,但我总是失败。 I always end up with R outputting eg, [***] in the HTML file or throwing an error. 我总是最终输出例如,HTML文件中的[***]或抛出错误。

Could you help me figuring out the right code to combine fixed string values in the notes with dynamic cutoffs? 你能帮我找出合适的代码,将笔记中的固定字符串值与动态截止值结合起来吗?

This is an interesting problem. 这是一个有趣的问题。 After inspecting the code, I think the issue arises because the code that replaces [***] , [**] etc. with the appropriate cutoffs comes before the custom notes vector is applied (when it should come after). 在检查代码之后,我认为问题出现是因为在应用自定义notes向量之前 (当它应该在之后),用适当的截止值替换[***][**]等的代码。 So, the solution would be to re-arrange the code so that it comes in the correct order. 因此,解决方案是重新安排代码,使其按正确的顺序排列。 This requires a bit of code surgery. 这需要一些代码手术。 The following works for me; 以下为我工作; I am running stargazer_5.2 : 我正在运行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"))))

Results: First, before: 结果:第一,之前:

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 *** [.***] ** [.**] * [.*]

We have the problem, as you point out. 正如你所指出的那样,我们遇到了问题。 Now, using our modified stargazer.() function (note the dot at the end): 现在,使用我们修改过的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM