*!Version 1.0 19 Aug2016 by Mead Over, Center for Global Devleopment
*!	Companion program to be executed by the help file: firstpartial.sthlp

program define firstpartial_example_translog
	version 11.1

	capture which flexmake
	if _rc~=0 {
		di as err "Running this example requires that the user-written program -{search flexmake}- already be installed."  ///
		_n "User can install this program by clicking on the above link and installing the flexmake program"  ///
		_n "from CGD's Stata library at " as res "net from http://www.cgdev.org/doc/stata."
		exit 198
	}

	set more off
	`0'

	capture prog drop firstpartial
end

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

program Ms2
*	Use this version for messages containing macros
*	Use _char(36) for $ (the global macro delimiter)
*	Use _char(96) for ` (the backtick)
*	Use _char(39) for ' (the foretick or single quote)
	di as txt
	local intro `"as txt "-> ""'
	while `"`0'"'~="" {
		local nextchar = strpos(`"`0'"', "_char(")
		if `nextchar' >0 {
			local stuff = substr(`"`0'"',1,`nextchar'-1)
			local macdelim = substr(`"`0'"',`nextchar',9)
			di `intro' as inp `"`space'`stuff'"' `macdelim' _c
			local 0 = substr(`"`0'"',`nextchar'+9,.)
			if "`macdelim'"=="_char(39)" {
				local space " "
			}
			else {
				local space
			}
			local intro
		}
		else {
			di `intro' as inp `"`0'"'
			local 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 unwind_translog

	*	Load the data from Bill Greene's website
	capture import delimited using "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF4-4.txt", delimiters(" ", collapse) varn(2) clear
	if _rc~=0 {
		Msg import delimited using "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF4-4.txt", delimiters(" ", collapse) varn(2) clear
		di as err "no; data in memory would be lost"
		di as err "First {stata clear:clear} your memory before running these examples"
		exit
	}

	Xeq set more off
	Xeq import delimited using "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF4-4.txt", delimiters(" ", collapse) varn(2) clear

	Xeq drop v11
	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 * make log transformation
	Xeq flexmake costbypf, outputs(q) prices(pkbypf plbypf) ln half replace globals nomake

	Xeq return list

	Msg *	Here are the cross-equation constraints
	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]

	Ms2 sureg (_char(36)depvar _char(36)ownprices _char(36)ownprices2 _char(36)crossprices _char(36)ownoutputs _char(36)ownoutputs2 _char(36)crossoutputs _char(36)outputsxprices ) (sk _char(36)ownprices _char(36)ownoutputs ) (sl _char(36)ownprices _char(36)ownoutputs ), constraints(1/8)
	sureg ($depvar $ownprices $ownprices2 $crossprices $ownoutputs $ownoutputs2 $crossoutputs $outputsxprices ) (sk $ownprices $ownoutputs ) (sl $ownprices $ownoutputs ), constraints(1/8)

	Xeq est store Translog

	Msg *	First partial deriviative w.r.t. output, which is the marginal cost of output at the mean
	Ms2 firstpartial ln_q , cmdline(_char(96)e(cmdline)_char(39)) chain(ln)
	firstpartial ln_q , cmdline(`e(cmdline)') chain(ln)

	Ms2 global margexpression _char(96)r(margexpression)_char(39)
	global margexpression `r(margexpression)'

	Ms2 global genexpression _char(96)r(genexpression)_char(39)
	global genexpression `r(genexpression)'

	Msg *	The program returns in the local macro -genexpression- the expression for the marginal cost
	Msg *	evaluated at every point in the sample. Using this expression requires that
	Msg *	the user create the variable -depvar-_hat using the predict command.
	Msg *	Then -gen- can create the observation-by-observation marginal costs.
	Xeq predict ln_costbypf_hat, xb

	Ms2 gen d_TC_d_q = _char(36){genexpression}
	gen d_TC_d_q = ${genexpression}
	Xeq 	lab var d_TC_d_q "Marginal cost of output"

	Msg *	So one option for computing a summary value for the marginal cost
	Msg *	is simply to compute the mean of the generated variable.
	Xeq sum d_TC_d_q

	Msg *	Using {cmd:firstpartial}'s local macro -r(margexpression)- (which was stored
	Msg *	above as the global macro with the same name), Stata's -margins- program
	Msg *	computes not only the the marginal cost but also its standard forecast error.
	Msg *	Without other options, margins also evaluates the marginal cost by observation.
	Msg *	So its estimate matches the mean of the estimate from the generate command.

	Ms2 margins , expression(_char(36){margexpression})
	margins , expression(${margexpression})

	Msg *	Furthermore, the margins command will easily compute the predicted marginal cost
	Msg *	of output at other values of output. For this purpose, its best to hold other variables
	Msg *	at fixed values, such as with the "atmeans" option
	Xeq sum ln_q, det
	Ms2 margins , expression(_char(36){margexpression}) atmeans at(ln_q=(_char(96)r(mean)_char(39)   _char(96)r(p10)_char(39)   _char(96)r(p25)_char(39)   _char(96)r(p50)_char(39)   _char(96)r(p75)_char(39)   _char(96)r(p90)_char(39)))
	margins , expression(${margexpression}) atmeans at(ln_q=(`r(mean)' `r(p10)' `r(p25)' `r(p50)' `r(p75)' `r(p90)'))

	Msg *	The command marginsplot gives the marginal cost curve as a function of ln_q
	Xeq marginsplot, xlabel(,angle(45) format(%9.3f)) ytitle("Marginal cost of output") name(translog, replace)

end