[英]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 ‘*’
我想通過動態提取截止值並結合固定的字符向量來實現這一點。 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).
我現在嘗試了所有可能的組合,但我總是失敗。 我總是最終輸出例如,HTML文件中的[***]或拋出錯誤。
你能幫我找出合適的代碼,將筆記中的固定字符串值與動態截止值結合起來嗎?
這是一個有趣的問題。 在檢查代碼之后,我認為問題出現是因為在應用自定義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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.