我试图理解标准错误“聚类”以及如何在R中执行(在Stata中是微不足道的)。在RI中使用plm
或编写我自己的函数均未成功。我将使用包装中的diamonds
数据ggplot2
。
我可以使用任一虚拟变量进行固定效果
> library(plyr)
> library(ggplot2)
> library(lmtest)
> library(sandwich)
> # with dummies to create fixed effects
> fe.lsdv <- lm(price ~ carat + factor(cut) + 0, data = diamonds)
> ct.lsdv <- coeftest(fe.lsdv, vcov. = vcovHC)
> ct.lsdv
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
carat 7871.082 24.892 316.207 < 2.2e-16 ***
factor(cut)Fair -3875.470 51.190 -75.707 < 2.2e-16 ***
factor(cut)Good -2755.138 26.570 -103.692 < 2.2e-16 ***
factor(cut)Very Good -2365.334 20.548 -115.111 < 2.2e-16 ***
factor(cut)Premium -2436.393 21.172 -115.075 < 2.2e-16 ***
factor(cut)Ideal -2074.546 16.092 -128.920 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
或通过消除左右两侧的含义(此处没有时间不变的回归变量)并校正自由度。
> # by demeaning with degrees of freedom correction
> diamonds <- ddply(diamonds, .(cut), transform, price.dm = price - mean(price), carat.dm = carat .... [TRUNCATED]
> fe.dm <- lm(price.dm ~ carat.dm + 0, data = diamonds)
> ct.dm <- coeftest(fe.dm, vcov. = vcovHC, df = nrow(diamonds) - 1 - 5)
> ct.dm
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
carat.dm 7871.082 24.888 316.26 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
我无法使用来复制这些结果plm
,因为我没有“时间”索引(即,这实际上不是一个面板,只是在错误方面可能有共同偏见的集群)。
> plm.temp <- plm(price ~ carat, data = diamonds, index = "cut")
duplicate couples (time-id)
Error in pdim.default(index[[1]], index[[2]]) :
我还尝试使用Stata对它们的cluster
选项的解释(这里解释),用聚类标准误差编码我自己的协方差矩阵,这是为了解决其中, si簇数,是残差对于观测值,是预测变量的行向量,包括常数(在Wooldridge的“ 横截面”和“面板数据”中也显示为等式(7.22)ÙĴ=ΣÇ升ü小号吨ËřĴë我*X我ñçë我我吨ħ
plm
基于一个因素进行集群,因此我不确定如何对我的代码进行基准测试。
> # with cluster robust se
> lm.temp <- lm(price ~ carat + factor(cut) + 0, data = diamonds)
>
> # using the model that Stata uses
> stata.clustering <- function(x, clu, res) {
+ x <- as.matrix(x)
+ clu <- as.vector(clu)
+ res <- as.vector(res)
+ fac <- unique(clu)
+ num.fac <- length(fac)
+ num.reg <- ncol(x)
+ u <- matrix(NA, nrow = num.fac, ncol = num.reg)
+ meat <- matrix(NA, nrow = num.reg, ncol = num.reg)
+
+ # outer terms (X'X)^-1
+ outer <- solve(t(x) %*% x)
+
+ # inner term sum_j u_j'u_j where u_j = sum_i e_i * x_i
+ for (i in seq(num.fac)) {
+ index.loop <- clu == fac[i]
+ res.loop <- res[index.loop]
+ x.loop <- x[clu == fac[i], ]
+ u[i, ] <- as.vector(colSums(res.loop * x.loop))
+ }
+ inner <- t(u) %*% u
+
+ #
+ V <- outer %*% inner %*% outer
+ return(V)
+ }
> x.temp <- data.frame(const = 1, diamonds[, "carat"])
> summary(lm.temp)
Call:
lm(formula = price ~ carat + factor(cut) + 0, data = diamonds)
Residuals:
Min 1Q Median 3Q Max
-17540.7 -791.6 -37.6 522.1 12721.4
Coefficients:
Estimate Std. Error t value Pr(>|t|)
carat 7871.08 13.98 563.0 <2e-16 ***
factor(cut)Fair -3875.47 40.41 -95.9 <2e-16 ***
factor(cut)Good -2755.14 24.63 -111.9 <2e-16 ***
factor(cut)Very Good -2365.33 17.78 -133.0 <2e-16 ***
factor(cut)Premium -2436.39 17.92 -136.0 <2e-16 ***
factor(cut)Ideal -2074.55 14.23 -145.8 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1511 on 53934 degrees of freedom
Multiple R-squared: 0.9272, Adjusted R-squared: 0.9272
F-statistic: 1.145e+05 on 6 and 53934 DF, p-value: < 2.2e-16
> stata.clustering(x = x.temp, clu = diamonds$cut, res = lm.temp$residuals)
const diamonds....carat..
const 11352.64 -14227.44
diamonds....carat.. -14227.44 17830.22
可以在R中完成吗?这是计量经济学中相当普遍的技术(本讲座中有一个简短的教程),但我无法在R中弄清楚。谢谢!
cluster
解释该选项的一些Stata手册,将会很有帮助。我相信这将是可能的R.复制
factor
它无关,factanal
而是指分类变量。但是R中的簇是指簇分析,k均值只是THE分区方法:statmethods.net/advstats/cluster.html。我没有收到您的问题,但我也猜想群集与它无关。