BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
userbester1
Fluorite | Level 6

I want to draw a graph for observed vs predicted values with confidence and predicted intervals in simple linear regression. And also need to show residuals versus model predicted values on the same graph (similar to the below graph).

userbester1_0-1661747959071.png

Does anyone know how to do it?

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

How about this one ?

 

data have;
 set sashelp.class;
run;

ods select none;
ods output FitStatistics=FitStatistics;
proc reg data=have ;
model weight= height;
output out=want p=pred r=residual ;
quit;
ods select all;
data _null_;
 set FitStatistics;
 call symputx('rsquare',put(nValue2,8.2));
 stop;
run;
data want;
 set want;
 residual=-residual;
run;
proc sgplot data=want;
title 'Hoffman Holstein Model';
reg x=weight y=pred/clm nomarkers;
scatter x=weight y=pred/markerattrs=(symbol=circlefilled) name='a' legendlabel='Observed DMI';
scatter x=weight y=residual/markerattrs=(symbol=diamondfilled) name='b' legendlabel='Residuals';
lineparm x=0 y=0 slope=1/lineattrs=(color=grey);
inset "R(*ESC*){sup '2'} = &rsquare."/  position=TopLeft border;
refline 0/axis=y;
yaxis label='Predicted DMI';
xaxis label='Observed DMI';
keylegend 'a' 'b';
run;

Ksharp_0-1661774468074.png

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Show us a portion of your data. Show us your code.

 

Do you really want residuals and the raw data on the same plot? That really doesn't make sense to do.

--
Paige Miller
Ksharp
Super User

How about this one ?

 

data have;
 set sashelp.class;
run;

ods select none;
ods output FitStatistics=FitStatistics;
proc reg data=have ;
model weight= height;
output out=want p=pred r=residual ;
quit;
ods select all;
data _null_;
 set FitStatistics;
 call symputx('rsquare',put(nValue2,8.2));
 stop;
run;
data want;
 set want;
 residual=-residual;
run;
proc sgplot data=want;
title 'Hoffman Holstein Model';
reg x=weight y=pred/clm nomarkers;
scatter x=weight y=pred/markerattrs=(symbol=circlefilled) name='a' legendlabel='Observed DMI';
scatter x=weight y=residual/markerattrs=(symbol=diamondfilled) name='b' legendlabel='Residuals';
lineparm x=0 y=0 slope=1/lineattrs=(color=grey);
inset "R(*ESC*){sup '2'} = &rsquare."/  position=TopLeft border;
refline 0/axis=y;
yaxis label='Predicted DMI';
xaxis label='Observed DMI';
keylegend 'a' 'b';
run;

Ksharp_0-1661774468074.png

 

userbester1
Fluorite | Level 6
Thanks for your help
Rick_SAS
SAS Super FREQ

Use the OUTPUT statement in your regression procedure to output the predicted and residual values to a data set. Use PROC SGPLOT to create the plot. You will use two SCATTER statements and a SERIES statement. As Paige says, you'd better hope that the scale of the residuals is such that you can plot both Y and Residuals on the same axis. 

 

data Have;
set sashelp.class;
X = Height;
Y = Weight;
keep X Y;
run;

proc reg data=Have plots=none;
   model Y = X;
   output out=RegOut P=Pred R=Resid;
quit;

proc sort data=RegOut;
   by X;
run;

proc sgplot data=RegOut;
   scatter x=X y=Y;   /* origin data */
   series x=X y=Pred; /* regression fit */
   scatter x=x y=Resid / markerattrs=GraphData2;
   refline 0;
run;
userbester1
Fluorite | Level 6

Thank you so much

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is ANOVA?

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.

Discussion stats
  • 5 replies
  • 828 views
  • 3 likes
  • 4 in conversation