Statistical programming, matrix languages, and more

How do I graph 500 spline on one chart and obtain the predicted values of all.

Posts: 1

How do I graph 500 spline on one chart and obtain the predicted values of all.

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

Posts: 3,238

Re: How do I graph 500 spline on one chart and obtain the predicted values of all.

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

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"};


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 */

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

/* 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 */

Post a Question
Discussion Stats
  • 1 reply
  • 2 in conversation