简体   繁体   中英

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:

*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:

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

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). 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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