forked from kerrydu/sbmeff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbmeff.ado
288 lines (223 loc) · 6.74 KB
/
sbmeff.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
*! version 1.0
* By Kerry Du, 29 Oct 2019
capture program drop sbmeff
program define sbmeff, rclass
version 16
if strpos(`"`0'"',":")==0{ // Tone(2001) SBM without undesriable outputs
sbmeffnu `0'
exit
}
gettoken word 0 : 0, parse(" =:,")
while `"`word'"' != ":" & `"`word'"' != "=" {
if `"`word'"' == "," | `"`word'"'=="" {
error 198
}
local invars `invars' `word'
gettoken word 0 : 0, parse("=:,")
}
unab invars : `invars'
gettoken word 0 : 0, parse(" =:,")
while `"`word'"' != ":" & `"`word'"' != "=" {
if `"`word'"' == "," | `"`word'"'=="" {
error 198
}
local gopvars `gopvars' `word'
gettoken word 0 : 0, parse(" =:,")
}
unab gopvars : `gopvars'
syntax varlist [if] [in], dmu(varname) [Time(varname) SEQuential VRS SAVing(string) maxiter(numlist integer >0 max=1) tol(numlist max=1)]
if "`maxiter'"==""{
local maxiter=-1
}
if "`tol'"==""{
local tol=-1
}
if "`sequential'"!=""{
if "`time'"==""{
disp as error "For sequential SBM model, time() should be specified."
error 498
}
else{
local techflag "<="
}
}
preserve
marksample touse
markout `touse' `invars' `gopvars'
local bopvars `varlist'
local invars: list uniq invars
local gopvars: list uniq gopvars
local bopvars: list uniq bopvars
confirm numeric var `invars' `gopvars' `bopvars'
local comvars: list invars & gopvars
if !(`"`comvars'"'==""){
disp as error "`comvars' should not be specified as input and desriable output simultaneously."
error 498
}
local comvars: list invars & bopvars
if !(`"`comvars'"'==""){
disp as error "`comvars' should not be specified as input and undesriable output simultaneously."
error 498
}
local comvars: list gopvars & bopvars
if !(`"`comvars'"'==""){
disp as error "`comvars' should not be specified as desriable and undesriable outputs simultaneously."
error 498
}
local ninp: word count `invars'
local ngo: word count `gopvars'
local nbo: word count `bopvars'
local rstype=1
if "`vrs'"!=""{
local rstype=0
}
qui keep `invars' `gopvars' `bopvars' `dmu' `time' `touse'
qui gen _Row=_n
label var _Row "Row #"
qui keep if `touse'
qui gen double TE=.
label var TE "Technical Efficiency"
foreach v in `invars' `gopvars' `bopvars'{
qui gen double S_`v'=.
label var S_`v' `"Slack:`v'"'
local slackvars `slackvars' S_`v'
}
tempvar tvar dmu2
if `"`time'"'!="" {
qui egen `tvar'=group(`time')
qui egen `dmu2'=group(`dmu')
}
else{
qui gen `tvar'=1
qui gen `dmu2'=_n
}
sort `dmu2' `tvar' _Row
/*
if "`super'"!=""{
local sup "sup"
}
*/
qui mata mata mlib index
mata: sbmu(`"`invars'"',`"`gopvars'"',`"`bopvars'"',"`dmu2'","`tvar'",`rstype',"`techflag'","TE",`"`slackvars'"',`maxiter',`tol')
order _Row `dmu' `time' TE `slackvars'
keep _Row `dmu' `time' TE `slackvars'
disp _n(2) " SBM Efficiency Results:"
disp " (_Row: Row # in the original data; TE: Efficiency Score; S_X: Slack of X)"
//disp " S_X : Slack of X"
list _Row `dmu' `time' TE `slackvars', sep(0)
//disp _n
if `"`saving'"'!=""{
save `saving'
gettoken filenames saving:saving, parse(",")
local filenames `filenames'.dta
disp _n `"Estimated Results are saved in `filenames'."'
}
//tempname resmat
//mkmat _Row `dmu' `time' TE `slackvars', mat(`resmat')
//matrix list `resmat', noblank nohalf noheader f(%9.6g)
//return mat results=`resmat'
return local file `filenames'
restore
end
//////////////////////////////////////////////////////////////
capture program drop sbmeffnu
program define sbmeffnu, rclass
version 16
// disp "`0'"
// get and check invarnames
gettoken word 0 : 0, parse("=,")
while ~("`word'" == ":" | "`word'" == "=") {
if "`word'" == "," | "`word'" == "" {
error 198
}
local invars `invars' `word'
gettoken word 0 : 0, parse("=,")
//disp "`word'"
}
unab invars : `invars'
syntax varlist [if] [in], dmu(varname) [Time(varname) SEQuential VRS SAVing(string) maxiter(numlist integer >0 max=1) tol(numlist max=1)]
if "`maxiter'"==""{
local maxiter=-1
}
if "`tol'"==""{
local tol=-1
}
if "`sequential'"!=""{
if "`time'"==""{
disp as error "For sequential SBM model, time() should be specified."
error 498
}
else{
local techflag "<="
}
}
preserve
marksample touse
markout `touse' `invars'
//count if `touse'
local opvars `varlist'
local invars: list uniq invars
local opvars: list uniq opvars
confirm numeric var `invars' `opvars'
local comvars: list invars & opvars
if !(`"`comvars'"'==""){
disp as error "`comvars' should not be specified as input and output simultaneously."
error 498
}
local rstype=1
if "`vrs'"!=""{
local rstype=0
}
qui keep `invars' `opvars' `dmu' `time' `touse'
qui gen _Row=_n
label var _Row "Row #"
qui keep if `touse'
qui gen double TE=.
label var TE "Technical Efficiency"
foreach v in `invars' `opvars'{
qui gen double S_`v'=.
label var S_`v' `"Slack:`v'"'
local slackvars `slackvars' S_`v'
}
tempvar tvar dmu2
if `"`time'"'!="" {
qui egen `tvar'=group(`time')
qui egen `dmu2'=group(`dmu')
}
else{
qui gen `tvar'=1
qui gen `dmu2'=_n
}
sort `dmu2' `tvar' _Row
/*
if "`super'"!=""{
local sup "sup"
}
*/
qui mata mata mlib index
mata: sbm(`"`invars'"',`"`opvars'"',"`dmu2'","`tvar'",`rstype',"`techflag'","TE",`"`slackvars'"', `maxiter',`tol')
order _Row `dmu' `time' TE `slackvars'
keep _Row `dmu' `time' TE `slackvars'
disp _n(2) " SBM Efficiency Results:"
disp " (_Row: Row # in the original data; TE: Efficiency Score; S_X: Slack of X)"
//disp " S_X : Slack of X"
list _Row `dmu' `time' TE `slackvars', sep(0)
//disp _n
if `"`saving'"'!=""{
save `saving'
gettoken filenames saving:saving, parse(",")
local filenames `filenames'.dta
disp _n `"Estimated Results are saved in `filenames'."'
}
//tempname resmat
//mkmat _Row `dmu' `time' TE `slackvars', mat(`resmat')
//matrix list `resmat', noblank nohalf noheader f(%9.6g)
//return mat results=`resmat'
return local file `filenames'
restore
end
/*
make sbmeff, replace toc pkg title( Slacks-based Measure of Efficiency in Stata) ///
version(1.0) author(Kerry Du) affiliation(Xiamen University) ///
email([email protected]) install("sbmeff.ado;sbmeff.sthlp;lsbmeff.mlib")
*/