-
Notifications
You must be signed in to change notification settings - Fork 0
/
causal_inference_package_alternates.R
301 lines (222 loc) · 10.5 KB
/
causal_inference_package_alternates.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
require(wfe)
### NOTE: this example illustrates the use of wfe function with randomly
### generated panel data with arbitrary number of units and time.
## generate panel data with number of units = N, number of time = Time
N <- 10 # number of distinct units
Time <- 15 # number of distinct time
## treatment effect
beta <- 1
## generate treatment variable
treat <- matrix(rbinom(N*Time, size = 1, 0.25), ncol = N)
## make sure at least one observation is treated for each unit
while ((sum(apply(treat, 2, mean) == 0) > 0) | (sum(apply(treat, 2, mean) == 1) > 0) |
(sum(apply(treat, 1, mean) == 0) > 0) | (sum(apply(treat, 1, mean) == 1) > 0)) {
treat <- matrix(rbinom(N*Time, size = 1, 0.25), ncol = N)
}
treat.vec <- c(treat)
## unit fixed effects
alphai <- rnorm(N, mean = apply(treat, 2, mean))
## geneate two random covariates
x1 <- matrix(rnorm(N*Time, 0.5,1), ncol=N)
x2 <- matrix(rbeta(N*Time, 5,1), ncol=N)
x1.vec <- c(x1)
x2.vec <- c(x2)
## generate outcome variable
y <- matrix(NA, ncol = N, nrow = Time)
for (i in 1:N) {
y[, i] <- alphai[i] + treat[, i] + x1[,i] + x2[,i] + rnorm(Time)
}
y.vec <- c(y)
## generate unit and time index
unit.index <- rep(1:N, each = Time)
time.index <- rep(1:Time, N)
Data.str <- as.data.frame(cbind(y.vec, treat.vec, unit.index, x1.vec, x2.vec))
colnames(Data.str) <- c("y", "tr", "strata.id", "x1", "x2")
Data.obs <- as.data.frame(cbind(y.vec, treat.vec, unit.index, time.index, x1.vec, x2.vec))
colnames(Data.obs) <- c("y", "tr", "unit", "time", "x1", "x2")
############################################################
# Example 1: Stratified Randomized Experiments
############################################################
## run the weighted fixed effect regression with strata fixed effect.
## Note: the quantity of interest is Average Treatment Effect ("ate")
## and the standard errors allow heteroskedasticity and arbitrary
## autocorrelation.
### Average Treatment Effect
mod.ate <- wfe(y ~ tr + x1 + x2, data = Data.str, treat = "tr",
unit.index = "strata.id", method = "unit",
qoi = "ate", hetero.se=TRUE, auto.se=TRUE)
## summarize the results
summary(mod.ate)
### Average Treatment Effect for the Treated
mod.att <- wfe(y~ tr+x1+x2, data = Data.str, treat = "tr",
unit.index = "strata.id", method = "unit",
qoi = "att", hetero.se=TRUE, auto.se=TRUE)
## summarize the results
summary(mod.att)
############################################################
# Example 2: Observational Studies with Unit Fixed-effects
############################################################
## run the weighted fixed effect regression with unit fixed effect.
## Note: the quantity of interest is Average Treatment Effect ("ate")
## and the standard errors allow heteroskedasticity and arbitrary
## autocorrelation.
mod.obs <- wfe(y~ tr+x1+x2, data = Data.obs, treat = "tr",
unit.index = "unit", time.index = "time", method = "unit",
qoi = "ate", hetero.se=TRUE, auto.se=TRUE,
White = TRUE, White.alpha = 0.05)
## summarize the results
summary(mod.obs)
## extracting weigths
summary(mod.obs)$W
## Not run:
###################################################################
# Example 3: Observational Studies with differences-in-differences
###################################################################
## run difference-in-differences estimator.
## Note: the quantity of interest is Average Treatment Effect ("ate")
## and the standard errors allow heteroskedasticity and arbitrary
## autocorrelation.
mod.did <- wfe(y~ tr+x1+x2, data = Data.obs, treat = "tr",
unit.index = "unit", time.index = "time", method = "unit",
qoi = "ate", estimator ="did", hetero.se=TRUE, auto.se=TRUE,
White = TRUE, White.alpha = 0.05, verbose = TRUE)
## summarize the results
summary(mod.did)
## extracting weigths
summary(mod.did)$W
#########################################################################
# Example 4: DID with Matching on Pre-treatment Outcomes
#########################################################################
## implements matching on pre-treatment outcomes where the maximum
## deviation is specified as 0.5
mod.Mdid <- wfe(y~ tr+x1+x2, data = Data.obs, treat = "tr",
unit.index = "unit", time.index = "time", method = "unit",
qoi = "ate", estimator ="Mdid", hetero.se=TRUE, auto.se=TRUE,
White = TRUE, White.alpha = 0.05, maxdev.did = 0.5, verbose = TRUE)
## summarize the results
summary(mod.Mdid)
## Note: setting the maximum deviation to infinity (or any value
## bigger than the maximum pair-wise difference in the outcome) will
## return the same result as Example 3.
dev <- 1000+max(Data.obs$y)-min(Data.obs$y)
mod.did2 <- wfe(y~ tr+x1+x2, data = Data.obs, treat = "tr",
unit.index = "unit", time.index = "time", method = "unit",
qoi = "ate", estimator ="Mdid", hetero.se=TRUE, auto.se=TRUE,
White = TRUE, White.alpha = 0.05, maxdev.did = dev, verbose = TRUE)
## summarize the results
summary(mod.did2)
mod.did2$coef[1] == mod.did$coef[1]
################################################################################
N <- 10 # number of distinct units
Time <- 15 # number of distinct time
## generate treatment variable
treat <- matrix(rbinom(N*Time, size = 1, 0.25), ncol = N)
## make sure at least one observation is treated for each unit
while ((sum(apply(treat, 2, mean) == 0) > 0) | (sum(apply(treat, 2, mean) == 1) > 0) |
(sum(apply(treat, 1, mean) == 0) > 0) | (sum(apply(treat, 1, mean) == 1) > 0)) {
treat <- matrix(rbinom(N*Time, size = 1, 0.25), ncol = N)
}
treat.vec <- c(treat)
## unit fixed effects
alphai <- rnorm(N, mean = apply(treat, 2, mean))
## geneate two random covariates
x1 <- matrix(rnorm(N*Time, 0.5,1), ncol=N)
x2 <- matrix(rbeta(N*Time, 5,1), ncol=N)
pscore <- matrix(runif(N*Time, 0,1), ncol=N)
x1.vec <- c(x1)
x2.vec <- c(x2)
pscore <- c(pscore)
## generate outcome variable
y <- matrix(NA, ncol = N, nrow = Time)
for (i in 1:N) {
y[, i] <- alphai[i] + treat[, i] + x1[,i] + x2[,i] + rnorm(Time)
}
y.vec <- c(y)
## generate unit and time index
unit.index <- rep(1:N, each = Time)
time.index <- rep(1:Time, N)
Data.str <- as.data.frame(cbind(y.vec, treat.vec, unit.index, x1.vec, x2.vec))
colnames(Data.str) <- c("y", "tr", "strata.id", "x1", "x2")
Data.obs <- as.data.frame(cbind(y.vec, treat.vec, unit.index, time.index, x1.vec, x2.vec, pscore))
colnames(Data.obs) <- c("y", "tr", "unit", "time", "x1", "x2", "pscore")
############################################################
# Example 1: Stratified Randomized Experiments
############################################################
## run the weighted fixed effect regression with strata fixed effect.
## Note: the quantity of interest is Average Treatment Effect ("ate")
## and the standard errors allow heteroskedasticity and arbitrary
## autocorrelation.
### Average Treatment Effect
ps.ate <- pwfe(~ x1+x2, treat = "tr", outcome = "y", data = Data.str,
unit.index = "strata.id", method = "unit", within.unit = TRUE,
qoi = "ate", hetero.se=TRUE, auto.se=TRUE)
## summarize the results
summary(ps.ate)
### Average Treatment Effect for the Treated
ps.att <- pwfe(~ x1+x2, treat = "tr", outcome = "y", data = Data.str,
unit.index = "strata.id", method = "unit", within.unit = TRUE,
qoi = "att", hetero.se=TRUE, auto.se=TRUE)
## summarize the results
summary(ps.att)
############################################################
# Example 2: Observational Studies with Unit Fixed-effects
############################################################
## run the weighted fixed effect regression with unit fixed effect.
## Note: the quantity of interest is Average Treatment Effect ("ate")
## and the standard errors allow heteroskedasticity and arbitrary
## autocorrelation.
### Average Treatment Effect
ps.obs <- pwfe(~ x1+x2, treat = "tr", outcome = "y", data = Data.obs,
unit.index = "unit", time.index = "time",
method = "unit", within.unit = TRUE,
qoi = "ate", hetero.se=TRUE, auto.se=TRUE)
## summarize the results
summary(ps.obs)
## extracting weigths
summary(ps.obs)$Weights
### Average Treatment Effect with First-difference
ps.fd <- pwfe(~ x1+x2, treat = "tr", outcome = "y", data = Data.obs,
unit.index = "unit", time.index = "time",
method = "unit", within.unit = TRUE,
qoi = "ate", estimator = "fd", hetero.se=TRUE, auto.se=TRUE)
## summarize the results
summary(ps.fd)
############################################################
# Example 3: Estimation with pre-specified propensity score
############################################################
### Average Treatment Effect with Pre-specified Propensity Scores
mod.ps <- pwfe(treat = "tr", outcome = "y", data = Data.obs, pscore = "pscore",
unit.index = "unit", time.index = "time",
method = "unit", within.unit = TRUE,
qoi = "ate", hetero.se=TRUE, auto.se=TRUE)
## summarize the results
summary(mod.ps)
## End(Not run)
https://diff.healthpolicydatascience.org/
https://bookdown.org/ccolonescu/RPoE4/indvars.html#the-difference-in-differences-estimator
require(did)
data(mpdta)
## with covariates
out1 <- mp.spatt(lemp ~ treat, xformla=~lpop, data=mpdta,
panel=TRUE, first.treat.name="first.treat",
idname="countyreal", tname="year",
bstrap=FALSE, se=TRUE, cband=FALSE)
## summarize the group-time average treatment effects
summary(out1)
## summarize the aggregated treatment effect parameters
summary(out1$aggte)
## without any covariates
out2 <- mp.spatt(lemp ~ treat, xformla=NULL, data=mpdta,
panel=TRUE, first.treat.name="first.treat",
idname="countyreal", tname="year",
bstrap=FALSE, se=TRUE, cband=FALSE)
summary(out2)
#########################
http://tabletopwhale.com/
https://www.r-bloggers.com/the-popularity-of-point-and-click-guis-for-r/
http://r4stats.com/articles/software-reviews/
https://www.blueskystatistics.com/Default.asp
https://www.jamovi.org/jmv/
https://jasp-stats.org/
######################
http://davidcard.berkeley.edu/data_sets.html