BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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; 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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;

View solution in original post

10 REPLIES 10
djrisks
Barite | Level 11

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.

edasdfasdfasdfa
Quartz | Level 8

So is it not possible to do directly within proc iml without creating a data set?

Reeza
Super User

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

edasdfasdfasdfa
Quartz | Level 8

sorry I meant to say proc step.

Ksharp
Super User

Why not post it at IML forum and Calling @Rick_SAS 

edasdfasdfasdfa
Quartz | Level 8

Is there someone that can move it there? I wasn't sure if this was the better place or iml.

Rick_SAS
SAS Super FREQ

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;

edasdfasdfasdfa
Quartz | Level 8

@Rick_SAS you are so knowledgeable! 

 

btw is the line created by the series statement equivalent to a regression line?

Rick_SAS
SAS Super FREQ

Yes. The least-squares regression line is the same as the predicted values.

Rick_SAS
SAS Super FREQ

If your question has been answered, please close this thread.

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!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 10 replies
  • 1702 views
  • 0 likes
  • 5 in conversation