-
Notifications
You must be signed in to change notification settings - Fork 1
/
ivtsps.ado
220 lines (180 loc) · 5.13 KB
/
ivtsps.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
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
*! 1.0.0 Tom Palmer 05sep2021
program ivtsps, eclass
if _caller() >= 11 {
local vv : di "version " string(_caller()) ":"
}
version 10
local replay = replay()
if replay() {
if `"`e(cmd)'"' != "ivtsps" {
error 301
}
else if _by() {
error 190
}
else {
Display , lhs(`e(lhs)') endog(`e(endog)') exog(`e(exog)') inst(`e(inst)') replay(`replay') ///
link(`e(link)') `e(estonly)'
}
exit
}
Estimate `0'
local link `e(link)'
local estonly `e(estonly)'
_iv_parse `0'
local lhs `s(lhs)'
local endog `s(endog)'
local exog `s(exog)'
local inst `s(inst)'
Display , level(`level') lhs(`lhs') endog(`endog') exog(`exog') inst(`inst') link(`link') `estonly'
ereturn local cmd "ivtsps"
ereturn local cmdline `"ivtsps `0'"'
ereturn local lhs `lhs'
ereturn local endog `endog'
ereturn local exog `exog'
ereturn local inst `inst'
ereturn local link `link'
end
program Estimate, eclass
version 10
_iv_parse `0'
local lhs `s(lhs)'
local endog `s(endog)'
local exog `s(exog)'
local inst `s(inst)'
local 0 `s(zero)'
syntax [if] [in] [, Level(cilevel) s1xb(varlist numeric) Link(string) FROM(string) estonly *]
marksample touse
markout `touse' `lhs' `exog' `inst' `endog'
if "`link'" == "" local link identity
if !inlist("`link'", "identity", "logadd", "logmult", "logit") {
di as error "link() option must be one of identity, logadd, logmult, or logit"
error 198
}
local endogcount = wordcount("`endog'")
if `endogcount' != 1 {
di as error "Currently, only 1 endogenous variable is allowed"
error 198
}
// stage 1
tempname stage1b stage2b from
qui regress `endog' `inst' `exog' `if'`in'
mat `stage1b' = e(b)
cap ds predicted
if _rc == 0 {
local predictedreplace 1
tempvar predicted
qui gen `predicted' = predicted
qui drop predicted
}
else local predictedreplace 0
qui predict predicted `if'`in'
if "`link'" == "identity" {
// fit stage 2 linear regression to obtain initial values
qui regress `lhs' predicted `exog' `if'`in'
mat `stage2b' = e(b)
mat `from' = (`stage1b', `stage2b')
if "`estonly'" == "" {
if "`exog'" != "" {
local s2exogexpr "+ {s2exogxb:`exog'}"
}
gmm (`endog' - ({s1xb:`inst' `exog'}) - {a0}) ///
(`lhs' - ({b1}*({s1xb:} + {a0}) `s2exogexpr' + {b0})) ///
`if'`in', ///
instruments(1:`inst' `exog') ///
instruments(2:predicted `exog') ///
winitial(unadjusted, independent) ///
from(`from') ///
`options' nolog onestep
}
}
if "`link'" == "logadd" {
// fit stage 2 poisson regression to obtain initial values
// nb. for binary should be log-binomial regression, but poisson possibly safer if multi-category outcome
qui glm `lhs' predicted `exog' `if'`in', family(poisson) link(log)
mat `stage2b' = e(b)
mat `from' = (`stage1b', `stage2b')
if "`estonly'" == "" {
if "`exog'" != "" {
local s2exogexpr "+ {s2exogxb:`exog'}"
}
gmm (`endog' - ({s1xb:`inst' `exog'}) - {a0}) ///
(`lhs' - exp({b1}*({s1xb:} + {a0}) `s2exogexpr' + {b0})) ///
`if'`in', ///
instruments(1:`inst' `exog') ///
instruments(2:predicted `exog') ///
winitial(unadjusted, independent) ///
from(`from') ///
`options' nolog onestep
}
}
if "`link'" == "logmult" {
// fit stage 2 gamma regression to obtain initial values
qui glm `lhs' predicted `exog' `if'`in', family(gamma) link(log)
mat `stage2b' = e(b)
mat `from' = (`stage1b', `stage2b')
if "`estonly'" == "" {
if "`exog'" != "" {
local s2exogexpr "+ {s2exogxb:`exog'}"
}
gmm (`endog' - ({s1xb:`inst' `exog'}) - {a0}) ///
(`lhs' * exp(-1 * ({b1}*({s1xb:} + {a0}) `s2exogexpr' + {b0})) - 1) ///
`if'`in', ///
instruments(1:`inst' `exog') ///
instruments(2:predicted `exog') ///
winitial(unadjusted, independent) ///
from(`from') ///
`options' nolog onestep
}
}
if "`link'" == "logit" {
// fit stage 2 logistic regression to obtain initial values
qui logit `lhs' predicted `exog' `if'`in'
mat `stage2b' = e(b)
mat `from' = (`stage1b', `stage2b')
if "`estonly'" == "" {
if "`exog'" != "" {
local s2exogexpr "+ {s2exogxb:`exog'}"
}
gmm (`endog' - ({s1xb:`inst' `exog'}) - {a0}) ///
(`lhs' - invlogit({b1}*({s1xb:} + {a0}) `s2exogexpr' + {b0})) ///
`if'`in', ///
instruments(1:`inst' `exog') ///
instruments(2:predicted `exog') ///
winitial(unadjusted, independent) ///
from(`from') ///
`options' nolog onestep
}
}
if `predictedreplace' == 1 qui replace predicted = `predicted'
else qui drop predicted
ereturn local link `link'
ereturn local estonly `estonly'
end
program Display, rclass
version 10
syntax [anything] [, Level(cilevel) ///
lhs(varname) endog(varlist) exog(varlist) inst(varlist) ///
replay(integer 0) Link(string) estonly]
if `replay' == 1 | "`estonly'" != "" {
display
_coef_table , level(`level')
}
else {
qui _coef_table , level(`level') // to return r(table)
}
return add // to return r(table)
local eformopt ", eform"
if "`link'" == "identity" {
local parameter "Causal risk difference"
local eformopt ""
}
if inlist("`link'", "logadd", "logmult") local parameter "Causal risk ratio"
if "`link'" == "logit" local parameter "Causal odds ratio"
if "`estonly'" == "" {
foreach par in b1 {
di _n as txt "`parameter' for: `par'"
lincom /`par' `eformopt'
}
}
end