forked from kerrydu/STATA-DEA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPvrs.ado
344 lines (300 loc) · 8.32 KB
/
TCPvrs.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
capture program drop TCPvrs
program define TCPvrs
version 12.1
// syntax checking and validation-----------------------------------------------
// rts - return to scale, ort - orientation
// -----------------------------------------------------------------------------
// 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 and ovars must be inputed."
exit 198
}
// 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("=:,")
}
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(min=1) [if] [in], ///
dmu(varname) time(varname)
foreach j in CEFF MCPI EFFCH TECCH Tbetween{
cap drop `j'
}
set matsize 2000
set more off
local bopvars "`varlist'"
local ninp: word count `invars'
local ngo: word count `gopvars'
local nbo: word count `bopvars'
local nout=`ngo'+`nbo'
qui {
order `gopvars' `bopvars' `invars'
tempvar tid
sort `time' `dmu'
*cap drop tid
egen `tid'=group(`time')
*tempname T
qui su `tid'
local T=r(max)
count if `tid'==1
local N1=r(N)
local N1=_N-`N1'
count if `tid'==`T'
local N2=r(N)
local N2=_N-`N2'
mat d11=J(_N,1,.)
mat d12=J(`N1',1,.)
mat d21=J(`N2',1,.)
}
disp "Computing Dt[t] & Dt+1[t+1]"
disp "Pls wait..."
qui {
local k=1
*local i=1
*local j=1
forvalues i=1/`T' {
*preserve
*keep if `tid'==`i'
*keep `gopvars' `bopvars' `invars'
cap mat drop Xmat dmutemp2 dmutemp1 objfun Xmat2 lamd fobj
mkmat `gopvars' `bopvars' `invars' if `tid'<=`i', mat(Xmat)
mkmat `gopvars' `bopvars' `invars' if `tid'==`i', mat(Xmat0)
qui count if `tid'==`i'
local nobs=r(N)
*disp `nobs'
qui count if `tid'<=`i'
local M0=r(N)
mat objfun=J(`M0',1,0)
mat lamd=J(`M0',1,1)
forvalues j=1/`nobs' {
cap mat drop dmutemp2 dmutemp1
* mat list Xmat
mat dmutemp2=Xmat0[`j',....]
*mat list dmutemp2
mat dmutemp2[1,`nout'+1]=J(1,`ninp',0)
mat dmutemp2[1,1]=J(1,`ngo',0)
mat dmutemp1=Xmat0[`j',....]
*display "kerry"
mat dmutemp1[1,`ngo'+1]=J(1,`nbo',0)
*mat dmutemp1[1,1]=J(1,`nout',0)
mat dmutemp2=-dmutemp2
*mat dmutemp2[1,`ngo'+1]=-dmutemp2[1,`ngo'+1..`nout']
mat dmutemp1=[0 \ dmutemp1' \1]
mat dmutemp2=[1 \ dmutemp2' \0]
mat Xmat2=[objfun, Xmat,lamd]
* mat Xmat2=[objfun, Xmat]
mat Xmat2=Xmat2'
preserve
clear
svmat Xmat2
*local vnames : colfullnames Xmat2
svmat dmutemp2, names(theta)
gen rel="<="
replace rel="=" in 1
replace rel=">=" if _n<=`ngo'+1 & _n>1
replace rel="=" if _n>`ngo'+1 & _n<=`nout'+1
*replace rel="=" if _n==2+`ninp'+`nout'
replace rel="=" in `=_N'
svmat dmutemp1, names(rhp)
list rel
lp Xmat* theta1, min rhs(rhp1)
mat fobj=r(lprslt)
mat d11[`k',1]=1/fobj[1,1]
local ++k
*list rel
*mat dir
restore
}
}
}
cap drop CEFF1 CEFF
svmat d11, names(CEFF)
rename CEFF1 CEFF
qui replace CEFF=1/CEFF
disp "Computing Dt+1[t] "
disp "Pls wait..."
qui {
local k=1
forvalues i=2/`T' {
*local i=2
*preserve
*keep if `tid'==`i'
*keep `gopvars' `bopvars' `invars'
cap mat drop Xmat dmutemp2 dmutemp1 objfun Xmat2 lamd
mkmat `gopvars' `bopvars' `invars' if `tid'<=`i', mat(Xmat)
mkmat `gopvars' `bopvars' `invars' if `tid'==`i'-1, mat(Xmat0)
*mat list Xmat0
qui count if `tid'==`i'-1
local nobs=r(N)
*disp `nobs'
qui count if `tid'<=`i'
local M0=r(N)
mat objfun=J(`M0',1,0)
mat lamd=J(`M0',1,1)
forvalues j=1/`nobs' {
* local j=1
cap mat drop dmutemp2 dmutemp1
* mat list Xmat
mat dmutemp2=Xmat0[`j',....]
*mat list dmutemp2
mat dmutemp2[1,`nout'+1]=J(1,`ninp',0)
mat dmutemp2[1,1]=J(1,`ngo',0)
mat dmutemp1=Xmat0[`j',....]
mat dmutemp1[1,`ngo'+1]=J(1,`nbo',0)
*mat dmutemp1[1,1]=J(1,`nout',0)
mat dmutemp2=-dmutemp2
*mat dmutemp2[1,`ngo'+1]=-dmutemp2[1,`ngo'+1..`nout']
mat dmutemp1=[0 \ dmutemp1'\1]
mat dmutemp2=[1 \ dmutemp2'\0]
*mat Xmm=[Xmat \ Xmat0[`j',....]]
mat Xmat2=[objfun, Xmat,lamd]
mat Xmat2=Xmat2'
preserve
clear
svmat Xmat2
*local vnames : colfullnames Xmat2
svmat dmutemp2, names(theta)
gen rel="<="
qui replace rel="=" in 1
qui replace rel=">=" if _n<=`ngo'+1 & _n>1
qui replace rel="=" if _n>`ngo'+1 & _n<=`nout'+1
qui replace rel="=" in `=_N'
svmat dmutemp1, names(rhp)
lp Xmat2* theta1, min rhs(rhp1)
mat fobj=r(lprslt)
mat d21[`k',1]=1/fobj[1,1]
local ++k
*list rel
*mat dir
restore
}
}
}
disp "Computing Dt[t+1]"
disp "Pls wait..."
* local k=`N1'+1
qui {
local k=1
forvalues i=1/`=`T'-1' {
*preserve
*keep if `tid'==`i'
*keep `gopvars' `bopvars' `invars'
cap mat drop Xmat dmutemp2 dmutemp1 objfun Xmat2 lamd
mkmat `gopvars' `bopvars' `invars' if `tid'<=`i', mat(Xmat)
mkmat `gopvars' `bopvars' `invars' if `tid'==`i'+1, mat(Xmat0)
qui count if `tid'==`i'+1
local nobs=r(N)
qui count if `tid'<=`i'
local M0=r(N)
mat objfun=J(`M0',1,0)
mat lamd=J(`M0',1,1)
forvalues j=1/`nobs' {
cap mat drop dmutemp2 dmutemp1
* mat list Xmat
mat dmutemp2=Xmat0[`j',....]
*mat list dmutemp2
mat dmutemp2[1,`nout'+1]=J(1,`ninp',0)
mat dmutemp2[1,1]=J(1,`ngo',0)
mat dmutemp1=Xmat0[`j',....]
mat dmutemp1[1,`ngo'+1]=J(1,`nbo',0)
*mat dmutemp1[1,1]=J(1,`nout',0)
mat dmutemp2=-dmutemp2
*mat dmutemp2[1,`ngo'+1]=-dmutemp2[1,`ngo'+1..`nout']
mat dmutemp1=[0 \ dmutemp1' \1]
mat dmutemp2=[1 \ dmutemp2' \0]
*mat Xmm=[Xmat \ Xmat0[`j',....]]
mat Xmat2=[objfun, Xmat, lamd]
mat Xmat2=Xmat2'
preserve
clear
svmat Xmat2
*local vnames : colfullnames Xmat2
svmat dmutemp2, names(theta)
gen rel="<="
replace rel="=" in 1
replace rel=">=" if _n<=`ngo'+1 & _n>1
replace rel="=" if _n>`ngo'+1 & _n<=`nout'+1
replace rel="=" in `=_N'
svmat dmutemp1, names(rhp)
lp Xmat2* theta1, min rhs(rhp1)
mat fobj=r(lprslt)
mat d12[`k',1]=1/fobj[1,1]
local ++k
*list rel
*mat dir
restore
}
}
cap drop d111 d211 d121 d221
*list d111 in 1/10
qui count if `tid'==1
local N1=r(N)
*qui count if `tid'==T
*local N2=r(N)
*local N2
mat d22=d11[`N1'+1...,1]
mat d11=d11[1..`N2',1]
svmat d11
svmat d22
svmat d21
svmat d12
cap drop MCPI EFFCH TECCH
*gen MCPI=sqrt(d11*d21/d12/d22)
*replace MCPI=d21/d22 if missing(MCPI)
cap drop EFFCH TECCH MCPI
gen EFFCH=d11/d22
*gen CEEF=d11
gen TECCH=sqrt(d21/d11*d22/d12)
replace TECCH=d21/d11 if missing(TECCH)
gen MCPI=EFFCH*TECCH
cap drop d11 d22 d21 d12 d111 d211 d121 d221
*replace mleffch=. if mleffch>=5
*replace mltech=. if mltech>=5
*replace mlindex=. if missing(mleffch)|missing(mltech)
}
display "Computation is completed!"
*cap drop d11 d22 d21 d12
sort `dmu' `time'
tempvar tru
qui bys `dmu': gen `tru'=`time'[_n+1]
cap drop Tbetween
qui gen Tbetween=string(`time')+"~"+string(`tru')
preserve
qui drop if missing(`tru')
qui keep `dmu' Tbetween MCPI EFFCH TECCH
order `dmu' Tbetween MCPI EFFCH TECCH
disp _n(2) "Total carbon emissions performance (MCPI) and its decomposition"
list, sep(0)
restore
qui{
bys `dmu': gen Tbetween1=Tbetween[_n-1]
bys `dmu': gen MCPI1=MCPI[_n-1]
bys `dmu': gen EFFCH1=EFFCH[_n-1]
bys `dmu': gen TECCH1=TECCH[_n-1]
drop Tbetween MCPI EFFCH TECCH
rename Tbetween1 Tbetween
rename MCPI1 MCPI
rename EFFCH1 EFFCH
rename TECCH1 TECCH
}
dis "Results are also plasted in the data set!"
dis "Pls check it!"
dis _newline
dis "------------------------------------------"
dis "@This code is written by Kerry@"
dis "@All rights are reserved@"
end