*! Version 1.4 4Jan2017

program qscope_example2
    preserve
		set more off
		capture prog drop qscope

		di _n as inp `". sysuse qscope_costdata, clear"'
		sysuse qscope_costdata, clear

		di _n as inp ". *** Set the missing price variable to equal Other expenditure (q_oth=1)***"
		di _n as inp ". gen p_Oth = costOth"
		gen p_Oth = costOth
		di _n as inp `". lab var p_Oth "Arbitrary price for the -Other- input""'
		lab var p_Oth "Arbitrary price for the -Other- input"

		di _n as inp ". *** transform data ***"
		di _n as inp ". flexmake , outputs(artpy hivpy) prices(P_DP P_IP ptest P_OI pnut p_Oth) incosts(cost*) innames(DP IP test OI nut Oth) ln half global"
		flexmake , outputs(artpy hivpy) prices(P_DP P_IP ptest P_OI pnut p_Oth) incosts(cost*) innames(DP IP test OI nut Oth) ln half global

		di _n as inp ". ***	Specify the regressors ***"
		di _n as inp ". global outputvariables ${ownoutputs} ${ownoutputs2} ${crossoutputs} "
		global outputvariables ${ownoutputs} ${ownoutputs2} ${crossoutputs}

		di _n as inp ". global pricevariables  ${ownprices} ${ownprices2} ${crossprices} ${outputsxprices} "
		global pricevariables  ${ownprices} ${ownprices2} ${crossprices} ${outputsxprices}

		di _n as inp ". global othervars i.nation "
		global othervars i.nation

		di _n as inp ". *** Estimates of QSCOPE when cost function is estimated by SUR ***"
		di _n as inp ". *	Specify the command line"
		di _n as inp `". global cmdline "${depvar}: ${depvar} ${outputvariables} ${pricevariables} ${othervars}" "'
		global cmdline "${depvar}: ${depvar} ${outputvariables} ${pricevariables} ${othervars}"

		di _n as inp ". *** constraints ***"
		di _n as inp ". constraint 1  _b[ln_tc:c.ln_P_DP] = _b[sh_DP:_cons]	"
		constraint 1  _b[ln_tc:c.ln_P_DP] = _b[sh_DP:_cons]
		di _n as inp ". constraint 2  _b[ln_tc:c.ln_P_IP] = _b[sh_IP:_cons]	"
		constraint 2  _b[ln_tc:c.ln_P_IP] = _b[sh_IP:_cons]
		di _n as inp ". constraint 3  _b[ln_tc:c.ln_ptest] = _b[sh_test:_cons]	"
		constraint 3  _b[ln_tc:c.ln_ptest] = _b[sh_test:_cons]
		di _n as inp ". constraint 4  _b[ln_tc:c.ln_P_OI] = _b[sh_OI:_cons]	"
		constraint 4  _b[ln_tc:c.ln_P_OI] = _b[sh_OI:_cons]
		di _n as inp ". constraint 5  _b[ln_tc:c.ln_pnut] = _b[sh_nut:_cons]	"
		constraint 5  _b[ln_tc:c.ln_pnut] = _b[sh_nut:_cons]

		di _n as inp ". constraint 6  _b[ln_tc:c.ln_P_DP#c.ln_P_DP#c.__half] = _b[sh_DP:ln_P_DP]"
		constraint 6  _b[ln_tc:c.ln_P_DP#c.ln_P_DP#c.__half] = _b[sh_DP:ln_P_DP]
		di _n as inp ". constraint 7  _b[ln_tc:c.ln_P_IP#c.ln_P_IP#c.__half] = _b[sh_IP:ln_P_IP]	"
		constraint 7  _b[ln_tc:c.ln_P_IP#c.ln_P_IP#c.__half] = _b[sh_IP:ln_P_IP]
		di _n as inp ". constraint 8  _b[ln_tc:c.ln_ptest#c.ln_ptest#c.__half] = _b[sh_test:ln_ptest]"
		constraint 8  _b[ln_tc:c.ln_ptest#c.ln_ptest#c.__half] = _b[sh_test:ln_ptest]
		di _n as inp ". constraint 9  _b[ln_tc:c.ln_P_OI#c.ln_P_OI#c.__half] = _b[sh_OI:ln_P_OI]	"
		constraint 9  _b[ln_tc:c.ln_P_OI#c.ln_P_OI#c.__half] = _b[sh_OI:ln_P_OI]
		di _n as inp ". constraint 10 _b[ln_tc:c.ln_pnut#c.ln_pnut#c.__half] = _b[sh_nut:ln_pnut]	"
		constraint 10 _b[ln_tc:c.ln_pnut#c.ln_pnut#c.__half] = _b[sh_nut:ln_pnut]

		di _n as inp ". constraint 11 _b[ln_tc:c.ln_P_DP#c.ln_P_IP] = _b[sh_DP:ln_P_IP]"
		constraint 11 _b[ln_tc:c.ln_P_DP#c.ln_P_IP] = _b[sh_DP:ln_P_IP]
		di _n as inp ". constraint 12 _b[ln_tc:c.ln_P_DP#c.ln_P_IP] = _b[sh_IP:ln_P_DP]"
		constraint 12 _b[ln_tc:c.ln_P_DP#c.ln_P_IP] = _b[sh_IP:ln_P_DP]
		di _n as inp ". constraint 13 _b[ln_tc:c.ln_P_DP#c.ln_ptest] = _b[sh_DP:ln_ptest]"
		constraint 13 _b[ln_tc:c.ln_P_DP#c.ln_ptest] = _b[sh_DP:ln_ptest]
		di _n as inp ". constraint 14 _b[ln_tc:c.ln_P_DP#c.ln_ptest] = _b[sh_test:ln_P_DP]"
		constraint 14 _b[ln_tc:c.ln_P_DP#c.ln_ptest] = _b[sh_test:ln_P_DP]
		di _n as inp ". constraint 15 _b[ln_tc:c.ln_P_DP#c.ln_P_OI] = _b[sh_DP:ln_P_OI]"
		constraint 15 _b[ln_tc:c.ln_P_DP#c.ln_P_OI] = _b[sh_DP:ln_P_OI]
		di _n as inp ". constraint 16 _b[ln_tc:c.ln_P_DP#c.ln_P_OI] = _b[sh_OI:ln_P_DP]"
		constraint 16 _b[ln_tc:c.ln_P_DP#c.ln_P_OI] = _b[sh_OI:ln_P_DP]
		di _n as inp ". constraint 17 _b[ln_tc:c.ln_P_DP#c.ln_pnut] = _b[sh_DP:ln_pnut]"
		constraint 17 _b[ln_tc:c.ln_P_DP#c.ln_pnut] = _b[sh_DP:ln_pnut]
		di _n as inp ". constraint 18 _b[ln_tc:c.ln_P_DP#c.ln_pnut] = _b[sh_nut:ln_P_DP]"
		constraint 18 _b[ln_tc:c.ln_P_DP#c.ln_pnut] = _b[sh_nut:ln_P_DP]

		di _n as inp ". constraint 19 _b[ln_tc:c.ln_P_IP#c.ln_ptest] = _b[sh_IP:ln_ptest]"
		constraint 19 _b[ln_tc:c.ln_P_IP#c.ln_ptest] = _b[sh_IP:ln_ptest]
		di _n as inp ". constraint 20 _b[ln_tc:c.ln_P_IP#c.ln_ptest] = _b[sh_test:ln_P_IP]"
		constraint 20 _b[ln_tc:c.ln_P_IP#c.ln_ptest] = _b[sh_test:ln_P_IP]
		di _n as inp ". constraint 21 _b[ln_tc:c.ln_P_IP#c.ln_P_OI] = _b[sh_IP:ln_P_OI]"
		constraint 21 _b[ln_tc:c.ln_P_IP#c.ln_P_OI] = _b[sh_IP:ln_P_OI]
		di _n as inp ". constraint 22 _b[ln_tc:c.ln_P_IP#c.ln_P_OI] = _b[sh_OI:ln_P_IP]"
		constraint 22 _b[ln_tc:c.ln_P_IP#c.ln_P_OI] = _b[sh_OI:ln_P_IP]
		di _n as inp ". constraint 23 _b[ln_tc:c.ln_P_IP#c.ln_pnut] = _b[sh_IP:ln_pnut]"
		constraint 23 _b[ln_tc:c.ln_P_IP#c.ln_pnut] = _b[sh_IP:ln_pnut]
		di _n as inp ". constraint 24 _b[ln_tc:c.ln_P_IP#c.ln_pnut] = _b[sh_nut:ln_P_IP]"
		constraint 24 _b[ln_tc:c.ln_P_IP#c.ln_pnut] = _b[sh_nut:ln_P_IP]

		di _n as inp ". constraint 25 _b[ln_tc:c.ln_ptest#c.ln_P_OI] = _b[sh_test:ln_P_OI]"
		constraint 25 _b[ln_tc:c.ln_ptest#c.ln_P_OI] = _b[sh_test:ln_P_OI]
		di _n as inp ". constraint 26 _b[ln_tc:c.ln_ptest#c.ln_P_OI] = _b[sh_OI:ln_ptest]"
		constraint 26 _b[ln_tc:c.ln_ptest#c.ln_P_OI] = _b[sh_OI:ln_ptest]
		di _n as inp ". constraint 27 _b[ln_tc:c.ln_ptest#c.ln_pnut] = _b[sh_test:ln_pnut]"
		constraint 27 _b[ln_tc:c.ln_ptest#c.ln_pnut] = _b[sh_test:ln_pnut]
		di _n as inp ". constraint 28 _b[ln_tc:c.ln_ptest#c.ln_pnut] = _b[sh_nut:ln_ptest]"
		constraint 28 _b[ln_tc:c.ln_ptest#c.ln_pnut] = _b[sh_nut:ln_ptest]

		di _n as inp ". constraint 29 _b[ln_tc:c.ln_P_OI#c.ln_pnut] = _b[sh_OI:ln_pnut]"
		constraint 29 _b[ln_tc:c.ln_P_OI#c.ln_pnut] = _b[sh_OI:ln_pnut]
		di _n as inp ". constraint 30 _b[ln_tc:c.ln_P_OI#c.ln_pnut] = _b[sh_nut:ln_P_OI]"
		constraint 30 _b[ln_tc:c.ln_P_OI#c.ln_pnut] = _b[sh_nut:ln_P_OI]

		di _n as inp ". constraint 31 _b[ln_tc:c.ln_artpy#c.ln_P_DP] = _b[sh_DP:ln_artpy]"
		constraint 31 _b[ln_tc:c.ln_artpy#c.ln_P_DP] = _b[sh_DP:ln_artpy]
		di _n as inp ". constraint 32 _b[ln_tc:c.ln_artpy#c.ln_P_IP] = _b[sh_IP:ln_artpy]"
		constraint 32 _b[ln_tc:c.ln_artpy#c.ln_P_IP] = _b[sh_IP:ln_artpy]
		di _n as inp ". constraint 33 _b[ln_tc:c.ln_artpy#c.ln_ptest] = _b[sh_test:ln_artpy]"
		constraint 33 _b[ln_tc:c.ln_artpy#c.ln_ptest] = _b[sh_test:ln_artpy]
		di _n as inp ". constraint 34 _b[ln_tc:c.ln_artpy#c.ln_P_OI] = _b[sh_OI:ln_artpy]"
		constraint 34 _b[ln_tc:c.ln_artpy#c.ln_P_OI] = _b[sh_OI:ln_artpy]
		di _n as inp ". constraint 35 _b[ln_tc:c.ln_artpy#c.ln_pnut] = _b[sh_nut:ln_artpy]"
		constraint 35 _b[ln_tc:c.ln_artpy#c.ln_pnut] = _b[sh_nut:ln_artpy]

		di _n as inp ". constraint 36 _b[ln_tc:c.ln_hivpy#c.ln_P_DP] = _b[sh_DP:ln_hivpy]"
		constraint 36 _b[ln_tc:c.ln_hivpy#c.ln_P_DP] = _b[sh_DP:ln_hivpy]
		di _n as inp ". constraint 37 _b[ln_tc:c.ln_hivpy#c.ln_P_IP] = _b[sh_IP:ln_hivpy]"
		constraint 37 _b[ln_tc:c.ln_hivpy#c.ln_P_IP] = _b[sh_IP:ln_hivpy]
		di _n as inp ". constraint 38 _b[ln_tc:c.ln_hivpy#c.ln_ptest] = _b[sh_test:ln_hivpy]"
		constraint 38 _b[ln_tc:c.ln_hivpy#c.ln_ptest] = _b[sh_test:ln_hivpy]
		di _n as inp ". constraint 39 _b[ln_tc:c.ln_hivpy#c.ln_P_OI] = _b[sh_OI:ln_hivpy]"
		constraint 39 _b[ln_tc:c.ln_hivpy#c.ln_P_OI] = _b[sh_OI:ln_hivpy]
		di _n as inp ". constraint 40 _b[ln_tc:c.ln_hivpy#c.ln_pnut] = _b[sh_nut:ln_hivpy]"
		constraint 40 _b[ln_tc:c.ln_hivpy#c.ln_pnut] = _b[sh_nut:ln_hivpy]

		di _n as inp ". sureg (${cmdline}) ///"
		di _n as inp ". 	(sh_DP ${ownprices} ${ownoutputs} ${othervars}  ) ///"
		di _n as inp ". 	(sh_IP ${ownprices} ${ownoutputs} ${othervars}  ) ///"
		di _n as inp ". 	(sh_test ${ownprices} ${ownoutputs} ${othervars} ) ///"
		di _n as inp ". 	(sh_OI ${ownprices} ${ownoutputs}  ${othervars} ) ///"
		di _n as inp ". 	(sh_nut ${ownprices} ${ownoutputs} ${othervars} ) ///"
		di _n as inp ". 	, constraints(1/40) isure"
		sureg (${cmdline}) ///
			(sh_DP ${ownprices} ${ownoutputs} ${othervars}  ) ///
			(sh_IP ${ownprices} ${ownoutputs} ${othervars}  ) ///
			(sh_test ${ownprices} ${ownoutputs} ${othervars} ) ///
			(sh_OI ${ownprices} ${ownoutputs}  ${othervars} ) ///
			(sh_nut ${ownprices} ${ownoutputs} ${othervars} ) ///
			, constraints(1/40) isure

		di _n as inp ". *** qscope ***"
		di _n as inp ". qscope, outputs(${outputs}) depvar(${depvar}) epsilon(0.0001 0.001 0.01 0.1) chain(ln)"
		qscope, outputs(${outputs}) depvar(${depvar}) epsilon(0.0001 0.001 0.01 0.1) chain(ln)

		di _n as inp ". qscope, outputs(${outputs}) depvar(${depvar}) epsilon(0.0001 0.001 0.01 0.1) chain(ln) median"
		qscope, outputs(${outputs}) depvar(${depvar}) epsilon(0.0001 0.001 0.01 0.1) chain(ln) median

restore
end
*	Version 1.0 by Shunjie Tu  13Jul2016
*	Version 1.1 Revised by Mead Over, 9Sep2016
*	Version 1.2 31Oct2016 Update calls to flexmake to correspond to its version 2.0
*	Version 1.3 20Dec2016 Set the quantity rather than the price of Other to 1.0
*	Version 1.4 4Jan2017 Estimate qscope for four values of epsilon at once