-
Notifications
You must be signed in to change notification settings - Fork 2
/
cratreg.ado
328 lines (264 loc) · 7.76 KB
/
cratreg.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
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
*! v1.2.0, S Bauldry, 18sep2017
capture program drop cratreg
program cratreg, properties(swml svyb svyj svyr mi or rrr irr hr eform)
version 14.1
if replay() {
if (`"`e(cmd)'"' != "crreg") error 301
Replay `0'
}
else Estimate `0'
end
capture program drop Estimate
program Estimate, eclass sortpreserve
syntax varlist(numeric fv) [if] [in] [pweight fweight aweight iweight] [, ///
PRop(varlist fv) /// vars with proportionality constraint
FRee(varlist fv) /// vars with no constraint
LINK(string) /// link function (logit, probit, or cloglog)
Level(cilevel) /// display option for confidence intervals
vce(string) /// robust and cluster robust standard errors
or EForm /// exponential form options
svy * /// -mlopts, display options
]
* sample selection
marksample touse
* parse VCE statement
if ( `"`vce'"' != "" ) {
my_vce_parse , vce(`vce')
local vcetype "robust"
local clustervar "`r(clustervar)'"
if "`clustervar'" != "" {
markout `touse' `clustervar'
}
}
* parse weight statement
if ( "`weight'" != "" ) {
local wgt "[`weight'`exp']"
markout `touse' `wvar'
}
* check that there are cases
qui count if `touse' != 0
if r(N) == 0 {
dis as error "There are no observations"
exit 2000
}
* check for link function (default to logit)
if ( "`link'" == "logit" | "`link'" == "l" | "`link'" == "" ) {
global Link "logit"
local link_title "Ordered Logit Estimates"
}
else if ( "`link'" == "probit" | "`link'" == "p" ) {
global Link "probit"
local link_title "Ordered Probit Estimates"
}
else if ( "`link'" == "cloglog" | "`link'" == "c" ) {
global Link "cloglog"
local link_title "Ordered Complementary Log-Log Estimates"
}
else {
dis ""
dis as error "{yellow}`link'{red} is not a supported link function"
exit 198
}
* exponential form
local eform `or' `eform'
local efopt : word count `eform'
if `efopt' > 1 {
dis as error "only one of or or eform can be specified"
exit 198
}
* check for display and maximization options
_get_diopts diopts options, `options'
mlopts mlopts, `options'
* identify DV and IVs
gettoken Y X : varlist
_fv_check_depvar `Y'
* set globals for # categories
tempname Yval
qui tab `Y' if `touse', matrow(`Yval')
global nCat = r(r)
global nCatm1 = r(r) - 1
macro drop y_*
forval i = 1/$nCat {
global y_`i' = `Yval'[`i',1]
}
* too few categories
if ( $nCat < 3 ) {
dis ""
dis as error "{yellow}`Y'{red} has $nCat categories - a minimum" ///
" of 3 is required"
exit 148
}
* prepare IVs
if ( "`X'" != "" ) {
fvexpand `X'
local cnIV `r(varlist)'
}
* prepare no constraint variables
if ( "`free'" != "" ) {
fvexpand `free'
local frIV `r(varlist)'
* verify subset of IVs
local frchk : list local(frIV) - local(cnIV)
if ( "`frchk'" != "" ) {
dis ""
dis as error "free{yellow}(`frchk'){red} is not included in" ///
" the list of independent variables: {yellow}`IV'"
exit 198
}
* remove from list of IVs
local cnIV : list local(cnIV) - local(frIV)
}
* prepare proportionality constraint variables
if ( "`prop'" != "" ) {
fvexpand `prop'
local prIV `r(varlist)'
* verify subset of IVs
local prchk : list local(prIV) - local(cnIV)
if ( "`prchk'" != "" ) {
dis ""
dis as error "prop{yellow}(`prchk'){red} is not included in" ///
" the list of independent variables: {yellow}`IV'"
exit 198
}
* remove from list of IVs
local cnIV : list local(cnIV) - local(prIV)
}
* create ML model statements
* case 1: all variables with parallel assumption
if ( "`free'" == "" & "`prop'" == "" & "`cnIV'" != "" ) {
local model "(constrained: `Y' = `cnIV', nocons)"
forval i = 1/$nCatm1 {
local model "`model' /tau`i'"
}
* obtain ML estimates
ml model lf cr_lf `model' `wgt' if `touse', title(`link_title') ///
vce(`vcetype') maximize
* replace current b, V, and eqnames matrices
tempname b v
mat `b' = e(b)
mat `v' = e(V)
}
* case 2: all variables with non-parallel assumption
if ( "`free'" != "" & "`prop'" == "" & "`cnIV'" == "" ) {
local model "(eq1: `Y' = `free')"
forval i = 2/$nCatm1 {
local model "`model' (eq`i': `free')"
}
* obtain ML estimates
ml model lf cr_np_lf `model' `wgt' if `touse', title(`link_title') ///
vce(`vcetype') maximize
* replace current b, V, and eqnames matrices
tempname b v
mat `b' = e(b)
mat `v' = e(V)
}
* case 3: subset of variables with non-parallel assumption
if ( "`free'" != "" & "`prop'" == "" & "`cnIV'" != "" ) {
local model "(constrained: `Y' = `cnIV', nocons)"
forval i = 1/$nCatm1 {
local model "`model' (eq`i': `free')"
}
* obtain ML estimates
ml model lf cr_pnp_lf `model' `wgt' if `touse', title(`link_title') ///
vce(`vcetype') maximize
* replace current b, V, and eqnames matrices
tempname b v
mat `b' = e(b)
mat `v' = e(V)
}
* case 4: subset of variables with proportionality assumption
if ( "`free'" == "" & "`prop'" != "" & "`cnIV'" != "" ) {
local model "(constrained: `Y' = `cnIV', nocons) (factor: `prIV')"
forval i = 2/$nCatm1 {
local model "`model' /phi`i'"
}
* obtain ML estimates
ml model lf cr_pc_lf `model' `wgt' if `touse', title(`link_title') ///
vce(`vcetype') maximize
* replace current b, V, and eqnames matrices
tempname b v
mat `b' = e(b)
mat `v' = e(V)
}
* case 5: subset of variables with non-parallel assumption and
* proportionality assumption
if ( "`free'" != "" & "`prop'" != "" & "`cnIV'" != "" ) {
local model "(constrained: `Y' = `cnIV', nocons) (factor: `prIV', nocons)"
forval i = 1/$nCatm1 {
local model "`model' (eq`i': `free')"
}
forval i = 2/$nCatm1 {
local model "`model' /phi`i'"
}
* obtain ML estimates
ml model lf cr_ppc_lf `model' `wgt' if `touse', title(`link_title') ///
vce(`vcetype') maximize
* replace current b, V, and eqnames matrices
tempname b v
mat `b' = e(b)
mat `v' = e(V)
}
* Support for margins
* note: not implemented in this version
forval i = 1/$nCat {
local j = `Yval'[`i',1]
local mdflt `mdflt' predict(pr outcome(`j'))
}
* return and display results
ereturn scalar k_cat = $nCat
ereturn local cmd cratreg
ereturn local free `free'
ereturn local prop `prop'
ereturn local link `link'
ereturn local vce "`vce'"
ereturn local vceptype "`vcetype'"
ereturn local clustvar "`clustervar'"
ereturn repost b = `b' V = `v', rename
Replay, level(`level') `eform' `diopts' `options'
end
capture program drop Replay
program Replay
syntax [, Level(cilevel) or EForm *]
* display options
_get_diopts diopts options, `options'
local diopts `diopts' `eform' level(`level') `or'
ml display, `diopts'
end
capture program drop my_vce_parse
program my_vce_parse, rclass
syntax [, vce(string) ]
local case : word count `vce'
if ( `case' > 2 ) {
dis `"{red}{bf:vce(`vce')} invalid"'
exit 498
}
local 0 `", `vce'"'
syntax [, Robust Cluster * ]
if ( `case' == 2 ) {
if "`robust'" == "robust" | "`cluster'" == "" {
dis `"{red}{bf:vce(`vce')} invalid"'
exit 498
}
capture confirm numeric variable `options'
if _rc {
dis `"{red}{bf:vce(`vce')} invalid"'
exit 498
}
local clustervar "`options'"
}
else {
if ( "`robust'" == "" ) {
dis `"{red}{bf:vce(`vce')} invalid"'
exit 498
}
}
return clear
return local clustervar "`clustervar'"
end
/* History
1.0.0 11.15.16 initial program for arbitrary number of categories
1.0.1 06.21.17 updated labels
1.0.2 07.16.17 updated labels again
1.1.0 08.29.17 changed name of program
1.2.0 09.18.17 fixed bug with non-standard values for Y
1.2.1 09.20.17 updated exponential form options