Line chart with means and CIs in Stata

851 views Asked by At

I have a data set with effect estimates for different points in time (for 1 month, 2 months, 6 months, 12 months and 18 months) and its standard errors. Now I want to plot the means for each period and the corresponding CIs around the means.

My sample looks like:

effect  horizon   se
0.03    1         0.2
0.02    6         0.01
0.01    6         0.3
0.00    1         0.4
0.04    18        0.2
0.02    2         0.05
0.01    2         0.02
...     ...

The means of the effects for each horizon lead to 5 data points that I want to plot in a line chart together with the confidence intervals. I tried this:

egen means = mean(effect), by(horizon)
line means horizon

But how can I add the symmetric confidence bands? Such that I get something that looks like this: enter image description here

1

There are 1 answers

4
dimitriy On

Not entirely certain that this makes sense statistically, but here's how I might do this:

gen variance = se^2
collapse (mean) effect (sum) SV = variance (count) noobs = effect, by(horizon)
gen se_mean = sqrt(SV*(1/noobs)^2)
gen LB = effect - 1.96*se_mean
gen UB = effect + 1.96*se_mean

twoway (rline LB UB horizon, lpattern(dash dash)) (line effect horizon, lpattern(solid)), yline(0, lcolor(gray))   

Which yields:

enter image description here

To get the SE of the mean effects T̅, I am using the formula

V(T̅) = 1/(n2) Σin V(Ti)

(which assumes the covariances of the effects are all zero). I then take the square root to get the SE of T̅.