*! version 0.1 ZWANG Nov 1998, Menzies School of Health Research
*! version 0.2 M.S. Pearce Nov 1998, Child Health, University of Newcastle
*  upon Tyne
program define colelms
version 5.0
local varlist "req ex min(1) max(1)"
#delimit ;
local options "AGEgroup(string) SEX(string) df(int 4)
NOSmooth CENtile(string) save(string) ";
#delimit cr
parse "`*'"
parse "`varlist'", parse(" ")
tempvar xlog xrec mg mh ma sg sa sh A B sd sdlog sdr
if "`centile'"=="" {local centile "10 25 50 75 90"}
gen `xlog'=ln(`1')
gen `xrec'=1/`1'
local GM "`1'"
if "`save'"==""{local save "LMStemp"}
if "`sex'"!=""{
 # delimit ;
 collapse (mean) "`1'" `xlog' `xrec' (sd) sd="`1'" `sdlog'=`xlog'
 `sdr'=`xrec' (count) num=`1', by(`sex' `agegroup');
 # delimit cr
}
if "`sex'"==""{
 # delimit ;
 collapse (mean) "`1'" `xlog' `xrec' (sd) sd="`1'" `sdlog'=`xlog'
 `sdr'=`xrec' (count) num=`1', by(`agegroup');
 # delimit cr
}
gen `mg'=exp(`xlog')
gen `mh'=1/`xrec'
gen `ma'=`1'
gen `sg'=`sdlog'
gen `sa' =sd/`mg'
gen `sh'=`sdr'*`mg'
gen `A'=ln(`sa' /`sh')
gen `B'=ln((`sa' *`sh')/(`sg'^2))
qui {
 gen L=-`A'/(2*`B')
 gen S=`sg'*exp(`A'*L/4)
 gen M=`mg'+(`ma'-`mh')*L/2+(`ma'-2*`mg'+`mg')*(L^2)/2
 if "`nosmooth'"==""{
 if "`sex'"==""{
 gam L `agegroup', df(`df') big
 gen LS=GAM
 gam M `agegroup', df(`df') big
 gen MS=GAM
 gam S `agegroup', df(`df') big
 gen SS=GAM
 }
 if "`sex'"!=""{
quietly summ `sex'
local one=_result(5)
local two=_result(6)
gam L `agegroup' if `sex'==`one', df(`df') big
 gen LS=GAM if `sex'==`one'
 gam L `agegroup' if `sex'==`two', df(`df') big
 replace LS=GAM if `sex'==`two'
 gam M `agegroup' if `sex'==`one', df(`df') big
 gen MS=GAM if `sex'==`one'
 gam M `agegroup' if `sex'==`two', df(`df') big
 replace MS=GAM if `sex'==`two'
 gam S `agegroup' if `sex'==`one', df(`df') big
 gen SS=GAM if `sex'==`one'
 gam S `agegroup' if `sex'==`two', df(`df') big
 replace SS=GAM if `sex'==`two'
 }
 lab var LS "L smoothed"
 lab var MS "M smoothed"
 lab var SS "S smoothed"
 drop s_ r_ e_ GAM
 parse "`centile'", parse(" ")
 while "`1'" !="" {
 local tcent = `1'/100
 local Z`1' = invnorm(`tcent')
 gen C`1' = MS*((1+LS*SS*`Z`1'')^(1/LS))
 mac shift
 }
 }
}
end