Description Usage Arguments Details Value Note Author(s) References Examples
vcovTamal, creates a robust covariance variance matrix clustered at group level for first difference estimations of the lm::plm
command.
Clustering is based on Angist and Pischke (2009) resulting in Stata(TM) like standard errors of
regression coefficients.
1 |
estimate |
an object of class |
data |
a data.frame object, that must be the data.frame used to create the |
groupvar |
a string indicating a column in |
byhand |
logical, if TRUE, the clustered covariance matrix is calculated by formulas
without using the |
See package vignette.
A matrix containing the covariance matrix estimate
The function uses the plm
output and the data
to construct a transfomed (first differenced) data frame.
The esitmate is then re-estimated by OLS with lm
in order to extract the
covarinace matrix and correct for degrees of freedom and use a sandwich estimate with the group structure.
The implementation for Fixed Effects (within) estimates is outdated. Ihe command felm
of the package lfe
is able construct directly estimates with multiple cluster with multiple fixed effects.
ElĂas Cisneros <ec@elias-cisneros.de>
Angrist, J. D. & Pischke, J.-S. Mostly harmless econometrics: An empiricist's companion Princeton university press, 2009 Arai, M. Cluster-robust standard errors using R, 2015. See also: https://thetarzan.wordpress.com/2011/06/11/clustered-standard-errors-in-r/.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | ##packages
library(foreign)
library(clubTamal)
library(lmtest)
library(plm)
library(clubSandwich)
library(spd4testing)
library(multiwayvcov)
##data
d<-spd4testing()
d
##formula
f<- formula(y ~ x + factor(year))
##standard estimation
e<-plm(formula=f,data=d,model="fd")
summary(e)
## Not run:
# e<-plm(formula=f,data=d,model="within")
# summary(e)
## End(Not run)
##clustering
#no clustering
v<-e$vcov
coeftest(e)
#clustering at id level with plm-package
v<-vcovHC(e,type="HC1",cluster="group",tol=1*10^-20 )
coeftest(e,v)
##clustering at group level with clubSandwich-package
##only for Fixed Effects regressions
v<-vcovCR(e,d$gid,type="CR1")
coef_test(e,vcov=v,test="naive-t")
coeftest(e,v)
##clustering at group level with clubTamal
v<-vcovTamal(estimate=e,data=d,groupvar="gid")
coeftest(e,v)
##clustering with clubTamal but without using the multivcov package
## Not run:
v<-vcovTamal(estimate=e,data=d,groupvar="gid",byhand=T)
coeftest(e,v)
## End(Not run)
##clustering at group level with STATA (TM)
#export data
tdir<-tempdir()
write.csv(d,file.path(tdir,"spd.csv"))
#write stata file
sink(file.path(tdir,"clusterStata.do"))
cat(paste0('insheet using "',file.path("spd.csv"),'", clear'))
cat("\n")
cat('xtset id year')
cat("\n")
cat('xtreg y x i.year, fe')
cat("\n")
cat('xtreg y x i.year, fe vce(cluster gid)')
cat("\n")
cat('reg d.y d.x i.year')
cat("\n")
cat('reg d.y d.x i.year, vce(cluster gid)')
sink()
## RUN Stata file
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.