Jun 20, 2015

Random graphs (49): Means with confidence intervals


tempname foo
tempname cogscore
postfile `foo' time ART cog coglb cogub using `cogscore', replace

levelsof(time), local(timepoint)
levelsof(ART), local(art)
foreach y of local art {
  foreach x of local timepoint {
    qui reg COG if time == `x' & ART == `y'
    local cog   = _b[_cons]
    local coglb  = _b[_cons] - (1.96 * _se[_cons])
    local cogub = _b[_cons] + (1.96 * _se[_cons])
 di `timepoint' _skip(5) `art' _skip(5) `cog' _skip(5) `coglb' _skip(5) `cogub'
    post `foo' (`x') (`y') (`cog') (`coglb') (`cogub')
  }
}
postclose `foo'

use `cogscore', clear

label define time 1 "Age 3" 2 "Age 5" 3 "Age 7" 4 "Age 11"
label val time time

twoway (rarea coglb cogub time if ART == 1) ///
       (rarea coglb cogub time if ART == 0) ///
       (line cog time if ART == 0) ///
       (line cog time if ART == 1) ///
      , legend(label(3 "Planned natural conception") ///
               label(4 "ART conception") col(1) ring(0) pos(11) order(4 3)) ///
        ytitle("Average cognitive development") ///
        xtitle(" ") xlabel(1(1)4, valuelabels) ///
        note("{it:Note:} Shaded areas denote 95 % confidence intervals." ///
             "{it:Source:} Millennium Cohort Study.", span) name(bands, replace)