Stata Graphs for Trials: Two Group comparison with confidence intervals (Stata 12.0)

Standard

A) Line graph
alex

Step 1)
Generate a dataset with these variables in long format:
group time levelofoutcome lowerlimit upperlimit

Step 2) Sort time

sort time

Step 3) Draw graph

 line levelofoutcome time if(group==1) , lwidth(thick) ///
 || line lowerlimit time if(group==1) ///
    , lpattern(dash) lwidth(thick) lcolor(blue) ///
 || line upperlimit time if(group==1) ///
    , lpattern(dash) lwidth(thick) lcolor(blue) ///
 || line levelofoutcome time if(group==2) , lwidth(thick) ///
 || line lowerlimit time if(group==2) ///
    , lpattern(dash) lwidth(thick) lcolor(red) ///
 || line upperlimit time if(group==2) ///
    , lpattern(dash) lwidth(thick) lcolor(red) ///
 || scatter levelofoutcome time , mcolor(black) ///
 xlabel( 1 "Baseline" 2 "Postintervention") ///
 xscale( range(0.75 2.25)) ///
 legend( ///
        order(1 2 4 5) ///
        label(1 "Group A") label(2 "95% CI") ///
        label(4 "Group B") label(5 "95% CI") ///
       )

B) Bargraph

Two groups, bargraphs, confidence intervals at baseline/post-intervention

Two groups, bargraphs, confidence intervals at baseline/post-intervention

Step 1) Sort time AND group.

 sort  time group

Step 2) Generate a variable which provides the positions of the bars
in the graph.

 gen barposition = cond(time==1, _n, _n+1)

The “+1” generates the gap between the two sets of bars (time 1/time2).

Step 3) Draw graph over “bar position” variable.
Use [rcap] to draw confidence intervals.
To have different bar colours per group, draw them separately using [if(group=== X)]

 twoway bar  levelofoutcome barposition  if(group==1) ///
	|| bar  levelofoutcome barposition  if(group==2) ///
	|| rcap  lowerlimit upperlimit barposition 		 ///
	,lwidth(medthick) lcolor(black) msize(huge) ///
	, yscale(range(45 75)) xscale(range(0 6))  ///
	xlabel( 1.5 "Baseline" 4.5 "Postintervention") ///
	legend( ///
			cols(3) ///
			label(1 "Group A") ///
			label(2 "Group B") ///
			label(3 "95% CI")  ///
			) ///
	xtitle("Trial results") ytitle("Level of outcome")

C) Point estimates with confidence intervals.

Two groups, point estimates, confidence intervals at baseline/post-intervention

Two groups, point estimates, confidence intervals at baseline/post-intervention

Use first two steps of the “bargraph” and then a scatter plot instead of [twoway bar]

scatter  levelofoutcome barposition  if(group==1) ///
		,msymbol(D) msize(large) ///
	|| scatter  levelofoutcome barposition  if(group==2) ///
		,msymbol(S) msize(large) ///
	|| rcap  lowerlimit upperlimit barposition 		 ///
	,lwidth(medthick) lcolor(black) msize(huge) ///
	, yscale(range(45 75))   xscale(range(0 6))  ///
	xlabel( 1.5 "Baseline" 4.5 "Postintervention") ///
	legend( ///
			cols(3) ///
			label(1 "Group A") ///
			label(2 "Group B") ///
			label(3 "95% CI")  ///
			) ///
	xtitle("Trial results") ytitle("Level of outcome")

Data:
example_data_linegraph

Reference:

The trick for positioning bars/pointestimates comes from: UCLA on bargraphs with with errors in Stata

Advertisements