*!	Version 1.3 8Dec2017 of the companion help file for -flexelasticity.sthlp-
*	Executes the examples run by clicking inside -flexelasticity.sthlp- Version 1.0 5Jul2017

*	Calling program
program flexelasticity_example, rclass
	version 11.1
    preserve
		set more off
		capture prog drop flexelasticity

        if "`c(os)'"~="MacOSX" {
                qui set autotabgraphs on
        }
		args extorun

		`extorun'

	restore
end

*	Utility programs for example files
program Msg
	di as txt
	di as txt "-> " as inp `"`0'"'
end

program Xeq
	di as txt
	di as txt `"-> "' as inp _asis `"`0'"'
	`0'
end

program define NewFile
	args fn
	capture confirm new file `fn'
	if _rc {
		di as txt "{p 0 2 2}"
		di "Example cannot be run because you already have a"
		di "file named {res:`fn'}"
		di "{p_end}"
		error 602
	}
end

program define NewName
	args fn
	capture graph rename `fn' `fn'
	if _rc==0 {
		di as txt "{p 0 2 2}"
		di "Example cannot be run because you already have a"
		di "graph in memory named {res:`fn'}."
		di "Either drop this graph or simply click {stata graph drop _all}."
		di "{p_end}"
		di as err _n "graph `fn' already exists"
		exit 110
	}
end

program Meq  //  Version of Xeq which displays macros
	syntax,   ///
		cmd(string) [ ///
		part1(string) global1(string) ///
		local1(string) ///
		part2(string) local2(string) global2(string) ///
		part3(string) local3(string) global3(string) ///
		part4(string) local4(string) global4(string) ///
		part5(string) local5(string) global5(string) ///
		part6(string) local6(string) global6(string) ///
		part7(string) local7(string) global7(string) ///
		part8(string) local8(string) global8(string) ///
		part9(string) local9(string) global9(string) * ]

	local 0 , `options'

	syntax [, ///
		part10(string) local10(string) global10(string) ///
		part11(string) local11(string) global11(string) ///
		part12(string) local12(string) global12(string) ///
		part13(string) local13(string) global13(string) ///
		part14(string) local14(string) global14(string) ///
		part15(string) local15(string) global15(string) ]

	di as txt
	di as txt `"-> "'  _c
	foreach i of numlist 1/15 {
		if `"`part`i''"'~="" {
			di as inp _asis "`part`i''"  _c
		}
		if `"`global`i''"'~="" {
			di as inp _asis _char(36) "`global`i''" _c
		}
		if `"`local`i''"'~="" {
			di as inp _asis _char(96) "`local`i''" _char(39) _c
		}
		if `"`part`i''"'=="" & `"`local`i''"'=="" & `"`global`i''"'=="" {
			di
			continue, break
		}
	}
	`cmd'
end

prog define setup

*	Set up the problem by importing the data and saving it in a -snapshot- storage location
	Msg *	Setup: *	Setup: Import data and restrict sample to the first 123 observations
	Xeq import delimited using "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF4-4.txt", delimiters(" ", collapse) varn(2) clear

	Xeq drop v11
	Xeq keep if _n < 124
	Xeq gen plbypf = pl/pf
	Xeq lab var plbypf "= pl/pf"

	Xeq gen pkbypf = pk/pf
	Xeq lab var pkbypf "= pk/pf"

	Xeq gen costbypf = 1000000*cost/pf
	Xeq lab var costbypf "= 1000000*cost/pf"

	Msg * Apply the natural log transformation and create macros containing factor variable expressions

	Xeq flexmake costbypf, outputs(q) shares(sk sl) prices(pkbypf plbypf) ln half replace innames(k l) globals
	Xeq return list

	Xeq describe
	Xeq summarize

	//	Need not inform user of this snapshot activity...
	qui snapshot erase _all  //  Assures the snapshot will be #1
	qui snapshot save, label("flexelasticity example data without e(sample)")

end    /* End of program -setup- */


prog define defcon

	Xeq constraint 1 _b[ln_costbypf:c.ln_pkbypf] = _b[sk:_cons]
	Xeq constraint 2 _b[ln_costbypf:c.ln_plbypf] = _b[sl:_cons]
	Xeq constraint 3 _b[ln_costbypf:c.ln_pkbypf#c.ln_pkbypf#c.__half] = _b[sk:ln_pkbypf]
	Xeq constraint 4 _b[ln_costbypf:c.ln_plbypf#c.ln_plbypf#c.__half] = _b[sl:ln_plbypf]
	Xeq constraint 5 _b[ln_costbypf:c.ln_pkbypf#c.ln_plbypf] = _b[sl:ln_pkbypf]
	Xeq constraint 6 _b[ln_costbypf:c.ln_pkbypf#c.ln_plbypf] = _b[sk:ln_plbypf]
	Xeq constraint 7 _b[ln_costbypf:c.ln_q#c.ln_pkbypf] = _b[sk:ln_q]
	Xeq constraint 8 _b[ln_costbypf:c.ln_q#c.ln_plbypf] = _b[sl:ln_q]
	Xeq constraint dir

end    /* End of program -defcon- */

program define estsureg
	capture snapshot restore 1
		if _rc~=0  {
			di _n as err "User must start at the top of the example by"  ///
				_n "{stata flexelasticity_example setup:first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints}"  ///
				_n as err "before estimating the cost function system."
			exit 111
		}

	Msg * Now estimate the translog system
	Meq ,cmd(`"sureg ($depvar $ownprices $ownprices2 $crossprices $ownoutputs $ownoutputs2 $crossoutputs $outputsxprices ) (sk $ownprices $ownoutputs ) (sl $ownprices $ownoutputs ), constraints(1/8)"')  ///
		part1(`"sureg ("') global1(`"depvar"')  ///
		part2(" ") global2("ownprices")  ///
		part3(" ") global3("ownprices2")  ///
		part4(" ") global4("crossprices")  ///
		part5(" ") global5("ownoutputs")  ///
		part6(" ") global6("ownoutputs2")  ///
		part7(" ") global7("crossoutputs")  ///
		part8(" ") global8("outputsxprices") ///
		part9(`") (sk "') global9(`"ownprices"')  ///
		part10(" ") global10(`"ownoutputs"')  ///
		part11(`") (sl "') global11(`"ownprices"')  ///
		part12(" ") global12(`"ownoutputs"')  ///
		part13(`"), constraints(1/8)"')

	Meq, cmd(`"global cmdline `e(cmdline)'"')  ///
		part1(`"global cmdline "') local1(`"e(cmdline)"')

	Xeq predict ln_costbypf_hat, xb

	//	Need not inform user of this snapshot activity...
	qui snapshot save, label("flexelasticity example data and e(sample)")

end  /* End of program -estsureg- */

prog define tcwrtq
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg *	Elasticity of TC w.r.t. output (scale elasticity)

	Xeq flexelasticity ln_q

end

prog define tcwrtqat
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg *	Elasticity of TC w.r.t. output at various output levels
	Xeq flexelasticity ln_q , atspecs(at((p10) ln_q) at((p25) ln_q) at((p50) ln_q) at((p75) ln_q) at((p90) ln_q))

	Msg *	Stata's {help marginsplot} command shows economies of scale at low output levels
	Xeq marginsplot, ytitle("Elasticity of total cost" "w.r.t. output") xtitle(Centiles of output)

end

program define tcwrtk
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "Must {stata flexelasticity_example setup:clicking to import the data, run flexmake and estimate the system} before running any flexelasticity example from this help file."
			exit 111
		}

	Msg * Elasticity of TC w.r.t. the price of capital

	Xeq flexelasticity ln_pkbypf

end

program define tcwrtkat
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg * Expression for the partial derivative of total cost with respect to the price of capital
	Meq, cmd(`"firstpartial ln_pkbypf , chain(ln) cmdline($cmdline)"')  ///
		part1(`"firstpartial ln_pkbypf , chain(ln) cmdline("') global1("cmdline") part2(`")"')

	Meq, cmd(`"global fpk `r(margexpression)'"')  ///
		part1(`"global fpk "') local1(r(margexpression))

	Msg * Slope of TC w.r.t. the price of capital decreases, in accord with theory
	Meq, cmd(`"margins, expression($fpk) at((p25) ln_pkbypf) at((p50) ln_pkbypf) at((p75) ln_pkbypf) at((p90) ln_pkbypf)"')  ///
		part1(`"margins, expression("') global1("fpk")  ///
		part2(`") at((p25) ln_pkbypf) at((p50) ln_pkbypf) at((p75) ln_pkbypf) at((p90) ln_pkbypf)"')

	Msg * Slope of TC w.r.t. the price of capital decreases, in accord with theory
	Xeq marginsplot, ytitle("Slope of total cost" "w.r.t. the price of capital") xtitle(Centiles of pk)

end

*	These two examples on the elasticity of cost wrt wage rate are omitted from the help file as redundant
program define tcwrtl
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg * Elasticity of TC w.r.t. the price of labor

	Xeq flexelasticity ln_plbypf

end

program define tcwrtlat
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg * Expression for the partial derivative of total cost with respect to the price of labor
	Meq, cmd(`"firstpartial ln_plbypf , chain(ln) cmdline($cmdline)"')  ///
		part1(`"firstpartial ln_plbypf , chain(ln) cmdline("') global1("cmdline") part2(`")"')

	Meq, cmd(`"global fpl `r(margexpression)'"')  ///
		part1(`"global fpl "') local1(r(margexpression))

	Msg * Slope of TC w.r.t. the price of labor decreases, in accord with theory
	Meq, cmd(`"margins, expression($fpl) at((p25) ln_plbypf) at((p50) ln_plbypf) at((p75) ln_plbypf) at((p90) ln_plbypf)"')  ///
		part1(`"margins, expression("') global1("fpl")  ///
		part2(`") at((p25) ln_plbypf) at((p50) ln_plbypf) at((p75) ln_plbypf) at((p90) ln_plbypf)"')

	Msg * Slope of TC w.r.t. the price of labor decreases, in accord with theory
	Xeq marginsplot, ytitle("Slope of total cost" "w.r.t. the price of labor") xtitle(Centiles of pl)

end

program define aeskl
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg * Allen partial elasticity of substitution between capital and labor

	Xeq flexelasticity ln_pkbypf ln_plbypf, shares(sk sl)

end

program define aesklact
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg * If we instead use actual factor shares (as Christensen and Greene might have done):

	Xeq flexelasticity ln_pkbypf ln_plbypf, shares(sk sl) actual

end

program define ekk
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg * Own elasticity of demand of capital

	Xeq flexelasticity ln_pkbypf ln_pkbypf, shares(sk sk)

end

program define ekkact
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "User must start at the top of the example by {stata flexelasticity_example setup: first clicking to import the data and run -flexmake-}"  ///
				_n as err "and then {stata flexelasticity_example defcon:clicking to define the constraints} and then {stata flexelasticity_example estsureg:clicking to estimate the system}"  ///
				_n as err "before running any flexelasticity example from this help file."
			exit 111
		}

	Msg * If we instead use actual factor shares (as Christensen and Greene might have done):

	Xeq flexelasticity ln_pkbypf ln_pkbypf, shares(sk sk) actual

end

program define rtrnlst
	capture snapshot restore 2
		if _rc~=0  {
			di _n as err "Must {stata flexelasticity_example setup:clicking to import the data, run flexmake and estimate the system} before running any flexelasticity example from this help file."
			exit 111
		}

	// Execute the program again quietly in order to refill the r() space
	qui flexelasticity ln_pkbypf ln_pkbypf, shares(sk sk) actual

	Msg *  Information stored by flexelasticity includes the usual results returned
	Msg *  by {help margins##results:margins} plus the first estimated elasticity, its standard error and its p-value.
	Xeq return list

end
*	Version 1.0 5Jul2017	Uses -snapshot- in order to protect user's data.
*	Version 1.2 11Aug2017	Exploit flexelasticity's version 1.2 option to use e(cmdline) instead of cmdline() option
*	Version 1.3 8Dec2017	Fix above subroutine -rtrnlst- by refilling the r() space