r - 如何在clubSandwich的vcovCR()中对标准错误进行聚类?

标签 r plm standard-error

我试图在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

最佳答案

首先将您的数据设为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")

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

关于r - 如何在clubSandwich的vcovCR()中对标准错误进行聚类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55934280/

相关文章:

r - 拟合具有稳健标准误差的模型

r - 将阴影标准误差曲线添加到ggplot2中的geom_密度

r - 根据组的第一行对组中的每一行进行变异

r - 如何将列值的下半部分移动到新创建的列中?

R:使用 plm 和 pglm 绘制面板模型预测

r - plm 中的滞后

r - 使用均值组/Fama-MacBeth 估计器的 Newey-West 标准误差

r - 如何根据在ggplot中分配的概率对单词重新排序

python - 从网站抓取表格

r - 用plm方法预测