I am trying to understand standard error "clustering" and how to execute in R (it is trivial in Stata). In R I have been unsuccessful using either plm
or writing my own function. I'll use the diamonds
data from the ggplot2
package.
I can do fixed effects with either dummy variables
> 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
or by de-meaning both left- and right-hand sides (no time invariant regressors here) and correcting degrees of freedom.
> # 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
I can't replicate these results with plm
, because I don't have a "time" index (i.e., this isn't really a panel, just clusters that could have a common bias in their error terms).
> plm.temp <- plm(price ~ carat, data = diamonds, index = "cut")
duplicate couples (time-id)
Error in pdim.default(index[[1]], index[[2]]) :
I also tried to code my own covariance matrix with clustered standard error using Stata's explanation of their cluster
option (explained here), which is to solve $$\hat V_{cluster} = (X'X)^{-1} \left( \sum_{j=1}^{n_c} u_j'u_j \right) (X'X)^{-1}$$ where $u_j = \sum_{cluster~j} e_i * x_i$, $n_c$ si the number of clusters, $e_i$ is the residual for the $i^{th}$ observation and $x_i$ is the row vector of predictors, including the constant (this also appears as equation (7.22) in Wooldridge's Cross Section and Panel Data). But the following code gives very large covariance matrices. Are these very large values given the small number of clusters I have? Given that I can't get plm
to do clusters on one factor, I'm not sure how to benchmark my code.
> # 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
Can this be done in R? It is a fairly common technique in econometrics (there's a brief tutorial in this lecture), but I can't figure it out in R. Thanks!
cluster
option is explained. I am sure it would be possible to replicate in R. $\endgroup$factor
had nothing to do withfactanal
but refers to categorized variables. However cluster in R refers to cluster analysis, k-means is just THE partitioning method: statmethods.net/advstats/cluster.html . I don't get your question, but I also guess cluster has nothing to do with it. $\endgroup$