*! STB-24: sg26.2 program define fpcurve quietly { version 3.1 local varlist "opt new max(1)" local exp "opt nopre" local if "opt pre" local in "opt pre" local options "Coefficients(str) CONstant(real 0) Logpowers(str) noGraph Obs(int 0) Powers(str) Range(str) *" parse "`*'" cap drop `varlist' /* Parse the powers, coefficients, and log powers. */ parse "`powers'", parse(" ,") while "`1'"!="" { if "`1'"!="," { local p "`p' `1'" } mac shift } parse "`coeffic'", parse(" ,") while "`1'"!="" { if "`1'"!="," { local c "`c' `1'" } mac shift } parse "`logpowe'", parse(" ,") while "`1'"!="" { if "`1'"!="," { local l "`l' `1'" } mac shift } local powers : word count `p' if (`powers'==0) { di in re "no powers specified" error 98 } local p0 "." parse "`p'", parse(" ") local i 0 while `i'<`powers' { local i = `i' + 1 local p`i' ``i'' } local coefs : word count `c' parse "`c'", parse(" ") local i 0 while `i'<`powers' { local i = `i' + 1 if `i'<=`coefs' { local c`i' ``i'' } else { local c`i' 1 } } local logs: word count `l' parse "`l'", parse(" ") local i 0 while `i'<`powers' { local j = `i' local i = `i' + 1 if `i'<=`logs' { local l`i' = ``i'' } else if (!`logs') & (`i'>1) & (`p`i''==`p`j'') { local l`i' = 1 + `l`j'' } else { local l`i' 0 } } /* Process input variable/expression/range. */ tempvar touse x y z if "`exp'"!="" { mark `touse' `if' `in' gen double `x' = `exp' if `touse' } else { if !_N { preserve if `obs' { set obs `obs' } else set obs 1000 } gen byte `touse' = 1 if "`range'"=="" { local min 0 local max 1 } else { parse "`range'", parse(" ,") while "`1'"!="" { if "`1'"!="," { local r "`r' `1'" } mac shift } local nr : word count `r' if `nr'!=2 { noi di in re "range must specify min and max" error 98 } parse "`r'", parse(" ") local min `1' local max `2' } gen double `x' = `min' + (`max'-`min')*(_n-1)/(_N-1) local exp "x=[`min',`max']" } cap conf v `exp' if !_rc { local lbl : variable label `exp' if "`lbl'"=="" { local lbl "`exp'" } } else { local lbl "`exp'" } lab var `x' "`lbl'" markout `touse' `x' _crcnuse `touse' local ifin "in $S_3/$S_4" if $S_2 { local ifin "`ifin' if `touse'" } /* Calculate the FP curve. */ gen double `y' = `constan' `ifin' gen double `z' = . local i 0 while `i'<`powers' { local i = `i' + 1 if "`p`i''"=="-0" { replace `z' = 1/log(`x') `ifin' } else if `p`i''==0 { replace `z' = log(`x') `ifin' } else if `p`i''>0 { replace `z' = `x'^(`p`i'') `ifin' } else { replace `z' = 1/`x'^(abs(`p`i'')) `ifin' } if `l`i''>0 { replace `z' = `z' * (log(`x'))^(`l`i'') `ifin' } else if `l`i''<0 { replace `z' = `z'/(log(`x'))^(abs(`l`i'')) `ifin' } replace `y' = `y' + (`c`i'' * `z') `ifin' } /* Label and graph the output. */ local p = trim("`p'") local lbl "`exp', p(`p')" if `logs' { local l = trim("`l'") local lbl "`lbl' l(`l')" } if `coefs' { local c = trim("`c'") local lbl "`lbl' c(`c')" } lab var `y' "`lbl'" } /* end quietly */ if "`graph'"=="" { gr `y' `x', s(.) c(l) sort `options' } if "`varlist'"!="" { rename `y' `varlist' } end