-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_weights.do
71 lines (52 loc) · 1.57 KB
/
compute_weights.do
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
#delimit;
clear all;
set more off;
/*
COMPUTE IMPLIED SHOCK WEIGHT FUNCTION
Please run "save_shocks.do" before this file
MPM 2024-11-11
*/;
* Compute weights;
cap mkdir fig;
cap erase "weights.dta";
foreach f in q m {; // For quarterly and monthly shocks...;
use shock_`f', clear;
foreach v of varlist * {;
if "`v'" == "`f'date" {;
continue;
};
su `v';
replace `v' = `v'/`r(sd)'; // Change units to standard deviations;
* Compute weight function via regression;
levelsof `v', local(levels); // Values of shock in sample;
su `v';
local levels "`levels' `=`r(min)'-0.01' `=`r(max)'+.01'"; // Add values slightly smaller and larger than support, so weights drop to 0;
tempfile fil;
postfile handle x b se using `fil', replace;
foreach l of local levels {; // For each x value...;
gen ind = (`v'>=`l'); // Indicator;
reg ind `v', robust; // Regression to compute weight;
post handle (`l') (_b[`v']) (_se[`v']); // Store coefficient and SE;
drop ind;
};
postclose handle;
* Compute total weight on positive shocks;
gen max0 = max(`v',0) if !missing(`v');
reg max0 `v', robust;
local weight_pos : display %4.3f _b[`v'];
local weight_pos_se : display %4.3f _se[`v'];
drop max0;
local label: variable label `v';
* Store results for later plotting;
preserve;
use `fil', clear;
rename (b se) `v'_=;
label var `v'_b "`label' ({&omega}>0: `weight_pos')"; // Variable label with weight;
cap merge 1:1 x using weights, nogen;
save weights, replace;
restore;
};
};
use weights, clear;
compress;
save weights, replace;