R:观星台中强大的SE和模型诊断

标签 r latex stargazer

我尝试使用stargazer软件包将通过AER软件包中的ivreg()生成的一些2SLS回归输出放到Latex文档中。我有几个问题,但是我似乎无法解决自己。

  • 我无法弄清楚ivreg()摘要提供的如何插入模型诊断。即弱仪器测试,Wu-Hausmann和Sargan测试。我希望它们具有通常在表格下方报告的统计信息,例如观测值数量,R平方和残差。 SE。 stargazer函数似乎没有参数,您可以在其中提供带有其他诊断信息的列表。我之所以没有将其纳入示例,是因为我真的不知道从哪里开始。
  • 我想将普通标准错误与健壮的标准错误交换,而我发现做到这一点的唯一方法是产生具有健壮标准错误的对象,并使用stargazer()将它们添加到se=list()函数中。我将其放在下面的最小工作示例中。是否可以使用一种更优雅的方式对此进行编码,或者重新估计模型并将其保存为可靠的标准错误?
  • 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))并输出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)
    

    都将接受list对象的coeftest,并产生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))
    

    关于R:观星台中强大的SE和模型诊断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048097/

    相关文章:

    emacs - 在 emacs 窗口中居中文本

    算法流程图中的 latex 钻石符号

    R函数在删除重复项的同时计算数量

    r - 迭代更新字符串匹配模式

    r - 如何缩进新段落?

    r - R观星台中的小数

    r - 马尔可夫切换回归 : Standard errors of the msmFit and receiving Latex Output

    r - 在R中为H2O模型打印 “pretty”表

    r - 包含唯一值数字的列

    R - 帮助将因子转换为日期 (%m/%d/%Y %H :%M)