簡體   English   中英

R:觀星表中的穩健 SE 和模型診斷

[英]R: Robust SE's and model diagnostics in stargazer table

我嘗試使用 stargazer 包將通過ivreg()從 AER 包生成的一些 2SLS 回歸輸出放入 Latex 文檔中。 但是,我有幾個問題似乎無法自己解決。

  1. 我無法弄清楚如何插入ivreg()摘要提供的模型診斷。 即弱儀器測試、Wu-Hausmann 和 Sargan 測試。 我想讓它們帶有通常在表格下方報告的統計數據,例如觀察次數、R 平方和殘差。 東南。 stargazer 函數似乎沒有參數,您可以在其中提供帶有附加診斷的列表。 我沒有把這個放在我的例子中,因為老實說我不知道​​從哪里開始。
  2. 我想用健壯的標准錯誤交換正常的標准錯誤,我發現做到這一點的唯一方法是生成具有健壯標准錯誤的對象,並使用se=list()將它們添加到stargazer()函數中。 我把它放在下面的最小工作示例中。 是否有更優雅的方式來編碼這個或者重新估計模型並用健壯的標准錯誤保存它?
library(AER)
library(stargazer)

y <- rnorm(100, 5, 10)
x <- rnorm(100, 3, 15)
z <- rnorm(100, 3, 7)
a <- rnorm(100, 1, 7)
b <- rnorm(100, 3, 5)

# Fitting IV models
fit1 <- ivreg(y ~ x + a  |
             a + z,
             model = TRUE)
fit2 <- ivreg(y ~ x + a  |
             a + b + z,
             model = TRUE)

# Here are the se's and the diagnostics i want
summary(fit1, vcov = sandwich, diagnostics=T)
summary(fit2, vcov = sandwich, diagnostics=T)

# Getting robust se's, i think HC0 is the standard
# used with "vcov=sandwich" from the  above summary
cov1        <- vcovHC(fit1, type = "HC0")
robust1     <- sqrt(diag(cov1))
cov2        <- vcovHC(fit2, type = "HC0")
robust2     <- sqrt(diag(cov1))

# Create latex table
stargazer(fit1, fit2, type = "latex", se=list(robust1, robust2))

這是做你想做的一種方法:

require(lmtest)

rob.fit1        <- coeftest(fit1, function(x) vcovHC(x, type="HC0"))
rob.fit2        <- coeftest(fit2, function(x) vcovHC(x, type="HC0"))
summ.fit1 <- summary(fit1, vcov. = function(x) vcovHC(x, type="HC0"), diagnostics=T)
summ.fit2 <- summary(fit2, vcov. = function(x) vcovHC(x, type="HC0"), diagnostics=T)

stargazer(fit1, fit2, type = "text", 
          se = list(rob.fit1[,"Std. Error"], rob.fit2[,"Std. Error"]), 
          add.lines = list(c(rownames(summ.fit1$diagnostics)[1], 
                             round(summ.fit1$diagnostics[1, "p-value"], 2), 
                             round(summ.fit2$diagnostics[1, "p-value"], 2)), 
                           c(rownames(summ.fit1$diagnostics)[2], 
                             round(summ.fit1$diagnostics[2, "p-value"], 2), 
                             round(summ.fit2$diagnostics[2, "p-value"], 2)) ))

這將產生:

==========================================================
                                  Dependent variable:     
                              ----------------------------
                                           y              
                                   (1)            (2)     
----------------------------------------------------------
x                                 -1.222        -0.912    
                                 (1.672)        (1.002)   

a                                 -0.240        -0.208    
                                 (0.301)        (0.243)   

Constant                          9.662         8.450**   
                                 (6.912)        (4.222)   

----------------------------------------------------------
Weak instruments                   0.45          0.56     
Wu-Hausman                         0.11          0.18     
Observations                       100            100     
R2                                -4.414        -2.458    
Adjusted R2                       -4.526        -2.529    
Residual Std. Error (df = 97)     22.075        17.641    
==========================================================
Note:                          *p<0.1; **p<0.05; ***p<0.01

如您所見,這允許在相應模型中手動包含診斷。


您可以通過創建一個接受模型list(summ.fit1, summ.fit2)例如list(summ.fit1, summ.fit2) )並輸出seadd.lines參數所需的對象的函數來自動化這種方法。

gaze.coeft <- function(x, col="Std. Error"){
    stopifnot(is.list(x))
    out <- lapply(x, function(y){
        y[ , col]
    })
    return(out)
}
gaze.coeft(list(rob.fit1, rob.fit2))
gaze.coeft(list(rob.fit1, rob.fit2), col=2)

都將接收coeftest對象list ,並產生se預期的 SE 向量:

[[1]]
(Intercept)           x           a 
  6.9124587   1.6716076   0.3011226 

[[2]]
(Intercept)           x           a 
  4.2221491   1.0016012   0.2434801

診斷也可以這樣做:

gaze.lines.ivreg.diagn <- function(x, col="p-value", row=1:3, digits=2){
    stopifnot(is.list(x))
    out <- lapply(x, function(y){
        stopifnot(class(y)=="summary.ivreg")
        y$diagnostics[row, col, drop=FALSE]
    })
    out <- as.list(data.frame(t(as.data.frame(out)), check.names = FALSE))
    for(i in 1:length(out)){
        out[[i]] <- c(names(out)[i], round(out[[i]], digits=digits))
    }
    return(out)
}
gaze.lines.ivreg.diagn(list(summ.fit1, summ.fit2), row=1:2)
gaze.lines.ivreg.diagn(list(summ.fit1, summ.fit2), col=4, row=1:2, digits=2)

兩個調用都會產生:

$`Weak instruments`
[1] "Weak instruments" "0.45"             "0.56"            

$`Wu-Hausman`
[1] "Wu-Hausman" "0.11"       "0.18"      

現在stargazer()調用變得如此簡單,產生與上面相同的輸出:

stargazer(fit1, fit2, type = "text", 
      se = gaze.coeft(list(rob.fit1, rob.fit2)), 
      add.lines = gaze.lines.ivreg.diagn(list(summ.fit1, summ.fit2), row=1:2))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM