【问题标题】:How to cluster standard error in clubSandwich's vcovCR()?如何在 clubSandwich 的 vcovCR() 中聚类标准错误?
【发布时间】:2019-05-01 09:37:39
【问题描述】:

我试图在 plm 之后使用 clubSandwich 包中的 vcovCR() 为我的模拟数据(我用于功率模拟)指定一个集群变量,但我收到以下错误消息: “[.data.frame(eval(mf$data, envir), , index_names) 中的错误:选择了未定义的列”

我不确定这是否特定于 vcovCR() 或关于 R 的一般内容,但谁能告诉我我的代码有什么问题? (我在How to cluster standard errors of plm at different level rather than id or time?看到了一个相关的帖子,但它并没有解决我的问题)。

我的代码:

N <- 100;id <- 1:N;id <- c(id,id);gid <- 1:(N/2);
gid <- c(gid,gid,gid,gid);T <- rep(0,N);T = c(T,T+1)
a <- qnorm(runif(N),mean=0,sd=0.005)
gp <- qnorm(runif(N/2),mean=0,sd=0.0005)
u <- qnorm(runif(N*2),mean=0,sd=0.05)
a <- c(a,a);gp = c(gp,gp,gp,gp)
Ylatent <- -0.05*T + a + u
Data <- data.frame(
  Y = ifelse(Ylatent > 0, 1, 0),
  id = id,gid = gid,T = T
)
library(clubSandwich)
library(plm)
fe.fit <- plm(formula = Y ~ T, data = Data, model = "within", index = "id",effect = "individual", singular.ok = FALSE)
vcovCR(fe.fit,cluster=Data$id,type = "CR2") # doesn't work, but I can run this by not specifying cluster as in the next line
vcovCR(fe.fit,type = "CR2")
vcovCR(fe.fit,cluster=Data$gid,type = "CR2") # I ultimately want to run this

【问题讨论】:

  • 对我来说看起来像是 clubSandwich 中的一个错误,并且已经在此处报告:github.com/jepusto/clubSandwich/issues/38
  • 谢谢。但它适用于其他数据集。例如:library(clubSandwich) library(plm) data("Produc", package = "plm") plm_FE &lt;- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, data = Produc, index = c("state","year"), effect = "individual", model = "within") vcovCR(plm_FE, type="CR2",cluster = Produc$state)

标签: r plm standard-error


【解决方案1】:

首先将您的数据设为pdata.frame。这更安全,特别是如果您想自动创建时间索引(看起来就是这种情况)。

继续你所拥有的:

pData <- pdata.frame(Data, index = "id") # time index is created automatically
fe.fit2 <- plm(formula = Y ~ T, data = pData, model = "within", effect = "individual")
vcovCR(fe.fit2, cluster=Data$id,type = "CR2")
vcovCR(fe.fit2, type = "CR2")
vcovCR(fe.fit2,cluster=Data$gid,type = "CR2")

由于 plm 对象的 clubSandwich 的数据提取函数 get_index_order(从版本 0.3.3 起)中的错误,您的示例无法正常工作。它假定两个索引变量都在原始数据中,但在您的示例中不是这种情况,时间索引是通过仅通过 index 参数指定单个维度来自动创建的。

【讨论】:

    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2021-10-24
    • 2016-01-14
    相关资源
    最近更新 更多