You are on page 1of 4

Biostatistics 140.

621 Graphing Confidence Intervals 1





2009 Johns Hopkins University Department of Biostatistics 10/01/09



How to Graph Point Estimates and
95% Confidence Intervals Using Stata 11 or Excel


The methods presented here are just several of many ways to construct the graph.


A. Simplest method using Stata:

One simple way in which to portray a graphical representation of the confidence intervals for the
difference in mean weight change for each of the age-gender groups is to use the Stata command
serrbar, with the option scale(1.96) to provide bars extending to
+/- 1.96 standard errors.



1) First, create a new small data set in the editor by manually entering the following
variables:

agegen (age-gender group), mean(the mean weight change within an age-gender group), and se
(the associated standard error of the mean weight change).

Enter the following values into the Stata data editor:

. list

agegen mean se
1. 1 16.125 3.505799
2. 2 22.88889 3.316515
3. 3 6.948718 2.211474
4. 4 14.02941 5.124386




2) Label the agegen variable:

. label define agegen 1 "M < 30" 2 "M 30+" 3 "F < 30" 4 "F 30+"

. label values agegen agegen

. list

agegen mean se
1. M < 30 16.125 3.505799
2. M 30+ 22.88889 3.316515
3. F < 30 6.948718 2.211474
4. F 30+ 14.02941 5.124386





. destring, replace
Update: Before step 2, run the following command to turn strings into numbers:
Biostatistics 140.621 Graphing Confidence Intervals 2



2009 Johns Hopkins University Department of Biostatistics 10/01/09




3) Graph using the serrbar command:




. serrbar mean se agegen, scale (1.96)

0
1
0
2
0
3
0
m
e
a
n
1 2 3 4
agegen

4) Enhance the serrbar command with some options:



serrbar mean se agegen, scale (1.96) title("95% Confidence Interval for Mean
Weight Change") sub("Between Current Weight and Weight at Age 18") yline(0)
yline(0) b2(Age-Gender Group) t1(95% Confidence Interval for Mean Weight Change)
l1(Weight Change (pounds)) ylab(-5(5)35) xlab(1(1)4) xlabel(, valuelabel)

-
5
0
5
1
0
1
5
2
0
2
5
3
0
3
5
m
e
a
n
M < 30 M 30+ F < 30 F 30+
agegen
W
e
i
g
h
t

C
h
a
n
g
e

(
p
o
u
n
d
s
)
95% Confidence Interval for Mean Weight Change
Age-Gender Group
Between Current Weight and Weight at Age 18
95% Confidence Interval for Mean Weight Change
Biostatistics 140.621 Graphing Confidence Intervals 3



2009 Johns Hopkins University Department of Biostatistics 10/01/09



B. Method 2 using Stata:
1) Enter the data as follows, with a separate line for the lower limit of the CI, the mean, and
the upper limit of the CI:
group level change
1 Lower 8.65
1 Mean 16.13
1 Upper 23.60
2 Lower 15.89
2 Mean 22.89
2 Upper 29.89
3 Lower 2.47
3 Mean 6.95
3 Upper 11.43
4 Lower 3.60
4 Mean 14.03
4 Upper 24.46

2) Label the age-gender group variable.

label define agegenf 1 "M < 30" 2 "M 30+" 3 "F < 30" 4 "F 30+"
label values group agegenf

3) Sort the data.

egen xmin=min(group), by(group)
egen xmax=max(group), by(group)
gsort -xmin -xmax group group

4) Plot using the twoway scatter command.

twoway scatter change group, msymbol(+) c(L) xlab(1(1)4) ylab(-5(5)35) l2("95%
Confidence Interval") l1("for the true mean change in weight") b2(Age-Gender
Group) t1(Example of graph comparing 95% confidence intervals) yline(0) xlabel(,
valuelabel)

5) The above commands yield the following plot:
-
5
0
5
1
0
1
5
2
0
2
5
3
0
3
5
C
h
a
n
g
e
M < 30 M 30+ F < 30 F 30+
Group
f
o
r

t
h
e

t
r
u
e

m
e
a
n

c
h
a
n
g
e

i
n

w
e
i
g
h
t
9
5
%

C
o
n
f
i
d
e
n
c
e

I
n
t
e
r
v
a
l
Example of graph comparing 95% confidence intervals
Age-Gender Group

Biostatistics 140.621 Graphing Confidence Intervals 4



2009 Johns Hopkins University Department of Biostatistics 10/01/09



C. A method using Excel:

Step 1: Enter the data into a new spreadsheet. You will need to enter the group variable, the
mean and the error bound (this is the critical value (t or z) times the standard error, aka the
difference from the mean to the confidence limits). For our example, enter the data as
illustrated below:
Group Mean ErrorBound
1 16.13 7.28
2 22.89 7
3 6.95 4.48
4 14.03 10.43
Step 2: Follow the steps below using the Chart Wizard to construct the graph.

a) Using the mouse, select the Group and Mean values.
b) Click on the Chart Wizard.
c) Select XY (Scatter), click Next.
d) Select: Series in Columns, click Next.
e) Remove Gridlines, add axis labels as you want, click Next.
f) Select how you want to store the graph, click Finish.
g) Go into the graph and double-click on one of the data points.
h) A menu will appear titled Format Data Series.
i) Select Y Error Bars menu.
j) In Display, select Both.
k) In Error Amount, select Custom.
l) You want to add and subtract the error bounds that you entered into the spreadsheet, so
for each + and -, select the error bounds from your spreadsheet by click and
dragging over these values.
m) Then select Okay.
The graph you obtain looks something like this:
Example of 95% confidence intervals
M,<30
M,30+
F,<30
F,30+
0
5
10
15
20
25
30
35
Group
9
5
%

c
o
n
f
i
d
e
n
c
e

i
n
t
e
r
v
a
l

f
o
r

t
h
e

t
r
u
e

m
e
a
n

d
i
f
f
e
r
e
n
c
e

i
n

w
e
i
g
h
t

You may want to change the labels for the groups or axis labels or title.

You might also like