[go: up one dir, main page]

Menu

[r19]: / trunk / dea_ci.ado  Maximize  Restore  History

Download this file

298 lines (249 with data), 8.7 kB

  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
*! version 1.0.0 03MAY2014
capture program drop dea_ci
program define dea_ci, rclass
version 11.0
// syntax checking and validation-----------------------------------------------
// -----------------------------------------------------------------------------
// returns 1 if the first nonblank character of local macro `0' is a comma,
// or if `0' is empty.
if replay() {
dis as err "ivars = ovars required."
exit 198
}
// quick check
#del ;
syntax anything(name=inoutvars equalok) [if] [in] [using/]
,
envars(varlist) // environment variable list
[
reps1(integer 100) // perform # bootstrap replications; default is 100
reps2(integer 2000) // perform # replications; default is 2000
rts(string) // ignore case sensitive,[{CRS|CCR}|{BCC|VRS}|DRS|IRS]
ort(string) // ignore case sensitive,[{IN|INPUT}|{OUT|OUTPUT}]
stage(integer 1) // dea stage 1 or 2
trace // Whether or not to do the log
* // other dea options(varname is options)
];
#del cr
// get invars and outvars
local varnames `inoutvars'
gettoken word inoutvars : inoutvars, parse("=")
while ~("`word'" == "=") {
local invars `invars' `word'
gettoken word inoutvars : inoutvars, parse("=")
}
local outvars `inoutvars'
unab invars : `invars'
unab outvars : `outvars'
// default model - CRS(Constant Return to Scale)
if ("`rts'" == "") {
local rts = "CRS"
}
// default orientation - Input Oriented
if ("`ort'" == "") local ort = "IN"
else {
local ort = upper("`ort'")
if ("`ort'" == "I" | "`ort'" == "IN" | "`ort'" == "INPUT") {
local ort = "IN"
}
else if ("`ort'" == "O" | "`ort'" == "OUT" | "`ort'" == "OUTPUT") {
local ort = "OUT"
}
else {
di as err "option ort allows for case-insensitive " _c
di as err "(i|in|input|o|out|output) or nothing."
exit 198
}
}
if ("`trace'" == "trace") {
di as txt "invars:[`invars']"
di as txt "outvars:[`outvars']"
di as txt "envars:[`envars']"
di as txt "reps1:[`reps1']"
di as txt "rts:[`rts']"
di as txt "ort:[`ort']"
di as txt "options:[`options']"
}
if ("`using'" != "") use "`using'", clear
if (~(`c(N)' > 0 & `c(k)' > 0)) {
dis as err "dataset required!"
exit 198
}
// end of syntax checking and validation ---------------------------------------
// REP1 : loop count of stage1
// REP2 : loop count of stage2
set more off
preserve
if ("`if'" != "" | "`in'" != "") {
qui keep `in' `if' // filtering : keep in range [if exp]
}
_stage1, invars(`invars') outvars(`outvars') envars(`envars') ///
reps1(`reps1') ///
rts(`rts') ort(`ort') stage(`stage') `trace' `options'
return add
restore, preserve
_stage2
restore, preserve
set more on
end
********************************************************************************
* STAGE1 -
********************************************************************************
program define _stage1, rclass
syntax, invars(varlist) outvars(varlist) envars(varlist) reps1(integer) ///
rts(string) ort(string) stage(integer) [trace *]
// declare the tempname
tempname dearslt
tempname TM EM T
capture drop theta_h1 theta_hs theta_hh
// step1: calculate the theta_h with dea
qui dea `invars' = `outvars', rts(`rts') ort(`ort') stage(`stage') `options'
matrix `dearslt' = r(dearslt)
matrix `TM' = `dearslt'[1...,"theta"]
mata: _roundmat("`TM'", 1e-14) // round off
matrix colnames `TM' = theta_h
svmat `TM', names(col)
if ("`ort'" == "IN") {
qui replace theta_h = 1 / theta_h
}
gen float theta_hs = 0
gen float theta_hh = 0
if ("`trace'" == "trace") {
di _n(2) as txt "[theta_h values as dea result.]:"
list
}
// step2: truncated regression theta_h on environment variables
qui truncreg theta_h `envars', ll(1) noconstant
scalar sigma_h = e(sigma)
matrix `EM' = e(b)
if ("`trace'" == "trace") {
di _n(2) as txt "[ereturn list as truncreg result.]:"
ereturn list sigma
di _n(2) as txt "[matrix list e(b) as truncreg result.]:"
matrix list e(b)
}
forvalues i = 1/`=`reps1'' {
capture drop _epsilon _theta_s _theta_hi
// step3: 1st bootstrap stage, L1 times : computing dea
// step3.1: calculate the epsilon1 using sigma_h in step 2.
gen float _epsilon = sigma_h * invnormal(normal(1/sigma_h) ///
+ (1 - normal(1/sigma_h)) * uniform())
// step3.2: calculate theta_s: _theta_s = ZiB + ei
gen float _theta_s = _epsilon
foreach envar of varlist `envars' {
matrix `T' = `EM'[1,"eq1:`envar'"]
qui replace _theta_s = _theta_s + `envar' * `T'[1,1] // beta_h
}
if ("`trace'" == "trace") {
di _n(2) as txt "[`i'][_theta_s = ZiB + ei]:"
list
}
// step3.3: reset the dataset using theta_h in step1:and theta_s in step3.2:
if ("`ort'" == "IN") {
foreach invar of varlist `invars' {
qui replace `invar' = `invar' * (theta_h / _theta_s)
}
}
else { // if ("`ort'" == "OUT") {
foreach outvar of varlist `outvars' {
qui replace `outvar' = `outvar' * (theta_h / _theta_s)
}
}
if ("`trace'" == "trace") {
di _n(2) as txt "[`i'][reset the dataset using theta_h]:"
list
}
// step3.4: compute dea with changed dataset in step 3.3:
qui dea `invars' = `outvars', ///
rts(`rts') ort(`ort') stage(`stage') `options'
matrix `dearslt' = r(dearslt)
matrix `TM' = `dearslt'[1...,"theta"]
mata: _roundmat("`TM'", 1e-14) // round off
matrix colnames `TM' = _theta_hi
svmat `TM', names(col)
// step4: Loop-i times
qui replace theta_hs = theta_hs + _theta_hi
if ("`trace'" == "trace") {
di _n(2) as txt "[`i'][result of calculate the theta_hs]:"
list
}
} // end of forvalues
capture drop _epsilon _theta_s _theta_hi
// step3.5: calculate mean of theta_hs
qui replace theta_hs = theta_hs / `reps1'
// step4: compute bias-corrected efficiency estimates
qui replace theta_hh = 2 * theta_h - theta_hs
if ("`trace'" != "trace") {
di _n(2) as txt "[result of stage1]:"
list
}
end
********************************************************************************
* STAGE2 -
********************************************************************************
program define _stage2, rclass
list
end
// Start of the MATA Definition Area -------------------------------------------
version 10
mata:
mata set matastrict on
// if function name start with '_', call at the stata.
/**
* used to look for special values:
* @param mat - matrix name
* @param cmp - compare type(ge, gt, eq, lt, le: default is eq)
* @param value - comparing value.
*/
function _all (string scalar mat, string scalar cmp, real scalar value)
{
real matrix M;
real scalar bool;
M = st_matrix(mat)
if (cmp == "ge") {
bool = all (M :>= value);
}
else if (cmp == "gt") {
bool = all (M :> value);
}
else if (cmp == "lt") {
bool = all (M :< value);
}
else if (cmp == "le") {
bool = all (M :<= value);
}
else { // if (cmp == "eq") {
bool = all (M :== value);
}
st_numscalar("r(bool)", bool)
}
/**
* used to look for special values:
* @param mat - matrix name
* @param cmp - compare type(ge, gt, eq, lt, le: default is eq)
* @param value - comparing value.
*/
function _any (string scalar mat, string scalar cmp, real scalar value)
{
real matrix M;
real scalar bool;
M = st_matrix(mat)
if (cmp == "ge") {
bool = any (M :>= value);
}
else if (cmp == "gt") {
bool = any (M :> value);
}
else if (cmp == "lt") {
bool = any (M :< value);
}
else if (cmp == "le") {
bool = any (M :<= value);
}
else { // if (cmp == "eq") {
bool = any (M :== value);
}
st_numscalar("r(bool)", bool)
}
end
// End of the MATA Definition Area ---------------------------------------------