xtfrontier.ado
来自「是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到」· ADO 代码 · 共 467 行
ADO
467 行
*! version 1.4.1 20dec2004
program define xtfrontier, eclass byable(onecall)
if _by() {
local BY `"by `_byvars'`_byrc0':"'
}
`BY' _vce_parserun xtfrontier, panel jkopts(eclass) : `0'
if "`s(exit)'" != "" {
exit
}
version 8
if replay() {
if _by() {
error 190
}
if "`e(cmd)'" != "xtfrontier" {
error 301
}
Replay `0'
exit
}
qui syntax varlist(ts) [fweight iweight] [if] [in] /*
*/ [, ti tvd Level(cilevel) * ]
local wc : word count `ti' `tvd'
if `wc' > 1 {
di as err "ti and tvd cannot both be specified"
exit 198
}
if `wc' == 0 {
di as err "either ti or tvd must be specified"
exit 198
}
if _by() {
by `_byvars' `_byrc0': xtsfEST `0'
}
else xtsfEST `0'
end
program define xtsfEST, eclass byable(recall) sortpreserve
syntax varlist(ts) [fweight iweight] [if] [in] /*
*/ [, noCONStant COST /*
*/ I(varname num) T(varname num) /*
*/ FROM(string) Level(passthru) noLOg TI TVD /*
*/ noDIFficult * ]
if "`constant'" != "" {
local nocns=", `constant'"
}
if "`difficult'" == "" {
local difficult difficult
}
marksample touse
/* Parse variable list */
gettoken lhs varlist : varlist
if "`varlist'" == "" & "`constant'" != "" {
error 102
}
/* put ts ops in cannonical
* form
*/
tsunab lhs : `lhs'
/* check `lhs' not constant */
qui _rmcoll `lhs'
if "`r(varlist)'" == "" {
di as err "dependent variable cannot be constant"
exit 198
}
/* set panelvar and timevar */
xt_iis `i'
local ivar "`s(ivar)'"
cap xt_tis `t' /* -t()- is not required */
local tvar "`s(timevar)'"
if "`ti'" == "" & _rc {
xt_tis `t'
}
tempvar wvar
if "`weight'"~="" {
qui gen double `wvar' `exp' if `touse'
sort `touse' `ivar' /* sort to use _crcchkw */
_crcchkw `ivar' `wvar' `touse'
sort `ivar' `tvar' /* restore order */
local wtopt "[`weight'=`wvar']"
}
else {
qui gen byte `wvar' = 1
}
markout `touse' `ivar' `tvar' `wvar' /* iis does not allow string */
qui count if `touse' == 1
if r(N) == 0 {
error 2000
}
global S_XTby "`ivar'"
global S_XTt "`tvar'"
/* remove collinearity */
cap noi _rmdcoll `lhs' `varlist' if `touse' `wtopt' `nocns'
if _rc {
di as err "some independent variables " /*
*/ "collinear with the dependent variable"
exit _rc
}
local varlist `r(varlist)'
local names `varlist'
/* time-series operator */
local eq1 : subinstr local lhs "." "_", all
tsrevar `varlist'
local varlist `r(varlist)'
local lhsname `lhs'
tsrevar `lhs'
local lhs `r(varlist)'
markout `touse' `varlist' `lhs'
if "`cost'"~="" {
global S_COST=-1
local function "cost"
if "`constant'"~="" {
local costopt "cost"
}
else local costopt ", cost"
}
else {
global S_COST=1
local function "production"
}
mlopts mlopts, `options'
qui reg `lhs' `varlist' `wtopt' if `touse' `nocns'
/* starting values */
if "`from'"=="" {
/* Searching for starting values using MOM " */
if "`ti'" == "" {
tempname T
qui summ `tvar' if `touse', meanonly
local Tmax = r(max)
local nobs = r(N)
/* time dummies */
qui tab `tvar' if `touse', gen(`T')
local Tnum = r(r)
/* there is no constant term in OLS */
qui reg `lhs' `varlist' `T'1-`T'`Tnum' `wtopt', nocons
tempvar olsres olsres2 olsres3 m3 interv etai y
tempname b0 m2 m3T ou2 ov2 eta cons0
matrix `b0'=e(b)
qui predict double `olsres' if `touse', res
qui gen double `olsres2'=`olsres'^2 if `touse'
qui sum `olsres2' `wtopt' if `touse', meanonly
scalar `m2' = r(mean)
qui gen double `olsres3'=`olsres'^3 if `touse'
qui gen double `m3'=.
forvalues i=1/`Tnum' {
qui sum `olsres3' `wtopt' if `T'`i', meanonly
qui replace `m3' = /*
*/ cond($S_COST*r(sum)<0, /*
*/ r(mean), -.0001*$S_COST) /*
*/ if `T'`i'
if `i'==`Tnum' {
scalar `m3T' = /*
*/ cond($S_COST*r(sum)<0, /*
*/ r(mean), -.0001*$S_COST)
}
}
scalar `ou2'=($S_COST*`m3T'/sqrt(2/_pi) /*
*/ /(1-4/_pi))^(2/3)
qui gen double `etai' = ($S_COST*`m3'/sqrt(2/_pi) /*
*/ /(1-4/_pi))^(1/3)/sqrt(`ou2') if `touse'
qui gen double `y' = ln(`etai') if `touse'
qui gen `interv' = -(`tvar'-`Tmax') if `touse'
qui reg `y' `interv' `wtopt', nocons
scalar `eta' = _b[`interv']
local etaopt ", `eta'"
qui replace `etai' = `etai'^2
qui sum `etai' `wtopt' if `touse', meanonly
scalar `ov2' = `m2'-`ou2'*(1-2/_pi)*r(mean)
scalar `ov2' = cond( `ov2'>0, `ov2', .0001)
/* adjust constant term */
local num : word count `varlist'
if "`constant'" == "" {
scalar `cons0'=0
forvalues i=1/`Tnum' {
local j = `i'+`num'
tempvar uitemp
qui gen double `uitemp' = /*
*/ sqrt(`ou2') /*
*/ *exp(`eta'*`interv')/*
*/ if `T'`i'
summ `uitemp' if `T'`i', meanonly
scalar `cons0' = `b0'[1,`j'] /*
*/ + $S_COST*sqrt(2/_pi) /*
*/ *r(mean) /*
*/ +`cons0'
}
scalar `cons0'=`cons0'/`Tnum'
if `num' > 0 {
mat `b0' = (`b0'[1,1..`num'],`cons0')
}
else {
mat `b0' = (`cons0')
}
}
else {
mat `b0' = `b0'[1,1..`num']
}
}
/* time-invariant TE effects */
else {
tempname b0 ou2 ov2 cons0
/* weights won't work with -xtreg, re-
-noconstant- is not allowed */
qui xtreg `lhs' `varlist' if `touse', /*
*/ i(`ivar') fe
matrix `b0'=e(b)
scalar `ov2' = e(sigma_e)^2
scalar `ou2' = e(sigma_u)^2/(1-2/_pi)
/* adjust constant term */
local num : word count `varlist'
if "`constant'" == "" {
scalar `cons0' = `b0'[1,`num'+1] /*
*/ + $S_COST*sqrt(2/_pi)*sqrt(`ou2')
if `num' > 0 {
mat `b0' = (`b0'[1,1..`num'],`cons0')
}
else {
mat `b0' = (`cons0')
}
}
else {
mat `b0' = `b0'[1,1..`num']
}
local etaopt
}
tempname lnsigS2 gamma mu ltgamma cons0
scalar `lnsigS2' = ln(`ou2'+`ov2')
scalar `gamma' = `ou2'/(`ou2'+`ov2')
scalar `ltgamma' = ln(`gamma'/(1-`gamma'))
scalar `mu' = 0
mat `b0' = (`b0', `lnsigS2', `ltgamma', `mu' `etaopt')
local start "init(`b0', copy)"
}
else {
local start "init(`from')"
}
if "`ti'" ~= "" {
local prog "xtsf_llti"
local model "ti"
local effect "Time-invariant"
local eq_eta
}
else {
local prog "xtsf_ll"
local model "tvd"
local effect "Time-varying decay"
local eq_eta "(eta:)"
}
ml model d2 `prog' (`eq1': `lhs'=`varlist' `nocns') /*
*/ (lnsigma2:) (ilgtgamma:) (mu:) `eq_eta' /*
*/ `wtopt' if `touse', /*
*/ max miss `start' search(off) nopreserve /*
*/ `mlopts' `log' `constr' `difficult' /*
*/ title("`effect' inefficiency model")
/* backup e() results */
local scalars : e(scalars)
foreach i of local scalars {
local eret_s`i' = e(`i')
}
local macros : e(macros)
foreach i of local macros {
if "`i'" != "depvar" {
local eret_m`i' `e(`i')'
}
}
tempname b V C ilog
mat `b' = e(b)
mat `V' = e(V)
mat `ilog' = e(ilog)
local bnames : colnames `b'
if `"`varlist'"' != "" {
local i = 1
foreach var of local varlist {
local orig: word `i' of `names'
local bnames : subinstr local bnames `"`var'"' /*
*/ `"`orig'"', word
local i = `i' + 1
}
}
mat colnames `b' = `bnames'
mat rownames `V' = `bnames'
mat colnames `V' = `bnames'
/* constraints */
capture mat `C' = get(Cns)
if _rc {
eret post `b' `V', depname(`lhsname') esample(`touse')
}
else {
eret post `b' `V' `C', depname(`lhsname') esample(`touse')
}
/* restore e() results */
foreach i of local scalars {
eret scalar `i' = `eret_s`i''
}
foreach i of local macros {
if "`i'" != "depvar" {
eret local `i' `"`eret_m`i''"'
}
}
eret mat ilog = `ilog'
/* for header display */
qui {
tempvar Ti touse1
qui gen byte `touse1' = e(sample)
sort `touse1' `ivar'
if "`weight'" == "iweight" {
replace `wvar' = 1 if `touse1'
}
by `touse1' `ivar': gen double `Ti' = sum(`touse1'*`wvar')
by `touse1' `ivar': replace `Ti' = . if _n~=_N
count if `Ti'~=0 & `Ti'<.
local N = r(N) /* # of panels */
summ `Ti' if `Ti'>0, meanonly
eret scalar N_g = `N'
eret scalar g_min = r(min)
eret scalar g_avg = r(mean)
eret scalar g_max = r(max)
eret scalar Tcon = ( r(min) == r(max) )
}
eret local depvar "`lhsname'"
eret local wexp "`exp'" /* repost weight expression */
eret local predict "xtfront_p"
eret local function "`function'"
eret local model "`model'"
eret local ivar "`ivar'"
eret local tvar "`tvar'"
eret scalar sigma2 = exp([lnsigma2]_cons)
eret scalar gamma = exp([ilgtgamma]_cons)/(1+exp([ilgtgamma]_cons))
eret scalar sigma_u = sqrt(`e(gamma)'*`e(sigma2)')
eret scalar sigma_v = sqrt((1-`e(gamma)')*`e(sigma2)')
eret local cmd "xtfrontier" /* last eret */
Replay, `level'
mac drop S_COST
end
/* ---------------------------DISPLAY------------------------------------- */
program define Replay
syntax [, Level(cilevel)]
/* Header */
cap confirm integer number `e(g_avg)'
if _rc == 0 {
local avgfmt "%9.0f"
}
else {
local avgfmt "%9.1f"
}
if `"`e(tvar)'"' != "" {
local disp_t /*
*/ as txt "Time variable (t): " as res abbrev("`e(tvar)'",12)
}
di as txt _n "`e(title)'" _col(49) "Number of obs" _col(68) "=" /*
*/ _col(70) as res %9.0g e(N)
di as txt "Group variable (i): " as res abbrev("`e(ivar)'",12) /*
*/ _col(49) as txt "Number of groups" _col(68) "=" /*
*/ _col(70) as res %9.0g e(N_g) _n
di `disp_t' /*
*/ as txt _col(49) "Obs per group: min" /*
*/ _col(68) "=" _col(70) as res %9.0g e(g_min)
di as txt _col(64) "avg" _col(68) "=" _col(70) as res `avgfmt' e(g_avg)
di as txt _col(64) "max" _col(68) "=" _col(70) as res %9.0g e(g_max)
if !missing(e(df_r)) {
local model as txt _col(49) "F(" ///
as res %4.0f e(df_m) as txt "," ///
as res %7.0f e(df_r) as txt ")" ///
_col(68) "=" _col(70) as res %9.2f abs(e(F))
local pvalue _col(49) as txt "Prob > F" _col(68) "=" ///
as res _col(73) %6.4f Ftail(e(df_m),e(df_r),e(F))
}
else {
local model as txt _col(49) "`e(chi2type)' chi2(" ///
as res e(df_m) as txt ")" ///
_col(68) "=" _col(70) as res %9.2f abs(e(chi2))
local pvalue _col(49) as txt "Prob > chi2" _col(68) "=" ///
as res _col(73) %6.4f chiprob(e(df_m),abs(e(chi2)))
}
di as txt _n `model'
if "`e(ll)'" != "" {
di as txt "Log likelihood = " as res %10.0g e(ll) `pvalue'
}
else {
di `pvalue'
}
di
/* end header */
eret display, level(`level') first plus
_diparm mu, level(`level')
if "`e(model)'" == "tvd" {
_diparm eta, level(`level')
}
_diparm lnsigma2, level(`level')
_diparm ilgtgamma, level(`level')
di as text "{hline 13}" _col(14) `"{c +}"' "{hline 64}"
_diparm lnsigma2, level(`level') exp label(sigma2)
_diparm ilgtgamma, level(`level') ilogit label(gamma)
_diparm lnsigma2 ilgtgamma, /*
*/ func( exp(@1)*exp(@2)/(1+exp(@2)) ) /*
*/ der( exp(@1)*exp(@2)/(1+exp(@2)) /*
*/ exp(@1)*(exp(@2)/(1+exp(@2))-(exp(@2)/(1+exp(@2)))^2) ) /*
*/ label(sigma_u2)
_diparm lnsigma2 ilgtgamma, /*
*/ func( exp(@1)*(1-exp(@2)/(1+exp(@2))) ) /*
*/ der( exp(@1)*(1-exp(@2)/(1+exp(@2))) /*
*/ (-exp(@1))*(exp(@2)/(1+exp(@2))-(exp(@2)/(1+exp(@2)))^2))/*
*/ label(sigma_v2)
di as text "{hline 13}" _col(14) `"{c BT}"' "{hline 64}"
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?