-
Notifications
You must be signed in to change notification settings - Fork 5
/
05-fixed-effects-estimates.R
59 lines (39 loc) · 1.43 KB
/
05-fixed-effects-estimates.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
# Use Fixed Effects to control for MRs ----
## Call previous scripts
source("01-packages-and-data.R")
source("02-prepare-data.R")
## Create Fixed Effects
## This step is VERY DIFFERENT in Stata, in Stata you need to create
## 830 0-1 columns, here you only need 2 factor columns
gravity2 <- gravity2 %>%
mutate(
exp_time = as.factor(paste(exporter, year, sep = "_")),
imp_time = as.factor(paste(importer, year, sep = "_"))
)
## Remove 0 flows
## IMPORTANT: If we don't do this, lm fails because log(0) = -Inf
gravity2 <- gravity2 %>%
filter(exporter != importer, trade > 0)
## See how many dummy variables we are adding to the OLS model
## There's a high risk of collinearity!
length(levels(gravity2$exp_time)) + length(levels(gravity2$imp_time))
## Adjust
fe_formula <- as.formula("log_trade ~ log_dist + cntg + lang + clny +
exp_time + imp_time")
model5 <- lm(fe_formula, data = gravity2)
## Check for collinear terms
collinear_terms <- alias(model5)
collinear_matrix <- collinear_terms$Complete
rownames(collinear_matrix)
## Compute clustered standard errors
vcov_cluster5 <- vcovCL(model5, cluster = gravity2[, "pair"],
df_correction = TRUE)
coef_test5 <- tidy(coeftest(
model5,
vcov_cluster5[
which(!grepl("^exp_time|^imp_time", rownames(vcov_cluster5))),
which(!grepl("^exp_time|^imp_time", colnames(vcov_cluster5)))
]
))
coef_test5
summary(model5)$r.squared