📄 frontier.ado
字号:
*! version 1.3.5 01apr2005
program define frontier, eclass sort byable(onecall) prop(ml_score)
if _by() {
local BY `"by `_byvars'`_byrc0':"'
}
`BY' _vce_parserun frontier : `0'
if "`s(exit)'" != "" {
exit
}
version 8
if replay() {
if _by() {
error 190
}
if "`e(cmd)'"~="frontier" {
error 301
}
if _by() {
error 190
}
Replay `0'
exit
}
`BY' sfe `0'
end
program define sfe, eclass sort byable(recall)
syntax varlist [fweight pweight iweight] [if] [in] /*
*/ [, cm(string) noCONStant CONSTraints(string) /*
*/ COST Distribution(string) FROM(string) /*
*/ UFrom(string) Level(cilevel) noLOg /*
*/ Vhet(string) Uhet(string) /*
not documented */ UTransformed SCore(passthru) /*
*/ noDIFficult * ]
ParseDist dist : `"`distribution'"'
if "`dist'" != "tnormal" {
if "`ufrom'" != "" {
local erropt "ufrom()"
}
if "`utransformed'" != "" {
local erropt "`erropt' utransformed"
}
if `"`cm'"' != "" {
local erropt "`erropt' cm()"
}
if "`erropt'" != "" {
AddCommas erropt : "`erropt'"
di as err "`erropt' only allowed with " /*
*/ "truncated-normal models"
exit 198
}
}
else {
if "`uhet'" ~= "" {
local erropt "uhet"
}
if "`vhet'" ~= "" {
local erropt "`erropt' vhet"
}
if "`erropt'" != "" {
AddCommas erropt : "`erropt'"
di as err "`erropt' not allowed with " /*
*/ "truncated-normal models"
exit 198
}
/* -utransformed- is undocumented */
if `"`from'"' != "" | `"`constraints'"' != "" {
local utransformed "utransformed"
}
}
if "`constant'"~="" {
local nocns="`constant'"
}
if "`difficult'" == "" {
local difficult difficult
}
local zhet `cm'
foreach opt in v u z {
if "``opt'het'" != "" {
local `opt'opt ``opt'het'
tokenize "``opt'opt'", parse(,)
unab `opt'het : `1'
confirm numeric var ``opt'het'
/* noCONStant */
if "`3'" != "" {
local l = length(`"`3'"')
if `"`3'"' == substr("noconstant", /*
*/ 1,max(6,`l')) {
local `opt'nocns "noconstant"
}
else {
di as err "`3' invalid"
exit 198
}
}
}
}
if "`cost'" != "" {
global S_COST=-1
local function "cost"
}
else {
global S_COST=1
local function "production"
}
marksample touse
/*Parse variable list */
gettoken lhs varlist : varlist
if "`varlist'"=="" & "`constant'"~="" {
error 102
}
/* check `lhs' not constant */
qui _rmcoll `lhs'
if "`r(varlist)'" == "" {
di as err "dependent variable cannot be constant"
exit 198
}
tempvar wvar
if "`weight'"~="" {
gen double `wvar' `exp' if `touse'
local wtopt "[`weight'=`wvar']"
}
else {
gen byte `wvar' = 1
}
markout `touse' `uhet' `vhet' `zhet' `wvar'
qui count if `touse'==1
if r(N) == 0 {
error 2000
}
mlopts mlopts, `options' const(`constraints')
local cns `s(constraints)'
/* 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)'
if "`uhet'"~="" {
local u "u"
cap noi _rmdcoll `lhs' `uhet' if `touse' `wtopt', `unocns'
if _rc {
di as err "some variables specified in uhet() " /*
*/ "collinear with the dependent variable"
exit _rc
}
local uhet `r(varlist)'
}
if "`vhet'"~="" {
local v "v"
cap noi _rmdcoll `lhs' `vhet' if `touse' `wtopt', `vnocns'
if _rc {
di as err "some variables specified in vhet() " /*
*/ "collinear with the dependent variable"
exit _rc
}
local vhet `r(varlist)'
}
if "`zhet'"~="" {
local z "z"
cap noi _rmdcoll `lhs' `zhet' if `touse' `wtopt', `znocns'
if _rc {
di as err "some variables specified in cm() " /*
*/ "collinear with the dependent variable"
exit _rc
}
local zhet `r(varlist)'
}
if "`ufrom'" ~= "" {
if "`from'" ~= "" {
noi di as err "from() cannot be " /*
*/ "specified with ufrom()"
exit 198
}
if "`constraints'" ~= "" {
noi di as err "constraints() cannot be " /*
*/ "specified with ufrom()"
exit 198
}
if "`utransformed'" ~= "" {
noi di as err "utransformed cannot be " /*
*/ "specified with ufrom()"
exit 198
}
cap confirm matrix `ufrom'
if _rc {
di as err "ufrom() must specify a matrix"
exit 198
}
tempname b_u s2 btemp
mat `b_u' = `ufrom'
local ncol = colsof(`b_u')
/* check dimension */
local dim : word count `varlist'
if "`constant'" == "" {
local dim = `dim' + 3
}
else {
local dim = `dim' + 2
}
if "`cm'" != "" {
local dim_cm : word count `zhet'
local dim = `dim' + `dim_cm' + ("`znocns'"=="")
}
else {
local dim = `dim' + 1
}
if `dim' != `ncol' {
di as err "ufrom() must specify a 1 x `dim' matrix"
exit 198
}
mat `btemp' = `b_u'[1,1..`ncol'-2]
scalar `s2' = exp(`b_u'[1,`ncol'])
mat `btemp' = `btemp'/sqrt(`s2')
mat `b_u' = `btemp', `b_u'[1,`ncol'-1], `b_u'[1,`ncol']
local from "`b_u', copy"
}
/* even -noconstant- specified, there
will be a constant term in OLS, which
is -$S_COST*E(u_i) */
qui reg `lhs' `varlist' `wtopt' if `touse'
tempname ll0_u
scalar `ll0_u'=e(ll)
/* 3rd moment test */
local nobs=e(N)
tempvar olsres olsres2 olsres3
tempname m2 m3 b0 m3t p_m3t
matrix `b0'=e(b)
qui predict double `olsres' if `touse', res
/* -summarize- does not allow pweights;
force weights to be iweights since
we only need the mean */
qui gen double `olsres2'=`olsres'^2 if `touse'
qui sum `olsres2' [iw=`wvar'] if `touse', meanonly
scalar `m2'=r(mean)
qui gen double `olsres3'=`olsres'^3 if `touse'
qui sum `olsres3' [iw=`wvar'] if `touse', meanonly
scalar `m3'=r(mean)
scalar `m3t'=`m3'/sqrt(6*`m2'^3/`nobs')
/* negative skewness, so one-side test */
scalar `p_m3t' = norm($S_COST*`m3t')
/* correct 3rd moment to be negative */
scalar `m3'=cond($S_COST*r(sum)<0, `m3', -.0001*$S_COST)
/* Use MOM to obtain starting values */
if "`from'"!="" {
local start "init(`from')"
}
else {
tempname ou2 ov2 cons0
if "`dist'" == "hnormal" | "`dist'" == "tnormal" {
scalar `ou2' = ($S_COST*`m3'/(sqrt(2/_pi) /*
*/ *(1-4/_pi)))^(2/3)
scalar `ov2' = `m2' - (1-2/_pi)*`ou2'
scalar `ov2' = cond( `ov2'>0, `ov2', .0001)
if "`constant'" == "" {
scalar `cons0' = _b[_cons] /*
*/ + $S_COST*(sqrt(2/_pi))*sqrt(`ou2')
mat `b0'[1,colsof(`b0')]=`cons0'
}
else {
local cols = colsof(`b0') - 1
mat `b0' = `b0'[1,1..`cols']
}
}
if "`dist'" == "exponential" {
scalar `ou2' =(-$S_COST*`m3'/2)^(2/3)
scalar `ov2' = `m2' - `ou2'
scalar `ov2' = cond( `ov2'>0, `ov2', .0001)
if "`constant'" == "" {
scalar `cons0' = _b[_cons] /*
*/ + $S_COST*sqrt(`ou2')
mat `b0'[1,colsof(`b0')]=`cons0'
}
else {
local cols = colsof(`b0') - 1
mat `b0' = `b0'[1,1..`cols']
}
}
if "`vhet'"=="" & "`uhet'"=="" {
mat `b0'=`b0', ln(`ov2'), ln(`ou2')
}
else if "`vhet'"~="" & "`uhet'"=="" {
tempvar lnv2 g
qui gen double `lnv2'=ln(`ov2') if `touse'
qui regress `lnv2' `vhet' /*
*/ `wtopt' if `touse', `vnocns'
mat `b0'=`b0', e(b), ln(`ou2')
}
else if "`vhet'"=="" & "`uhet'"~="" {
tempvar lnu2
qui gen double `lnu2'=ln(`ou2') if `touse'
qui reg `lnu2' `uhet' /*
*/ `wtopt' if `touse', `unocns'
mat `b0'=`b0', ln(`ov2'), e(b)
}
else if "`vhet'"~="" & "`uhet'"~="" {
tempvar lnv2 lnu2
tempname bv bu
qui gen double `lnv2'=ln(`ov2') if `touse'
qui reg `lnv2' `vhet' /*
*/ `wtopt' if `touse', `vnocns'
mat `bv'=e(b)
qui gen double `lnu2'=ln(`ou2') if `touse'
qui reg `lnu2' `uhet' /*
*/ `wtopt' if `touse', `unocns'
mat `bu'=e(b)
mat `b0'=`b0', `bv', `bu'
}
if "`dist'" == "tnormal" {
local nx : word count `varlist'
if "`constant'"=="" {
local nx = `nx' + 1
}
tempname bx sigmaS2 lnsigS2 gamma ltgamma mu
scalar `mu' = 0
scalar `sigmaS2' = `ou2' + `ov2'
scalar `gamma' = `ou2'/`sigmaS2'
scalar `lnsigS2' = ln(`sigmaS2')
scalar `ltgamma' = ln(`gamma'/(1-`gamma'))
mat `bx'=`b0'[1,1..`nx']
if "`utransformed'" == "" {
mat `bx'=`bx'/sqrt(`sigmaS2')
scalar `mu' = `mu'/sqrt(`sigmaS2')
}
if `"`zhet'"'=="" {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -