-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfc_toolkit.jl
613 lines (424 loc) · 14.1 KB
/
fc_toolkit.jl
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
using Pkg
#Install ...
# Pkg.activate(".")
# Pkg.add("Distributions")
# Pkg.add("StatsBase")
# Pkg.add(["DataFrames","DataFramesMeta","Chain"])
# Pkg.add("Plots")
# Pkg.add("CategoricalArrays")
# Pkg.add("StatFiles")
# Pkg.add("Tables")
# Pkg.add("CSV")
# Pkg.add("Optim")
# Pkg.add("Missings")
# Pkg.instantiate()
#Load packages ...
using Distributions
using LinearAlgebra
# using StatsBase
using DataFrames
# using Plots
# using CategoricalArrays
using StatFiles
using Chain
using Tables
using CSV
using Optim
using Random
using Missings
@doc """
Inputs\n
x: Array of N x K features \n
y: Outcome Array of N x 1 \n
fe: Binary array of N x nFixedEffects \n
cl: Categorical array with cluster classification N x 1 \n
Output \n
minKidx: minimum K indexes
""" ->
struct olsRegression
β::Array{Float64} # coefficient
x::Array{Float64} # features
y::Array{Float64} # response
flag_cl::Bool # clusters
cl::Array # clusters
# Define constructor function
function olsRegression(y, x, fe = nothing, cl = nothing, constant = true)
"""
Inputs:
Y: An array of N × 1 dimension (outputs).
X: An array of N × K dimension (inputs).
cl: An array of N × missing dimension (clusters)
"""
if isnothing(fe)
if constant == true
x = hcat(x,ones(size(x)[1],1))
else
x = x
end
else
x = hcat(x, fe)
end
xᵀx = transpose(x)*x
xᵀy = transpose(x)*y
β = inv(xᵀx)*xᵀy #compute parameters
isnothing(cl) ? new(β, x, y, false, [cl]) : new(β, x, y, true, cl)
end
end
@doc """
Inputs\n
x: Array of N x K features \n
d: Outcome Array of N x 1 \n
Output \n
minKidx: minimum K indexes
""" ->
struct probitModel
θ::Array{Float64} # coefficient
μ::Float64 # Dist. expected value
σ::Float64 # Dist. variance
x::Array{Float64} # features
d::Array{Float64} # dichotomous outcome
# Define constructor function
function probitModel(x, d)
"""
Inputs:
x: An array of N × K dimension (inputs).
d: An dichotomous array of N × 1 dimension (outputs).
"""
x = hcat(x,ones(size(x)[1],1))
param_init = zeros(size(x,2) + 2,1) .+ 0.01
sigmoid(x) = 1/(1+exp(x))
function objective(params, x, d)
σ = sigmoid(params[end])
μ = params[end-1]
θ = params[1:end-2]
yhat = x*θ
Φ = Normal(μ,σ)
log_p = d.*log.(cdf.(Φ, yhat)) + (1 .- d).*log.(1.0 .- cdf.(Φ, yhat))
log_L = reduce(+,log_p)
return -log_L
end
f(θ) = objective(θ, x, d)
result = optimize(f, param_init, LBFGS())
params_hat = Optim.minimizer(result)
new(params_hat[1:end-2], params_hat[end-1], sigmoid(params_hat[end]), x, d)
end
end
@doc """
tsls_regression(y, d, z, x=nothing, fe=nothing, intercept=true)\n
Inputs
y: Outcome array N x 1 features
d: Treatment variable array of N x 1
z: Instrument vector Array of N x F
x: Control variables N x K, it can be ::nothing
fe: Dichotomous Array N x nFixedEffects
intercept: Bool (default = true)
Output
β: Second stage coefficients
Π: First stage coefficients
""" ->
struct tsls_regression
β::Array{Float64} # coefficient
y::Array{Float64} # response
d::Array{Float64}
z::Array{Float64} # features
cl::Array # clusters
ZZ_inv::Array{Float64}
P_z::Array{Float64}
Π::Array{Float64}
function tsls_regression(y, d, z, x=nothing, fe=nothing, cl=nothing, intercept=true)
n = size(z)[1]
if ~isnothing(x)
z = hcat(z,x)
d = hcat(d,x)
end
if ~isnothing(fe)
intercept = false
z = hcat(z,fe)
d = hcat(d,fe)
end
if intercept == true
z = hcat(z,ones(n,1))
d = hcat(d,ones(n,1))
end
Q_Z, R_Z = qr(z)
ZZ_inv = inv(cholesky(R_Z' * R_Z))
P_Z = z * ZZ_inv * z'
β = inv(d' * P_Z * d) * d' * P_Z * y
Π = z\d
return isnothing(cl) ? new(β, y, d, z, [NaN], ZZ_inv, P_Z, Π) : new(β, y, d, z, cl, ZZ_inv, P_Z, Π)
end
end
function predict_outcome(fit::olsRegression, data = nothing)
isnothing(data) ? fitted = fit.x * fit.β : fitted = data * fit.β
return(fitted)
end
function predict_outcome(fit::tsls_regression, data = nothing)
isnothing(data) ? fitted = fit.d * fit.β : fitted = data * fit.β
return(fitted)
end
function predict_outcome(fit::probitModel, data = nothing)
isnothing(data) ? fitted = cdf.(Normal(fit.μ, fit.σ),fit.x * fit.θ) : fitted = cdf.(Normal(fit.μ, fit.σ), data * fit.θ)
return(fitted)
end
@doc """
Inputs\n
k: Number of neighbors to use\n
pscorei: Propensity score for individual i, should be float\n
neighborhood: Array that contains pscore for all individuals we are comparing\n
Output \n
minKidx: minimum K indexes
""" ->
function kNearest(k::Int, pscorei::Float64, neighborhood::Array{Float64})
#Calculate euclidean distance:
distance = (pscorei .- neighborhood).^2
Random.seed!(1234); distance = distance.* 100000 .+ shuffle(1:size(distance)[1]) ./ 10000 ; # Random ties elimination
dict = Dict(distance .=> 1:size(distance)[1])
minK = sort(distance)[1:k]
minKidx = [dict[ii] for ii in minK]
return minKidx
end
@doc """
Inputs \n
x: Covariates used to compute score P[D = 1 | X] \n
d: Treatment variable \n
y: Outcome variable \n
k: Number of neighbors \n
Output \n
ATE: Average Treatment Effect \n
ATT: Average Treatment on the Treated
""" ->
function propensityScoreMatching(x, y, d, k)
probit = probitModel(x,d)
pscore = predict_outcome(probit)
y_1 = y[vec(d .== 1)]
y_0 = y[vec(d .== 0)]
pscore_1 = pscore[vec(d .== 1)]
pscore_0 = pscore[vec(d .== 0)]
y_cf = zeros(size(y,1),1)
for ii in 1:size(pscore,1)
if d[ii] == 1 # If treated, compare to untreateds...
k_index = kNearest(k, pscore[ii], pscore_0)
y_mean = mean(y_0[k_index])
else
k_index = kNearest(k, pscore[ii], pscore_1)
y_mean = mean(y_1[k_index])
end
y_cf[ii] = y_mean
end
ATE = (sum(y[vec(d .== 1)]) + sum(y_cf[vec(d .== 0)]) - sum(y[vec(d .== 0)]) - sum(y_cf[vec(d .== 1)]))/size(y,1)
ATT = mean(y[vec(d .== 1)]) - mean(y_cf[vec(d .== 1)])
return ATE, ATT
end
function se_homoskedastic(fit::olsRegression)
x = fit.x; y = fit.y; β = fit.β
N = length(y); K = size(x, 2);
u = y - predict_outcome(fit) # residuals
XX_inv = inv(x' * x)
covar = sum(u.^2) * XX_inv
covar = covar .* (1 / (N - K)) # dof adjustment
# Get standard errors, t-statistics, and p-values
se = sqrt.(covar[diagind(covar)])
return se
end
function se_heteroskedastic_h1(fit::olsRegression)
x = fit.x; y = fit.y; β = fit.β
N = length(y); K = size(x, 2);
u = y - predict_outcome(fit) # residuals
XX = x' * x
XX_inv = inv(XX)
varfunction(u,x) = u^2*(x*x')
V_hat = sum(varfunction.(u,[x[ii,:] for ii in 1:size(x,1)]))
covar = XX_inv*V_hat*XX_inv # covar = covar .* (1 / (N - K)) # dof adjustment
se = sqrt.(covar[diagind(covar)])
return se
end
function se_heteroskedastic_h1(fit::tsls_regression)
d = fit.d; y = fit.y; β = fit.β;
ZZ_inv = fit.ZZ_inv ; Π = fit.Π
u = y - predict_outcome(fit) # residuals
varfunction(u,x) = u^2*(x*x')
V_hat = sum(varfunction.(u,[d[ii,:] for ii in 1:size(d,1)]))
covar = (Π' * ZZ_inv * Π)*V_hat*(Π' * ZZ_inv * Π) # covar = covar .* (1 / (N - K)) # dof adjustment
se = sqrt.(covar[diagind(covar)])
return se
end
function se_cluster(fit::olsRegression)
# Obtain data parameters
x = fit.x
y = fit.y
cl = fit.cl
n = size(y, 1);
k = size(x, 2);
res = y - predict_outcome(fit)
xᵀx = x'*x
clusters = unique(cl)
C = length(clusters)
R = (C / (C-1)) * ((n-1)/(n-k))
function clust_residuals(c)
cindex = findall(cl .== c) # Find index of observations that belong to cluster c
Xc = Matrix(x[cindex,:]) # Convert to matrix
resc = Matrix(res[cindex,:])
meat = Xc'*resc*resc'*Xc
return meat
end
meat_cluster = broadcast(
c -> clust_residuals(c),
clusters
)
Meat = reduce(+, meat_cluster.*R)
Bread = inv(xᵀx)
varcov = Bread' * Meat * Bread
function mat_posdef_fix(X::Matrix; tol = 1e-10)
if any(diag(X) .< tol)
e_vals, e_vecs = eigen(Symmetric(X))
e_vals[e_vals .<= tol] .= tol
X = e_vecs * Diagonal(e_vals) * e_vecs'
end
return X
end
vcov_matrix = mat_posdef_fix(varcov)
se = sqrt.(diag(vcov_matrix))
return se
end
# Cheated a little bit here since I'm defaulting indicator and β0, not much time to automate it.
function se_wild_bootstrap(fit::olsRegression, indicator=5, β0=sin(1))
# Unpack stuff
x = fit.x; y = fit.y ;cl = fit.cl; xᵀx = x'*x;
n = size(y, 1); k = size(x, 2);
clusters = unique(cl)
C = length(clusters)
R = (C / (C-1)) * ((n-1)/(n-k))
# Variables except for target D1
x_del = hcat(x[:,1:indicator-1],x[:,indicator+1:end])
β_del = y\x_del
# Eliminate the true effect:
u = y .- x[:,indicator] .* β0
# Random sign:
rand_sign = rand((0, 2), n) .- 1
# Construct wild y
y_w = x[:,indicator] .* β0 + x_del * β_del' + u.*rand_sign
# Beta wild:
β_w = y_w\x
# Residual:
res = y - x*β_w' #res = y_w - x*β_w'
function clust_residuals(c)
cindex = findall(cl .== c) # Find index of observations that belong to cluster c
Xc = Matrix(x[cindex,:]) # Convert to matrix
resc = Matrix(res[cindex,:])
meat = Xc'*resc*resc'*Xc
return meat
end
meat_cluster = broadcast(
c -> clust_residuals(c),
clusters
)
Meat = reduce(+, meat_cluster.*R)
Bread = inv(xᵀx)
varcov = Bread' * Meat * Bread
function mat_posdef_fix(X::Matrix; tol = 1e-10)
if any(diag(X) .< tol)
e_vals, e_vecs = eigen(Symmetric(X))
e_vals[e_vals .<= tol] .= tol
X = e_vecs * Diagonal(e_vals) * e_vecs'
end
return X
end
vcov_matrix = mat_posdef_fix(varcov)
se = sqrt.(diag(vcov_matrix))
return se
end
function inference(fit::tsls_regression, vartype="het")
# Obtain data parameters
d = fit.d
y = fit.y
β = fit.β
N = length(y);
K = size(d, 2);
u = y - predict_outcome(fit) # residuals
# Calculate the covariance under homoskedasticity
se = se_heteroskedastic_h1(fit)
#Get standard errors, t-statistics, and p-values
# se = sqrt.(covar[diagind(covar)])
t_stat = β./ se
p_val = 2 .* cdf.(TDist(N-K), - abs.(t_stat))
r2 = 1 - sum(u.^2)/sum((y.-mean(y)).^2)
# Organize and return output
output = (β = β, se = se, t = t_stat, p = p_val, r = r2)
return output
end
function inference(fit::olsRegression, vartype="hom", β_0=0)
# Obtain data parameters
x = fit.x
y = fit.y
β = fit.β
N = length(y);
K = size(x, 2);
u = y - predict_outcome(fit) # residuals
# Calculate the covariance under homoskedasticity
if fit.flag_cl == true # If cluster is passed on struct
if vartype == "clust"
# println("Errors: clustered")
se = se_cluster(fit)
elseif vartype == "wild"
# println("Errors: freaking wild")
se = se_wild_bootstrap(fit)
end
else # If cluster is not pased on struct
if vartype == "hom"
# println("Errors: Homoskedastic")
se = se_homoskedastic(fit)
elseif vartype == "het"
# println("Errors: heteroskedasticity")
se = se_heteroskedastic_h1(fit)
end
end
#Get standard errors, t-statistics, and p-values
# se = sqrt.(covar[diagind(covar)])
t_stat = (β.-β_0) ./ se
p_val = 2 .* cdf.(TDist(N-K), - abs.(t_stat))
r2 = 1 - sum(u.^2)/sum((y.-mean(y)).^2)
# Organize and return output
output = (β = β, se = se, t = t_stat, p = p_val, r = r2)
return output
end
@doc """
select_variables(df::DataFrame, y_name::list, x_name::list, z_name::list)\n
Inputs \n
df: DataFrame
y_name: List of symbols with outcome names.
x_name: List of symbols with covariates names.
d_name: List of symbols with endogenous.
z_name: List of symbols with exogenous.
Output \n
y: N x 1 Array with missings disallowed
x: N x d_H Array with missings disallowed
d: N x d_F Array with missings disallowed
""" ->
function select_variables(df::DataFrame, y_name, x_name=nothing, d_name=nothing, z_name=nothing)
# Join all names in all_names
all_names = []
try append!(all_names, y_name) catch end
try append!(all_names, x_name) catch end
try append!(all_names, d_name) catch end
try append!(all_names, z_name) catch end
for n in 1:length(all_names)
df = filter(all_names[n] => x -> !any(f -> f(x), (ismissing, isnothing, isnan)), df)
end
y = disallowmissing(Matrix(df[:, y_name]))
if ~isnothing(d_name)
d = disallowmissing(Matrix(df[:, d_name]))
else
d=nothing
end
if ~isnothing(x_name)
x = disallowmissing(Matrix(Matrix(df[:, x_name])))
else
x=nothing
end
if ~isnothing(z_name)
z = disallowmissing(Matrix(df[:, z_name]))
else
z=nothing
end
return y, x, d, z
end