*! version 1.0 7/21/94 program define conhull version 3.1 local varlist "req ex min(2) max(2)" local options /* */ "`options' Hull(int 500) Group(str) noGRAph Coname(str)" parse "`*'" parse"`varlist'", parse(" ") local size "`hull'" if ("`group'"=="") { tempvar all mac def _group `all' quietly gen byte `group'=1 } if ("`coname'"=="") { mac def _coname "conhull" } tempvar touse mark `touse' markout `touse' `varlist' `group' tempvar fs ls quietly summarize `group' local fs=_result(5) local ls=_result(6) quietly gen int `coname'=. tempvar marker fmk theta dx dy test slope quietly gen byte `marker'=1 quietly gen byte `fmk'=`marker' quietly gen `theta'=-1e37 quietly gen `dx'=. quietly gen `dy'=. quietly gen `test'=. quietly gen `slope'=. quietly while `fs'<=`ls' { tempvar touseb mark `touseb' if `group'==`fs' & `touse'==1 replace `touseb'=. if `touseb'==1 sort `touseb' tab `touseb' local i=(_result(1)+1) local io=`i' local chull=1 while `i'<=_N & `chull' <= `size' { sort `2' `1' in `i'/l replace `marker'=2 in `i' expand `marker' replace `marker'=0 in `i' replace `fmk'=`marker' in `i'/l local j=`i'+1 replace `theta'=-1e37 in `i' while `marker'[`i'] !=2 { replace `marker'=0 in `i' replace `fmk'=0 in `i' replace `dx'=`1'-`1'[`i'] in `j'/l replace `dy'=`2'-`2'[`i'] in `j'/l replace `theta'=0 in `j'/l replace `theta'=. if `dx'==0 & `dy'==0 in `j'/l replace `fmk'=0 if `dx'==0 & `dy'==0 in `j'/l replace `theta'=`dy' /(abs(`dx') + abs(`dy')) if `dy'!=0 in `j'/l replace `theta'=2-`theta' if `dx'<0 in `j'/l replace `theta'=4+`theta' if `dx'>=0 & `dy'<0 in `j'/l replace `theta'=`theta'-2 in `j'/l replace `theta'=. if `theta'<`theta'[`i'] in `j'/l sort `theta' in `j'/l local i=`j' local j=`j'+1 } local i=`io' local j=`i'+1 while `marker'[`i']!=2{ replace `slope'=(`1'[`i']-`1'[`j']) / (`2'[`i']-`2'[`j']) in `j'/l replace `test'=(`1'[`i']-`1') / (`2'[`i']-`2') in `j'/l replace `fmk'=0 if `test'==`slope' in `j'/l local i=`j' local j=`j'+1 } if "`graph'"=="" { tempvar two gen `two'=`2' if `marker'!=1 & _n>=`io' label var `two' "hull point" graph `2' `two' `1',c(.l) s([`group'][`group']) title("Group `fs' Hull # `chull'") drop `two' } drop if `marker'==2 replace `coname'=`chull' if `fmk'==0 & _n>= `io' count if `coname'==`chull' in `io'/l local i=`io'+_result(1) sort `coname' in `io'/l local chull=`chull'+1 local io=`i' } local fs=`fs'+1 drop `touseb' } if ("`group'"=="all") { quietly drop `group' } di "The hull number for each point is saved as " in green "`coname'" end