Fluorite | Level 6

## Regression plots

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).

Does anyone know how to do it?

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Regression plots

``````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;``````

5 REPLIES 5
Diamond | Level 26

## Re: Regression plots

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

--
Paige Miller
Super User

## Re: Regression plots

``````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;``````

Fluorite | Level 6

SAS Super FREQ

## Re: Regression plots

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;``````
Fluorite | Level 6

## Re: Regression plots

Thank you so much

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