FPCA Procedure
Getting Started: FPCA Procedure
Note: Input data must be in a CAS table that is accessible in your CAS session. You must refer to this table by using a two-level name. The first level must be a CAS engine libref, and the second level must be the table name. For more information, see the sections Using CAS Sessions and CAS Engine Librefs and Loading a SAS Data Set onto a CAS Server in Chapter 2, Shared Concepts.
This example shows how to use the FPCA procedure to perform functional principal component analysis on a simulated functional data set. Each curve in the data is generated as a linear combination of two orthonormal eigenfunctions (a cosine and a sine wave) plus a nonlinear mean function. The following statements use the IML procedure to generate the simulated data:
proc iml;
call randseed(233);
N = 50; /* Number of subjects */
M = 100; /* Number of time points */
s = do(0, 10, (10 - 0) / (M - 1));
start meanFunct(s);
return (s + 10 # exp(-(s - 5)##2));
finish;
start eigFunct1(s);
return (cos(2 * s * constant('PI') / 10) / sqrt(5));
finish;
start eigFunct2(s);
return (-sin(2 * s * constant('PI') / 10) / sqrt(5));
finish;
Ksi = j(N, 2);
call randgen(Ksi, "Normal");
Ksi = (Ksi - mean(Ksi)) / std(Ksi); /* standardize */
Ksi = Ksi * diag({5, 2});
eig1 = eigFunct1(s);
eig2 = eigFunct2(s);
meanVec = meanFunct(s);
y = Ksi * (eig1 // eig2) + meanVec;
s_col = s`;
s_y = s_col || y`;
y_colnames = "y1":"y50";
colnames = {"_TIMEPOINTS_"} || y_colnames;
create work.fpca_train_data from s_y[colname=colnames];
append from s_y;
close work.fpca_train_data;
quit;
The data table fpca_train_data contains 100 rows (one per time point) and 51 columns: the first column contains the time points, and the remaining columns represent the 50 functional observations.
The following DATA step creates the input data table mylib.fpca_train_data. These statements assume that your CAS engine libref is named mylib, but you can substitute any appropriately defined CAS engine libref.
data mylib.fpca_train_data;
set fpca_train_data;
run;
The following statements use PROC FPCA to extract the dominant functional components from the data. In this example, you use 50 bins to smooth the data, and two principal components are retained.
proc fpca data=mylib.fpca_train_data nbins=50;
input y1-y50;
output out=mylib.scores npc=2;
savestate rstore=mylib.state;
run;
The NPC=2 option retains two functional principal components. The mylib.scores output table contains the scores for each function, and the mylib.state table contains the analytic store that you can use to score new functional data.
These results provide a low-dimensional representation of the original functional data that you can use in a downstream task such as clustering, anomaly detection, forecasting, or visualization.