I know how to use plots within proc step but not sure how to add them within proc iml. In data step I can of course read the data step, in proc iml, I'm not sure how this is done.
I am just looking for a scatter plot with a regression line.
This is what I have so far.
proc iml;
A = {4 8,
6 12};
b = {5, 1};
/* find p as orthogonal projection onto first column of A */
C = A[,1];
p = (C`*b)/norm(C)**2 * C;
print p;
/* use a generalized inverse to find ANY x such that
A*x = p */
x = ginv(A)*p;
print x;
/* Check that A*x = b */
z = A*x;
print z p;
yhat = A*x;
r = b-yhat;
print yhat r;
sse = ssq(r);
dfe = nrow(A)-ncol(A);
mse = sse/dfe;
print sse dfe mse;
As stated in the documentation, the graphics in IML are not a complete interface to all the features that SGPLOT supports. So I think the best thing to do is to use the CREATE statement to write the data to a SAS data set and use PROC SGPLOT (either within a SUBMIT block or after you QUIT PROC IML.)
In your example, there are only two data points, so the scatter plot is rather sparse. But here it is:
/* Observed values are in b.
Predicted values are in yhat.
The explanatory variables are A[,1] and A[,2],
but A[,2]=2*A[,1], so this is equivalent to a 1-D problem */
xx = A[,1];
create PlotIt var {xx b yhat};
append;
close;
submit;
/* sort by X variable before using a SERIES statement */
proc sort data=PlotIt;
by xx;
run;
proc sgplot data=PlotIt;
label xx="Explanatory" b="Observed" yhat="Predicted";
scatter x=xx y=b;
series x=xx y=yhat;
run;
endsubmit;
I would just use Proc SGPLOT with the REG statement to do the plot. But you would need to have the data in a dataset first.
So is it not possible to do directly within proc iml without creating a data set?
@edasdfasdfasdfa wrote:
I know how to use plots within data step but not sure how to add them within proc iml.
Can you provide an example of how you create plots within a data step? That was something I didn't know was possible.....
sorry I meant to say proc step.
Why not post it at IML forum and Calling @Rick_SAS
Is there someone that can move it there? I wasn't sure if this was the better place or iml.
As stated in the documentation, the graphics in IML are not a complete interface to all the features that SGPLOT supports. So I think the best thing to do is to use the CREATE statement to write the data to a SAS data set and use PROC SGPLOT (either within a SUBMIT block or after you QUIT PROC IML.)
In your example, there are only two data points, so the scatter plot is rather sparse. But here it is:
/* Observed values are in b.
Predicted values are in yhat.
The explanatory variables are A[,1] and A[,2],
but A[,2]=2*A[,1], so this is equivalent to a 1-D problem */
xx = A[,1];
create PlotIt var {xx b yhat};
append;
close;
submit;
/* sort by X variable before using a SERIES statement */
proc sort data=PlotIt;
by xx;
run;
proc sgplot data=PlotIt;
label xx="Explanatory" b="Observed" yhat="Predicted";
scatter x=xx y=b;
series x=xx y=yhat;
run;
endsubmit;
@Rick_SAS you are so knowledgeable!
btw is the line created by the series statement equivalent to a regression line?
Yes. The least-squares regression line is the same as the predicted values.
If your question has been answered, please close this thread.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.