Hi,
Could you kindly help me?
I would like to add confidence intervals to a graph following proc GLM with data from multiple timepoints.
I calculated the means in each group, but not CIs. Could you teach me how to calculate and graph the CIs?
Or would it be easier to use a mixed model?
Data, code, and sample graph are below.
Thank you so much 🙂
proc glm data=exercise;
class exertype;
model time1 time2 time3 = exertype;
repeated time 3 ;
lsmeans exertype / out=means;
run;
quit;
proc print data=means;
run;
proc sgplot data=means;
series x=_name_ y=lsmean / group=exertype;
run;
quit;
sample data:
data exercise;
input id exertype diet time1 time2 time3;
cards;
1 1 1 85 85 88
2 1 1 90 92 93
3 1 1 97 97 94
4 1 1 80 82 83
5 1 1 91 92 91
6 1 2 83 83 84
7 1 2 87 88 90
8 1 2 92 94 95
9 1 2 97 99 96
10 1 2 100 97 100
11 2 1 86 86 84
12 2 1 93 103 104
13 2 1 90 92 93
14 2 1 95 96 100
15 2 1 89 96 95
16 2 2 84 86 89
17 2 2 103 109 90
18 2 2 92 96 101
19 2 2 97 98 100
20 2 2 102 104 103
21 3 1 93 98 110
22 3 1 98 104 112
23 3 1 98 105 99
24 3 1 87 132 120
25 3 1 94 110 116
26 3 2 95 126 143
27 3 2 100 126 140
28 3 2 103 124 140
29 3 2 94 135 130
30 3 2 99 111 150
;
run;
I just figured it out, thank you Reeza for your kind help 🙂 I added the group function in the band command.
proc sgplot data=means;
series x=_name_ y=lsmean / group=exertype ;
band x = _name_ upper=upperci lower=lowerci / fillattrs=(color=graydd) group=exertype transparency=.80;
run;
quit;
Start by adding the CL option to the LSMEANS statement and that will give you the confidence limits to plot.
proc glm data=exercise;
class exertype;
model time1 time2 time3 = exertype;
repeated time 3 ;
lsmeans exertype / out=means CL;
run;
Then you can add them with a BAND statement. I don't know what the exact variables are that come out of the LSMEANS statement so you'll need to modify the code to take the correct names.
proc sgplot data=means;
series x=_name_ y=lsmean / group=exertype;
band x = _name_ upper=upper_limit lower=lower_limit / fillattrs=(color=graydd);
run;
quit;
Dear Reeza,
Thank you so much for your reply 🙂
The standard error comes out from the CL option in lsmeans, and I converted it to upper and lower CI's. However, when I graph the upper and lower CI's using band, something doesn't quite work. Could you kindly help? Thank you again! I very much appreciate your help 🙂 Code, data, and graph are below.
proc glm data=exercise;
class exertype;
model time1 time2 time3 = exertype;
repeated time 3 ;
lsmeans exertype / out=means CL pdiff;
run;
quit;
data means;
set means;
upperci = lsmean + (1.96*STDERR);
lowerci = lsmean - (1.96*STDERR);
run;
proc print data=means;
run;
proc sgplot data=means;
series x=_name_ y=lsmean / group=exertype;
band x = _name_ upper=upperci lower=lowerci / fillattrs=(color=graydd);
run;
quit;
Sample of "mean" data:
Obs _NAME_ exertype LSMEAN STDERR upperci lowerci123456789
time1 | 1 | 90.2 | 1.84932 | 93.825 | 86.575 |
time1 | 2 | 93.1 | 1.84932 | 96.725 | 89.475 |
time1 | 3 | 96.1 | 1.84932 | 99.725 | 92.475 |
time2 | 1 | 90.9 | 2.95284 | 96.688 | 85.112 |
time2 | 2 | 96.6 | 2.95284 | 102.388 | 90.812 |
time2 | 3 | 117.1 | 2.95284 | 122.888 | 111.312 |
time3 | 1 | 91.4 | 3.47227 | 98.206 | 84.594 |
time3 | 2 | 95.9 | 3.47227 | 102.706 | 89.094 |
time3 | 3 | 126.0 | 3.47227 | 132.806 | 119.194 |
Dear Reeza,
Thank you so much for your help and reply! The CI option output the standard error (which I converted to CI's) and tried to add them to the graph with the band function. The graph is not looking quite right. Could you kindly advise? Thank you again! 🙂
proc glm data=exercise;
class exertype;
model time1 time2 time3 = exertype;
repeated time 3 ;
lsmeans exertype / out=means CL;
run;
quit;
data means;
set means;
upperci = lsmean + (1.96*STDERR);
lowerci = lsmean - (1.96*STDERR);
run;
proc print data=means;
run;
proc sgplot data=means;
series x=_name_ y=lsmean / group=exertype;
band x = _name_ upper=upperci lower=lowerci / fillattrs=(color=graydd);
run;
quit;
Obs _NAME_ exertype LSMEAN STDERR upperci lowerci123456789
time1 | 1 | 90.2 | 1.84932 | 93.825 | 86.575 |
time1 | 2 | 93.1 | 1.84932 | 96.725 | 89.475 |
time1 | 3 | 96.1 | 1.84932 | 99.725 | 92.475 |
time2 | 1 | 90.9 | 2.95284 | 96.688 | 85.112 |
time2 | 2 | 96.6 | 2.95284 | 102.388 | 90.812 |
time2 | 3 | 117.1 | 2.95284 | 122.888 | 111.312 |
time3 | 1 | 91.4 | 3.47227 | 98.206 | 84.594 |
time3 | 2 | 95.9 | 3.47227 | 102.706 | 89.094 |
time3 | 3 | 126.0 | 3.47227 | 132.806 | 119.194 |
I just figured it out, thank you Reeza for your kind help 🙂 I added the group function in the band command.
proc sgplot data=means;
series x=_name_ y=lsmean / group=exertype ;
band x = _name_ upper=upperci lower=lowerci / fillattrs=(color=graydd) group=exertype transparency=.80;
run;
quit;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.