*! version 2.0.0 25oct2016 *! version 1.0.4 02jun2016 *! version 1.0.3 25oct2015 *! version 1.0.2 11sep2015 *! version 1.0.1 03jul2015 *! version 1.0.0 01jun2015 /* -sfkk- version 1.0.0 June 1, 2015 Program Author: Dr. Mustafa Ugur Karakaplan E-mail: mukarakaplan@yahoo.com Website: www.mukarakaplan.com Recommended Citations: The following two citations are recommended for referring to the sfkk program package and the underlying econometric methodology: Karakaplan, Mustafa U. (2016) "Estimating Endogenous Stochastic Frontier Models in Stata." Forthcoming. The Stata Journal. Also available at www.mukarakaplan.com Karakaplan, Mustafa U. and Kutlu, Levent (2013) "Handling Endogeneity in Stochastic Frontier Analysis." Available at www.mukarakaplan.com */ program sfkk version 13.1 if replay() { if "`2'"=="version" | "`2'"=="ver" | "`2'"=="vers" | "`2'"=="versi" | "`2'"=="versio" { di _n(1) "{bf:{ul:Version}}" di _n(1) "{txt}{sf} sfkk version 2.0.0" di " October 25, 2016" di _n(1) "{bf:{ul:Program Author}}" di _n(1) " Dr. Mustafa Ugur Karakaplan" di `" E-mail: {browse "mailto:mukarakaplan@yahoo.com":mukarakaplan@yahoo.com}"' di " Website: {browse www.mukarakaplan.com}" di _n(1) "{pstd}For comments, suggestions, or questions about {cmd: sfkk}," di "please send an email to me." di _n(1) "{bf:{ul:Recommended Citations}}" di _n(1) "{pstd}The following two citations are recommended for referring to the sfkk program " di "package and the underlying econometric methodology: {p_end}" di _n(1) `"{phang}Karakaplan, Mustafa U. (2016) "Estimating Endogenous Stochastic"' di `"Frontier Models in Stata." {it:Forthcoming.} {browse "www.stata-journal.com":The Stata Journal}."' di " Also available at {browse www.mukarakaplan.com}{p_end}" di _n(1) "{phang}Karakaplan, Mustafa U. and Kutlu, Levent (2013)" di `""Handling Endogeneity in Stochastic Frontier Analysis." "' di `"Available at "' di "{browse www.mukarakaplan.com}{p_end}" exit } else if ("`e(cmd)'" != "sfkk") error 301 Replay `0' } else Estimate `0' end program Estimate, eclass syntax varlist(min=2 fv) [pweight fweight iweight aweight] [if] [in], [ COST PRODuction ENdogenous(varlist fv) /* */ Instruments(varlist fv) EXogenous(varlist fv) LEAVEout(varlist fv) Uhet(string) Whet(string) /* */ noCONStant INITial(string) DELVE FAST(string) SAVE(string) LOAD(string) NOMESSage NOOUTput NOREFresh /* */ EFFiciency(string) TEST TIMER BEEP DIFficult ITERate(string) TECHnique(string) /* */ HEADER COMPare NICEly MLMAX(string) MLMODel(string) MLDISplay(string) ] if ("`timer'"!="") { timer clear 1 timer on 1 } if ("`norefresh'"=="") { capture estimates drop ModelEN capture estimates drop ModelEX } gettoken lhs frontier : varlist _fv_check_depvar `lhs' ereturn clear if"`weight'" != "" local wgt "[`weight'`exp']" marksample touse capture set matsize 800 capture set matsize 11000 local porc = "production" //default is production frontier scalar prod = 1 if ("`production'" !="" & "`cost'" !="") { di as error "{p}Specify either {bf:{ul:prod}uction} or {bf:{ul:cost}}. Do not specify both." error 198 } else if ("`cost'" !="") { local porc = "cost" scalar prod = -1 } if("`technique'"!="" & "`fast'"=="") { if strpos("`technique'", "bhhh") != 0 { di as error "{p}bhhh technique is only allowed with {bf:fast({it:#})} specification." error 198 } } if ("`efficiency'"!="") { tokenize "`efficiency'", parse(",", " ") if ("`2'"!="," & "`2'"!="") { di as error "{p}Too many efficiency variables are specified to be generated." error 103 } capture summ `1'_EN if (_rc!=111 & "`3'"!="replace") { di as error "{p}`1'_EN is specified to be the efficiency variable but the variable is already in the data and the replace option is not specified. Either specify a new efficiency variable or specify the replace option." error 110 } if ("`compare'"!="") { capture summ `1'_EX if (_rc!=111 & "`3'"!="replace") { di as error "{p}`1'_EX is specified to be the efficiency variable but the variable is already in the data and the replace option is not specified. Either specify a new efficiency variable or specify the replace option." error 110 } } local effvar = "`1'" } if ("`uhet'"!="") { gettoken urhs uno: uhet, parse(",") if (strpos("`uno'",",")!=0 & strpos("`uno'","nocons")!=0) local unocons=1 else local unocons=0 if ("`urhs'"==",") { di as err "{p}Specify at least one variable or the constant in Uhet." error 198 } } else local unocons=0 if ("`whet'"!="") { gettoken wrhs wno: whet, parse(",") if (strpos("`wno'",",")!=0 & strpos("`wno'","nocons")!=0) { di as err "{p}noconstant option is not allowed in Whet." error 198 } if ("`wrhs'"==",") { di as err "{p}Invalid Whet syntax." error 198 } } if ("`norefresh'"=="") global exo=0 if (("`endogenous'"=="" | "`instruments'"=="") & "`norefresh'"=="" ) { global exo=1 if ("`nomessage'"=="") { di as error "{p}Specify both {bf:{ul:en}dogenous()} and {bf:{ul:i}nstruments()} to analyze the endogenous model." di _n(2) in red "Analyzing the exogenous model (Model EX)..." _n(1) sfkk `lhs' `frontier' `wgt' if `touse', `porc' u(`uhet') w(`whet') `constant' tech(`technique') `beep' noref di as error _n(1) "{p}Warning: Exogenous model is analyzed. Specify both {bf:{ul:en}dogenous()} and {bf:{ul:i}nstruments()} to analyze the endogenous model." if ("`timer'"!="") { timer off 1 capture timer list 1 di _n(2) in red "Completed in " `=int(r(t1)/3600)' " hour(s), " `=int((r(t1)-(int(r(t1)/3600)*3600))/60)' " minute(s) and " `=round(r(t1)-int((r(t1)-(int(r(t1)/3600)*3600))/60)*60)' " second(s)." } exit } } if ("`instruments'"!="") { forvalues j = 1/`=wordcount("`instruments'")' { if (strpos("`frontier'", "`=word("`instruments'", `j')'") != 0) { di as error "{p}Instrumental variable `=word("`instruments'", `j')' is specified as a frontier variable." error 110 } if (strpos("`urhs'", "`=word("`instruments'", `j')'") != 0) { di as error "{p}Instrumental variable `=word("`instruments'", `j')' is specified as a uhet variable." error 110 } if (strpos("`whet'", "`=word("`instruments'", `j')'") != 0) { di as error "{p}Instrumental variable `=word("`instruments'", `j')' is specified as a whet variable." error 110 } if (strpos("`endogenous'", "`=word("`instruments'", `j')'") != 0) { di as error "{p}Instrumental variable `=word("`instruments'", `j')' is specified as an endogenous variable." error 110 } if (strpos("`exogenous'", "`=word("`instruments'", `j')'") != 0 | strpos("`leaveout'", "`=word("`instruments'", `j')'") != 0) { di as error "{p}Instrumental variable `=word("`instruments'", `j')' is specified as an included exogenous variable." error 110 } } } if ("`exogenous'" !="" & "`leaveout'" !="") { di as error "{p}Specify either {bf:{ul:ex}ogenous({it:exovarlist})} or {bf:{ul:leave}out({it:lovarlist})}. Do not specify both." error 198 } global fastroute = 0 if ("`fast'"!="") { if (`fast'<=0) { di as error "{p}fast(`fast') is invalid -- invalid number, outside of allowed range" di as error "{p}fast({it:#}) can be specified to take any value larger than 0." error 125 } global fastroute = 1 } global p = wordcount("`endogenous'") forvalues j = 1/$p { if (strpos("`frontier'", "`=word("`endogenous'", `j')'") ==0) { if (strpos("`urhs'", "`=word("`endogenous'", `j')'") ==0) { di as error "{p}`=word("`endogenous'", `j')' is specified as an endogenous variable but not specified as a frontier or uhet variable." error 198 } } } if ("`exogenous'"=="") { local allexogenous "`frontier' `urhs' `whet'" local leftout = wordcount("`leaveout'") forvalues j = 1/`leftout' { local allexogenous : subinstr local allexogenous "`=word("`leaveout'", `j')'" "", word all } local allexogenous "`instruments' `allexogenous'" } else local allexogenous "`instruments' `exogenous'" forvalues j = 1/$p { local allexogenous : subinstr local allexogenous "`=word("`endogenous'", `j')'" "", word all } local iveq = "" forvalues j = 1/$p { local ivin = word("`endogenous'", `j') local iveq "`iveq'(ivr`j'_`=word("`endogenous'", `j')': `=word("`endogenous'", `j')' = `allexogenous') (eta`j'_`=word("`endogenous'", `j')': ) " } local le = "" forvalues j = 1/`=($p*($p+1))/2' { local le = "`le'(le`j': ) " } foreach x in lhs frontier urhs whet endogenous instruments allexogenous { local `x': list retokenize `x' local `x': list uniq `x' } if("`header'"!="") { di _n(2) in red "{p}{sf:`c(current_date)' `c(current_time)'}" di _n(2) in red "{p}{sf:`=upper("Endogenous Stochastic `porc' Frontier Model")' (Model EN)}" di _n(1) in red "{p}{sf:Dependent Variable:} " as text "`lhs'" if("`constant'"!="noconstant") di _n(1) in red "{p}{sf:Frontier Variable(s):} " as text "Constant `frontier'" else di _n(1) in red "{p}{sf:Frontier Variable(s):} " as text "`frontier'" if (`unocons'==0) di _n(1) in red "{p}{sf:U Variable(s):} " as text "Constant `urhs'" else di _n(1) in red "{p}{sf:U Variable(s):} " as text "`urhs'" di _n(1) in red "{p}{sf:W Variable(s):} " as text "Constant `whet'" di _n(1) in red "{p}{sf:Endogenous Variable(s):} " as text "`endogenous'" di _n(1) in red "{p}{sf:Excluded Instrument(s):} " as text "`instruments'" di _n(1) in red "{p}{sf:Exogenous Variable(s):} " as text "`allexogenous'{p_end}" _n(2) } if("`delve'"!="") di _n(1) in red "Delving into the problem..." if("`initial'"=="" & "`delve'"!="") { forvalues j = 1/$p { capture regress `=word("`endogenous'", `j')' `allexogenous' `wgt' if `touse' tempvar `=word("`endogenous'", `j')'_res capture noisily predict ``=word("`endogenous'", `j')'_res', res tempname B`=`j'+1' capture matrix `B`=`j'+1''=e(b) } local wc1 = wordcount("`frontier'") local f_res = "" forvalues j = 1/`wc1' { capture confirm variable ``=word("`frontier'", `j')'_res' if !_rc local f_res = "`f_res'``=word("`frontier'", `j')'_res' " } local wc2 = wordcount("`urhs'") local u_res = "" forvalues j = 1/`wc2' { capture confirm variable ``=word("`urhs'", `j')'_res' if !_rc local u_res = "`u_res'``=word("`urhs'", `j')'_res' " } local wc3 = wordcount("`whet'") local wc4 = wordcount("`allexogenous'") local preal = $p tempname B1 if ("`uhet'"=="`urhs'") { if scalar(prod)==1 capture frontier `lhs' `frontier' `f_res' `wgt' if `touse', u(`uhet' `u_res') v(`whet') `constant' tech(bfgs dfp bhhh ) iter(50) else capture frontier `lhs' `frontier' `f_res' `wgt' if `touse', cost u(`uhet' `u_res') v(`whet') `constant' tech(bfgs dfp bhhh ) iter(50) matrix `B1'=e(b) local ll = e(ll) capture sfkk `lhs' `frontier' `f_res' `wgt' if `touse', `porc' u(`uhet' `u_res') w(`whet') `constant' tech(bfgs dfp ) iter(50) nomess if `ll'