-
Notifications
You must be signed in to change notification settings - Fork 0
/
wildfe.ado
161 lines (131 loc) · 3.95 KB
/
wildfe.ado
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
/*---------------------------------------------------------------------wildfe.do
Stuart Craig
20190401
*/
cap prog drop wildfe
prog define wildfe, rclass
syntax varlist [if] [in] [aweight] , absorb(varlist) [cluster(varname)] [weights(string)] [niter(real 1000)]
/*
-------------------------------------------
Preliminaries
-------------------------------------------
*/
// Sample
marksample touse
// Parse the variable list
loc depvar: word 1 of `varlist'
loc indvars ""
local ncol=wordcount("`varlist'")
forval i=2/`ncol' {
loc in: word `i' of `varlist'
loc indvars "`indvars' `in'"
}
// Demand well-behaved weights
if "`weights'"=="" loc weights "rademacher"
cap assert inlist("`weights'","mammen","normal","rademacher")
if _rc!=0 {
di as error "`weights' weighting not supported"
exit 12
}
/*
-------------------------------------------
Implement the actual BS procedure
-------------------------------------------
*/
tempfile results
cap postclose BS
qui postfile BS b str40 ivar iter using `results', replace
// Run baseline model and generate residuals and fitted values
tempvar r
tempvar xb
di in yellow "+==============================================================+"
di in yellow "+ Baseline Estimates +"
di in yellow "+==============================================================+"
reghdfe `depvar' `indvars' if `touse' [`weight' `exp'], absorb(`absorb') resid(`r')
qui gen `xb' = `depvar' - `r'
foreach ivar of varlist `indvars' {
return scalar b_`ivar'=_b[`ivar']
}
// Generate the Mammen weights up front if necessary
// (reduces computation)
if inlist("`weights'","mammen") {
tempvar W1
tempvar W2
qui gen `W1' = -(sqrt(5) - 1)/2
qui gen `W2'=(sqrt(5) + 1)/2
loc cut = (sqrt(5) - 1)/(2*sqrt(5))
}
// Iterate:
di in yellow "+==============================================================+"
di in yellow "+ Running Bootstrap Iterations +"
di in yellow "+==============================================================+"
sort `cluster'
cap timer clear
timer on 1
forval iter=1/`niter' {
qui {
tempvar W
if "`weights'"=="rademacher" {
qui gen `W' = -1
qui replace `W' = 1 if runiform()>0.5
}
if "`weights'"=="mammen" {
qui gen `W' = `W1'
qui replace `W' = `W2' if runiform()< `cut'
}
if "`weights'"=="normal" qui gen `W' = rnormal()
// Cluster the errors if required
if "`cluster'"!="" {
qby `cluster': replace `W' = `W'[1]
}
tempvar ystar
cap drop `ystar'
qui gen `ystar' = `xb' + `r'*`W'
* reg `ystar' `indvars'
qui reghdfe `ystar' `indvars' if `touse' [`weight' `exp'] , absorb(`absorb')
loc cc=0
foreach v of varlist `indvars' {
loc ++cc
post BS (_b[`v']) ("`v'") (`iter')
}
drop `ystar' `W'
macro drop ystar W
}
di . _c
if mod(`iter',50)==0 {
qui timer off 1
qui timer list
di `iter', "in `r(t1)' seconds"
timer clear
timer on 1
}
}
postclose BS
// Display the results (crudely):
preserve
use `results', clear
cap drop p_lower
cap drop p_upper
qui gen p_lower=.
qui gen p_upper=.
qui levelsof ivar, local(vars)
foreach var of local vars {
qui _pctile b if ivar=="`var'", p(2.5, 97.5)
qui replace p_lower = r(r1) if ivar=="`var'"
qui replace p_upper = r(r2) if ivar=="`var'"
}
collapse (mean) Coeff=b (sd) SE=b (max) p_ll=p_lower p_ul=p_upper, by(ivar) fast
list ivar SE p_*, noobs
// Return the SEs
cap drop n
qui gen n=_n
qui levelsof ivar, local(ivars)
foreach ivar of local ivars {
qui summ n if ivar=="`ivar'", mean
return scalar se_`ivar' = SE[r(mean)]
return scalar p_ll_`ivar' = p_ll[r(mean)]
return scalar p_ul_`ivar'= p_ul[r(mean)]
}
restore
end
exit