forked from benjann/mindex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mindex_gmm.ado
154 lines (149 loc) · 5.5 KB
/
mindex_gmm.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
*! version 1.0.0 22nov2019 Ben Jann & Simon Seiler
program mindex_gmm
version 11
_parse comma lhs 0 : 0
syntax [, cmd(str) * ]
if `"`cmd'"'=="mlogit" {
mindex_gmm_mlogit `lhs', `options'
exit
}
di as err `"vce(gmm): '`cmd'' not supported"'
exit 499
end
program mindex_gmm_mlogit
syntax varlist if [fw aw iw pw], at(name) ///
depvar(str) nout(str) out(str) ibase0(str) ibase1(str) ///
[ derivatives(varlist) ]
// residual equations
quietly {
local eq 0
// compute mlogit probabilities and residuals
// (m=0: restricted model, m=1: extended model)
foreach m in 0 1 {
local ibase `ibase`m''
// obtain xb for each equation
forv i = 1/`nout' {
if `i'==`ibase' continue
local ++eq
tempvar xb`m'_`i'
matrix score double `xb`m'_`i'' = `at' `if', eq(#`eq')
}
// compute denominator
tempvar den`m'
local den `den`m''
gen double `den' = 1 `if'
forv i = 1/`nout' {
if `i'==`ibase' continue
local xb `xb`m'_`i''
replace `den' = /* from mlogit_p.ado:
*/ cond(`xb'<. & exp(`xb')>=., /*
*/ cond(`den'<0,`den'-1,-1), `den'+exp(`xb')) `if'
}
// compute probability for each outcome
forv i = 1/`nout' {
tempname p`m'_`i'
local p `p`m'_`i''
local xb `xb`m'_`i''
if `i'==`ibase' {
gen double `p' = /* from mlogit_p.ado:
*/ cond(`den'>0,1/`den',0) `if'
}
else {
gen double `p' = /* from mlogit_p.ado:
*/ cond(`den'>0,exp(`xb')/`den', /*
*/ cond(exp(`xb')<.,0,cond(`den'==-1,1,.))) `if'
}
}
// mlogit residual equations
forv i = 1/`nout' {
if `i'==`ibase' continue
local o: word `i' of `out'
gettoken resid varlist : varlist
replace `resid' = (`depvar'==`o') - `p`m'_`i'' `if'
}
// compute probability of observed outcome (needed for m-index)
tempvar p`m'
gen double `p`m'' = . `if'
forv i = 1/`nout' {
local o: word `i' of `out'
replace `p`m'' = `p`m'_`i'' `if' & `depvar'==`o'
}
}
// m-index residual equations
local ++eq
tempname M
matrix score double `M' = `at' `if', eq(#`eq')
gettoken resid varlist : varlist
replace `resid' = (ln(`p1') - ln(`p0')) - `M' `if'
}
// derivatives
if "`derivatives'"=="" exit
quietly {
// restricted model residual equations
forv i = 1/`nout' {
if `i'==`ibase0' continue
// - restricted model parameter equations
forv ii = 1/`nout' {
if `ii'==`ibase0' continue
gettoken deriv derivatives : derivatives
if `i' == `ii' {
replace `deriv' = `p0_`ii'' * (`p0_`ii'' - 1) `if'
}
else {
replace `deriv' = `p0_`i'' * `p0_`ii'' `if'
}
}
// - extended model parameter equations
forv ii = 1/`nout' {
if `ii'==`ibase1' continue
gettoken deriv derivatives : derivatives
// (derivative is zero)
}
// - m-index model equation
gettoken deriv derivatives : derivatives
// (derivative is zero)
}
// extended model residual equations
forv i = 1/`nout' {
if `i'==`ibase1' continue
// - restricted model parameter equations
forv ii = 1/`nout' {
if `ii'==`ibase0' continue
gettoken deriv derivatives : derivatives
// (derivative is zero)
}
// - extended model parameter equations
forv ii = 1/`nout' {
if `ii'==`ibase1' continue
gettoken deriv derivatives : derivatives
if `i' == `ii' {
replace `deriv' = `p1_`ii'' * (`p1_`ii'' - 1) `if'
}
else {
replace `deriv' = `p1_`i'' * `p1_`ii'' `if'
}
}
// - m-index model equation
gettoken deriv derivatives : derivatives
// (derivative is zero)
}
// m-index residual equations
// - restricted model parameter equations
forv i = 1/`nout' {
if `i'==`ibase0' continue
gettoken deriv derivatives : derivatives
local o: word `i' of `out'
replace `deriv' = `p0_`i'' - (`depvar'==`o') `if'
}
// - extended model parameter equations
forv i = 1/`nout' {
if `i'==`ibase1' continue
gettoken deriv derivatives : derivatives
local o: word `i' of `out'
replace `deriv' = (`depvar'==`o') - `p1_`i'' `if'
}
// - m-index model equation
gettoken deriv derivatives : derivatives
replace `deriv' = -1 `if'
}
end