(Very) Dear community,
I have a PROC GLM with one class variable that has two levels, and I would like to get plots for the means for each condition.
I attach an example of the type of plot that I'd like, below .
It is generally recommended to use dot plots with error bars (rather than bar charts) to plot a mean and confidence limits. This is built into PROC GLM. You can use the LSMEANS and PLOTS=MEANPLOT options to get the plot, as follows:
proc glm data=sashelp.class plots(only)=(meanplot(cl ascending)); /* plot of means and CIs */
class sex;
model Weight = sex;
output out=GLMOut Predicted=Pred lclm=lclm uclm=uclm;
lsmeans Sex / cl;
run;
quit;
If you insist on using a bar chart, then use ODS OUTPUT to capture the LSMeanCL table, then use the techniques in the article "Creating bar charts with confidence intervals" to create the plot. It will look something like this:
ods trace on;
proc glm data=sashelp.class plots(only)=(meanplot(cl ascending)); /* plot of means and CIs */
class sex;
model Weight = sex;
output out=GLMOut Predicted=Pred lclm=lclm uclm=uclm;
lsmeans Sex / cl;
ods output LSMeanCL=LS;
run;
quit;
proc sgplot data=LS;
vbarparm category=Sex response=LSMean /
limitlower=LowerCL limitupper=UpperCL;
run;
As Peter Flom wrote in the comments to my blog post:
Bar charts with confidence intervals are also known as "dynamite plots" and for two reasons: 1) They look like the old dynamite things on cartoons and 2) They are dangerous. See http://biostat.mc.vanderbilt.edu/twiki/pub/Main/TatsukiRcode/Poster3.pdf for a good explanation of why they are bad.
You can get close to this plot using one of the plotting options in PROC GLM, specifically ANOMPLOT or MEANPLOT. These won't match exactly what you show, but are specifically designed to provide meaningful graphics in the case where you are comparing means, so in that sense, the plots will be better than the plots you show.
Thank you!
It is generally recommended to use dot plots with error bars (rather than bar charts) to plot a mean and confidence limits. This is built into PROC GLM. You can use the LSMEANS and PLOTS=MEANPLOT options to get the plot, as follows:
proc glm data=sashelp.class plots(only)=(meanplot(cl ascending)); /* plot of means and CIs */
class sex;
model Weight = sex;
output out=GLMOut Predicted=Pred lclm=lclm uclm=uclm;
lsmeans Sex / cl;
run;
quit;
If you insist on using a bar chart, then use ODS OUTPUT to capture the LSMeanCL table, then use the techniques in the article "Creating bar charts with confidence intervals" to create the plot. It will look something like this:
ods trace on;
proc glm data=sashelp.class plots(only)=(meanplot(cl ascending)); /* plot of means and CIs */
class sex;
model Weight = sex;
output out=GLMOut Predicted=Pred lclm=lclm uclm=uclm;
lsmeans Sex / cl;
ods output LSMeanCL=LS;
run;
quit;
proc sgplot data=LS;
vbarparm category=Sex response=LSMean /
limitlower=LowerCL limitupper=UpperCL;
run;
As Peter Flom wrote in the comments to my blog post:
Bar charts with confidence intervals are also known as "dynamite plots" and for two reasons: 1) They look like the old dynamite things on cartoons and 2) They are dangerous. See http://biostat.mc.vanderbilt.edu/twiki/pub/Main/TatsukiRcode/Poster3.pdf for a good explanation of why they are bad.
Thank you! both are good.
I may take advantage of your expertise for one more step. The design is in fact more complex, with a nested factor that is random.
This is the syntax:
proc glm data=ebal_st plots=(meanplot(cl ascending));
class genre author;
model lessico = genre author(genre) /effectsize;
output out=GLMOut Predicted=Pred lclm=lclm uclm=uclm;
lsmeans genre / cl;
random author(genre);
ods output LSMeanCL=LS;
run;
quit;
run;
quit;
SAS automatically outputs an additional graph, to plot the random factor, AUTHOR, see below. I am not sure why for some of the levels of the random factor AUTHOR it puts what seem to be error bars. I cannot find the documentation where this graph is explained. Any idea?
The graph is below.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.