BookmarkSubscribeRSS Feed
Arkady
Calcite | Level 5

Dear SAS IML Experts,

I have some simulated data, n=500 in the following format:

x1 x2 x3 x4 x5 y1 y2 y3 y4 y5 count

I need to fit a cubic spline to each observation, probably by using the Splinec call in SAS/IML, and output 'nout' (200 fitted points).

But Splinec requires that I input 5x2 matrix (no weights required).

So I would have to enter the data in 5x2 format (e.g.: x1,y1; x2 y2; x3 y3; x4 y4; x5 y5).

How do I sample 500 5x2 matrixes from the 500 x 11 matrix that I have?

Note that each row is sufficient to create one 5x2 matrix.

Once I obtain 'nout' from each 5x2 matrix I would like to graph it, and overlay all 500 graphs on one plot. How could I do this?

Finally, I would like to have a output database where all the 'nout's are listed by 'count' and x variables - a DB of fitted values from slightly different splines for the same x-value.

How could I merge 500 'nouts' in an efficient manner?

Your help would be greatly appreciated. Many Thanks!


Best regards,

Arkady Gershteyn

1 REPLY 1
Rick_SAS
SAS Super FREQ

Well, I'm guessing at some of the details of how your data are structured and what you are really looking for, but here's my best guess. The following DATA step creates 50 (instead of 500) data points that follow the model y = 3 + sin(x) + RANDOM_NOISE.  Use whatever other model you want.

data have;
array x[5] x1-x5;
array y[5] y1-y5;
drop i;
call streaminit(12345);
do count = 1 to 50;
   do i = 1 to dim(x);
      x = i;
      y = 3 + sin(x) + rand("Normal", 0, 0.2);
   end;
   output;
end;
run;

It sounds like you want to read in this data and fit a cubic spline to each observation. To do that,read the x1-y5 variables  data into the matrix Z. For each row, use the SHAPE function and the T (transpose) function to reshape the row into a 5x2 matrix.  Call the SPLINE function to fit the data on evenly-spaced grid of x values.

Save all the results and write it to a SAS data set so that PROC SGPLOT can graph it.

proc iml;
varNames = ("x1":"x5") || ("y1":"y5");

use have;

read all var varNames into Z;

read all var {"Count"};

close;

nout = 20;               /* for testing, set NOUT small */
x = j(nrow(Z), nout);    /* for storing results */
y = j(nrow(Z), nout);    /* for storing results */
do i = 1 to nrow(Z);     /* for each row */
   v = shape(Z[i,],2);   /* x is row 1 and y is row 2 */
   xy = T(v);            /* x is col 1 and y is col 2 */
   call spline(fit, xy) NOUT=nout;
   x[i,] = T(fit[,1]);   /* store x values in i_th row of y */
   y[i,] = T(fit[,2]);   /* store predicted values in i_th row of y */
end;

/* Write data to data set; index each spline by ID var */
group = repeat(count, 1, nout);
create spline var {group x y};
append;
close;
quit;

/* plot overlay */
title "Overlay of Splines";
proc sgplot data=spline noautolegend;
series x=x y=y / group=group lineattrs=GraphData1 transparency=0.9; /* turn on transparency */
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1 reply
  • 801 views
  • 3 likes
  • 2 in conversation