/* Program to plot observed proportion of D for groupings of a */ /* continuous X variable (STB-30: sg50) */ /* 02/27/95 JMG (continuous y added, 01/04/96) */ /* Form:emptrend y x,[groups(#), round(#), or integer] plot([mean,prop,log]) */ /* Options Required: groups, round, or integer (only 1) */ /* Options Allowed: plot, xlabel, ylabel, titles */ program define lintrend version 3.1 #delimit ; local options "Groups(int 0) Round(real 0) Integer Plot(string) Title(string) *" ; #delimit cr local varlist "req ex min(2) max(2)" local if "opt" parse "`*'" parse "`varlist'", parse(" ") local choice=0 if `groups'>0 {local choice=`choice'+1} if `round'>0 {local choice=`choice'+1} if "`integer'"=="integer" {local choice=`choice'+1} if `choice'==0 { disp " " #delimit ; disp in red "You must chose one:" in yellow " groups(#)," " round(#), or integer"; #delimit cr exit } if `choice'>1 { disp " " #delimit ; disp in red "You must chose only one:" in yellow " groups(#)," " round(#), or integer"; #delimit cr exit } preserve capture keep `if' keep `varlist' quietly drop if `2'==. sort `1' quietly count if `1'[_n-1]~=`1' & `1'~=. if _result(1)==2 {local ytype=1} if _result(1)>2 {local ytype=2} if `ytype'==1 & "`plot'"=="mean" { disp " " #delimit ; disp in red "plot() only can be" in yellow " prop, log, or both" in red " for a binary Y" ; #delimit cr exit } if `ytype'==2 & ("`plot'"=="prop" | "`plot'"=="log" | "`plot'"=="both") { disp " " #delimit ; disp in red "plot() only can be" in yellow " mean" in red " for a continuous Y" ; #delimit cr exit } local varlblx : variable label `2' local vallblx : value label `2' sort `2' * If groups chosen, divide x into categories of equal size if `groups'>0 { quietly gen numgrps=group(`groups') quietly egen max=max(`2'), by(numgrps) quietly replace max=max[_n-1] if `2'==`2'[_n-1] #delimit ; quietly collapse `2' `1', by(max) min(min .) mean(mean .) sum(. y) count(total .) ; #delimit cr quietly gen _group=mean label var _group "Mean of `2' categories" } * If round chosen, round x to nearest specified value if `round'>0 { quietly gen _group=round(`2',`round') #delimit ; quietly collapse `1' `2', by(_group) sum(y .) count(total .) max(. max) min(. min); #delimit cr label var _group "`2' rounded to nearest `round'" } * If integer chosen, treat categories of x as original integers if "`integer'"=="integer" { quietly gen _group=`2' collapse `1', by(_group) sum(y) count(total) label var _group "Categorized by values of `2'" } * Calculate means, proportions, and log odds by groups of x quietly gen meany=y/total if `ytype'==1 {quietly gen logodds=ln(meany/(1-meany)) if y>0} if "`plot'"=="log" | "`plot'"=="both" { quietly reg logodds _group quietly predict hat } if "`plot'"=="mean" { quietly reg meany _group quietly predict hat } if `ytype'==1 { label var meany "Proportion of `1'" label var logodds "Log odds of `1'" } if `ytype'==2 {label var meany "Category Mean of `1'"} * Set up formats for output quietly compress format y %5.0f format total %7.0f if `groups'>0 { if _n==1 {local range=abs(max-min)} if `range'>=1000000 {format _group % 8.2e} else if `range'>=1 {format _group %10.1f} else if `range'>=.1 {format _group %10.2f} else if `range'>=.01 {format _group %10.3f} else if `range'>=.001 {format _group %10.4f} } if `ytype'==1 { format logodds %7.2f format meany %6.2f } if `ytype'==2 { egen miny=min(meany) if _n==1 {local ymin=miny} if `ymin'>=10000000 {format meany %8.2e} else if `ymin'>=1 {format meany %10.2f} else if `ymin'>=.1 {format meany %10.3f} else if `ymin'>=.01 {format meany %10.4f} else if `ymin'>=.001 {format meany %10.5f} else if `ymin'>=.0001 {format meany %10.6f} } * Graph results if ("`plot'"=="prop" | "`plot'"=="both") & `ytype'==1 { if "`title'"=="" { local title " Assessing Linearity Assumption -- Proportions" } graph meany _group, ti("`title'") `options' if "`plot'"=="both" { more } } if ("`plot'"=="log" | "`plot'"=="both") & `ytype'==1 { if "`title'"=="" | "`plot'"=="both" { local title " Assessing Linearity Assumption -- Log Odds" } graph logodds hat _group, c(.l) s(Oi) ti("`title'") `options' } if "`plot'"=="mean" & `ytype'==2 { if "`title'"=="" { local title " Assessing Linearity Assumption -- Group Means" } graph meany hat _group, c(.l) s(Oi) ti("`title'") `options' } * List results sort _group display " " display " " rename _group `2' rename meany `1' rename y d if `ytype'==1 /* outcome is binary */ { #delimit ; display "The proportion and log odds of" in green " `1' " in yellow "by categories of" in green " `2'" ; display " "; #delimit cr if `groups'>0 { display in blue " (Note: `groups' `2' categories of equal sample size;" display in blue " Uses mean `2' value for each category)" list `2' min max d total `1' logodds, nod noob } if `round'>0 { display in blue " (Note: `2' in categories rounded to nearest `round')" list `2' min max d total `1' logodds, nod noob } if "`integer'"=="integer" { display in blue " (Note: `2' in categories using original values)" label val `2' `vallblx' list `2' d total `1' logodds, nod noob } } if `ytype'==2 /* outcome is continuous */ { #delimit ; display "The mean of" in green " `1' " in yellow "by categories of" in green " `2' " ; display " "; #delimit cr if `groups'>0 { display in blue " (Note: `groups' `2' categories of equal sample size;" display in blue " Uses mean `2' value for each category)" list `2' min max total `1', nod noob } if `round'>0 { display in blue " (Note: `2' in categories rounded to nearest `round')" list `2' min max total `1', nod noob } if "`integer'"=="integer" { display in blue " (Note: `2' in categories using original values)" label val `2' `vallblx' list `2' total `1', nod noob } } end