【问题标题】:how to create one output with multiple linear regression models including Cluster-Robust Standard Errors in Stargazer如何使用多个线性回归模型创建一个输出,包括 Stargazer 中的集群稳健标准误差
【发布时间】:2019-12-28 08:10:43
【问题描述】:

我知道如何创建线性回归模型lm 以及如何使用summary 函数获取聚集的标准误差并将它们添加到stargazer 输出中:

# estimate models
ols1 <- lm(y ~ x) 

# summary with cluster-robust SEs
summary(ols1, cluster="cluster_id") 

# create table in stargazer
stargazer(ols1, se=list(coef(summary(ols1,cluster = c("cluster_id")))[, 2]), type = "text")

如果我想创建一个包含多个回归模型和聚集标准错误的 stargazer 输出,有人知道代码应该是什么样子吗?

代码逻辑如下:

1 步:创建 lm 模型

ols1 <- lm(y ~ x) 
ols2 <- lm(y ~ x + z) 
ols3 <- lm(y ~ x + z + a) 
ols2 <- lm(y ~ x + z + a + b) 

2 步:包含标准错误

summary(ols1, cluster="cluster_id")
summary(ols2, cluster="cluster_id")
summary(ols3, cluster="cluster_id")
summary(ols4, cluster="cluster_id")

3 步:使用 4 个不同的模型创建一个输出

stargazer(ols1,ols2,ols3,ols4, type="html", dep.var.labels=c("ROA"), intercept.bottom = FALSE,
          out="OLS1")

我认为 step 1step 2 并不重要,但我不知道如何设置 step 3 的代码。

第3步中不知道如何实现以下代码:

# create table in stargazer
stargazer(ols1, se=list(coef(summary(ols1,cluster = c("cluster_id")))[, 2]), type = "text")

谁能帮忙?

非常感谢!!!

【问题讨论】:

    标签: r regression stargazer standard-error


    【解决方案1】:

    您已经看到选项se 可以处理list。因此,只需将强大的 SE 保存在其他易于访问的地方,例如:

    library("sandwich")
    library("plm")
    library("stargazer")
    
    data("Produc", package = "plm")
    
    # Regression    
    model1 <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
                 data = Produc, 
                 index = c("state","year"),
                 method="pooling")
    
    model2 <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp),
                  data = Produc, 
                  index = c("state","year"),
                  method="pooling")
    
    # Adjust standard errors
    cov1         <- vcovHC(model1, type = "HC1")
    cov2         <- vcovHC(model2, type = "HC1")
    robust_se1    <- sqrt(diag(cov1))
    robust_se2    <- sqrt(diag(cov2))
    
    # Stargazer output (with and without RSE)
    stargazer(model1, model2, type = "text",
              se = list(robust_se1, robust_se2))
    

    现在,您可以在stargazer 中拥有任何具有强大 SE 的模型,它们彼此相邻。

    【讨论】:

      猜你喜欢
      • 2017-10-17
      • 1970-01-01
      • 2020-03-15
      • 2014-05-02
      • 2016-12-16
      • 2020-03-14
      • 2020-04-17
      • 2022-01-07
      • 2013-05-06
      相关资源
      最近更新 更多