forked from joachim-gassen/ExPanDaR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reg_se_clustering.R
41 lines (32 loc) · 1.18 KB
/
reg_se_clustering.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
library(plm)
library(lmtest)
library(ExPanDaR)
# See Millo (2017): https://www.jstatsoft.org/article/view/v082i03
# and https://blog.theleapjournal.org/2016/06/sophisticated-clustered-standard-errors.html
data(petersen, package = "multiwayvcov")
df <- pdata.frame(petersen, index=c("firmid", "year"))
pooled <- plm(y ~ x, df, model = "pooling")
fe.firm <- plm(y ~ x, df, model = "within", effect = "individual")
fe.year <- plm(y ~ x, df, model = "within", effect = "time")
fe.fyear <- plm(y ~ x, df, model = "within", effect = "twoways")
summary(fe.fyear)
# "sss" gives the small sample correction that is being used by Stata
# See Millo(2017): 22f.
# Clustered by time
coeftest(fe.fyear, vcov=vcovHC(fe.firm, type="sss", cluster="time"))
# Clustered by firm
coeftest(fe.fyear, vcov=vcovHC(fe.firm, type="sss", cluster="group"))
# Clustered by firm and year
coeftest(fe.fyear, vcov=vcovDC(pooled.ols, type="sss"))
prepare_regression_table(
petersen, rep("y", 7), rep("x", 7),
feffects = list(
"", "firmid", "year", c("firmid", "year"),
"firmid", "year", c("firmid", "year")
),
cluster = list(
"", "", "", "",
"firmid", "year", c("firmid", "year")
),
format = "text"
)