R:在观星者桌上进行强大的SE和模型诊断

avo*_*do1 7 latex r stargazer

我尝试使用stargazer包将来自AER包的ivreg生成的一些2SLS回归输出放入Latex文档中.我有几个问题,但我似乎无法解决自己.
1.我无法想象如何插入ivreg摘要提供的模型诊断.即弱仪器测试,Wu-Hausmann和Sargan测试.我想把它们与通常在表格下面报告的统计数据一样,如观察数量,R平方和Resid.SE.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))
Run Code Online (Sandbox Code Playgroud)

lan*_*oni 7

这是您想要做的一种方法:

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)) ))
Run Code Online (Sandbox Code Playgroud)

这将产生:

==========================================================
                                  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
Run Code Online (Sandbox Code Playgroud)

如您所见,这允许在各个模型中手动包括诊断。


您可以通过创建一个接受一系列模型(例如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)
Run Code Online (Sandbox Code Playgroud)

都将参加一个listcoeftest对象,并得到社会企业通过矢量预期se

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

[[2]]
(Intercept)           x           a 
  4.2221491   1.0016012   0.2434801
Run Code Online (Sandbox Code Playgroud)

可以对诊断执行相同的操作:

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)
Run Code Online (Sandbox Code Playgroud)

这两个调用都会产生:

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

$`Wu-Hausman`
[1] "Wu-Hausman" "0.11"       "0.18"      
Run Code Online (Sandbox Code Playgroud)

现在,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))
Run Code Online (Sandbox Code Playgroud)