The TPSPLINE Procedure

Getting Started: TPSPLINE Procedure

(View the complete code for this example.)

The following example demonstrates how you can use the TPSPLINE procedure to fit a semiparametric model.

Suppose that y is a continuous variable and x1 and x2 are two explanatory variables of interest. To fit a bivariate thin-plate spline model, you can use a MODEL statement similar to that used in many regression procedures in the SAS System:

proc tpspline;
   model y = (x1 x2);
run;

The TPSPLINE procedure can fit semiparametric models; the parentheses in the preceding MODEL statement separate the smoothing variables from the regression variables. The following statements illustrate this syntax:

proc tpspline;
   model y = z1 (x1 x2);
run;

This model assumes a linear relation with z1 and an unknown functional relation with x1 and x2.

If you want to fit several responses by using the same explanatory variables, you can save computation time by using the multiple responses feature in the MODEL statement. For example, if y1 and y2 are two response variables, the following MODEL statement can be used to fit two models. Separate analyses are then performed for each response variable.

proc tpspline;
   model y1 y2 = (x1 x2);
run;

The following example illustrates the use of PROC TPSPLINE. The data are from Bates et al. (1987).

data Measure;
   input x1 x2 y @@;
   datalines;
-1.0 -1.0   15.54483570   -1.0 -1.0    15.76312613
 -.5 -1.0   18.67397826    -.5 -1.0    18.49722167
  .0 -1.0   19.66086310     .0 -1.0    19.80231311
  .5 -1.0   18.59838649     .5 -1.0    18.51904737
 1.0 -1.0   15.86842815    1.0 -1.0    16.03913832
-1.0  -.5   10.92383867   -1.0  -.5    11.14066546
 -.5  -.5   14.81392847    -.5  -.5    14.82830425
  .0  -.5   16.56449698     .0  -.5    16.44307297
  .5  -.5   14.90792284     .5  -.5    15.05653924
 1.0  -.5   10.91956264    1.0  -.5    10.94227538
-1.0   .0    9.61492010   -1.0   .0     9.64648093
 -.5   .0   14.03133439    -.5   .0    14.03122345
  .0   .0   15.77400253     .0   .0    16.00412514
  .5   .0   13.99627680     .5   .0    14.02826553
 1.0   .0    9.55700164    1.0   .0     9.58467047
-1.0   .5   11.20625177   -1.0   .5    11.08651907
 -.5   .5   14.83723493    -.5   .5    14.99369172
  .0   .5   16.55494349     .0   .5    16.51294369
  .5   .5   14.98448603     .5   .5    14.71816070
 1.0   .5   11.14575565    1.0   .5    11.17168689
-1.0  1.0   15.82595514   -1.0  1.0    15.96022497
 -.5  1.0   18.64014953    -.5  1.0    18.56095997
  .0  1.0   19.54375504     .0  1.0    19.80902641
  .5  1.0   18.56884576     .5  1.0    18.61010439
 1.0  1.0   15.86586951    1.0  1.0    15.90136745
;

The data set Measure contains three variables x1, x2, and y. Suppose that you want to fit a surface by using the variables x1 and x2 to model the response y. The variables x1 and x2 are spaced evenly on a square, and the response y is generated by adding a random error to a function . The raw data are plotted in three-dimensional scatter plot by using the G3D procedure. In order to visualize those replicates, half of the data are shifted a little bit by adding a small value (0.001) to x1 values, as in the following statements:

data Measure1;
   set Measure;
run;

proc sort data=Measure1;
   by x2 x1;
run;

data Measure1;
   set Measure1;
   if mod(_N_, 2) = 0 then x1=x1+0.001;
run;
proc g3d data=Measure1;
   scatter x2*x1=y /size=.5
                    zmin=9 zmax=21
                    zticknum=4;
   title "Raw Data";
run;

Figure 118.1 displays the raw data.

Figure 118.1: Plot of Data Set MEASURE

Plot of Data Set MEASURE


The following statements invoke the TPSPLINE procedure, to analyze the Measure data set as input. In the MODEL statement, the x1 and x2 variables are listed as smoothing variables. The LOGNLAMBDA=  option specifies that PROC TPSPLINE examine a list of models with ranging from –4 to –2.5. The OUTPUT statement creates the data set estimate to contain the predicted values and the 95% upper and lower confidence limits from the best model selected by the GCV criterion.

ods graphics on;

proc tpspline data=Measure;
   model y=(x1 x2) /lognlambda=(-4 to -2.5 by 0.1);
   output out=estimate pred uclm lclm;
run;

proc print data=estimate;
run;

When ODS Graphics is enabled, PROC TPSPLINE produces several default plots. One of the default plots is the contour plot of the fitted surface, shown in Figure 118.2. The surface exhibits nonlinear patterns along the directions of both predictors.

Figure 118.2: Fitted Surface from PROC TPSPLINE

Fitted Surface from PROC TPSPLINE


Figure 118.3 shows the "Criterion Plot" that provides a graphical display of the GCV selection process. Three sets of values are shown in the plot: the specified smoothing values and their GCV values, the examined smoothing values and their GCV values during the optimization process, and the best smoothing parameter and its GCV value. The final thin-plate smoothing spline estimate is based on , which minimizes the GCV.

Figure 118.3: The GCV Criterion by

The GCV Criterion by 10(n)


Figure 118.4 shows that the data set Measure contains 50 observations with 25 unique design points. The final model contains no parametric regression terms and two smoothing variables. The order of the derivative in the penalty is 2 by default, and the dimension of polynomial space is 3. See the section Computational Formulas for definitions.

Figure 118.4 also lists the GCV values along with the supplied values of . The value that minimizes the GCV function is –3.5 among the given list of .

The residual sum of squares from the fitted model is 0.246110, and the model degrees of freedom are 24.593203. The standard deviation, defined as , is 0.098421. The predictions and 95% confidence limits are displayed in Figure 118.5.

Figure 118.4: Fitted Model Summaries from PROC TPSPLINE

The TPSPLINE Procedure
Dependent Variable: y

Summary of Input Data Set
Number of Non-Missing Observations50
Number of Missing Observations0
Unique Smoothing Design Points25

Summary of Final Model
Number of Regression Variables0
Number of Smoothing Variables2
Order of Derivative in the Penalty2
Dimension of Polynomial Space3

GCV Function
log10(n*Lambda)GCV 
-4.0000000.019215 
-3.9000000.019183 
-3.8000000.019148 
-3.7000000.019113 
-3.6000000.019082 
-3.5000000.019064*
-3.4000000.019074 
-3.3000000.019135 
-3.2000000.019286 
-3.1000000.019584 
-3.0000000.020117 
-2.9000000.021015 
-2.8000000.022462 
-2.7000000.024718 
-2.6000000.028132 
-2.5000000.033165 

Note: * indicates minimum GCV value.


Summary Statistics of Final Estimation
log10(n*Lambda)-3.4762
Smoothing Penalty2558.1432
Residual SS0.2461
Tr(I-A)25.4068
Model DF24.5932
Standard Deviation0.0984
GCV0.0191


Figure 118.5: Data Set ESTIMATE

Obsx1x2yP_yLCLM_yUCLM_y
1-1.0-1.015.544815.647415.511515.7832
2-1.0-1.015.763115.647415.511515.7832
3-0.5-1.018.674018.578318.443018.7136
4-0.5-1.018.497218.578318.443018.7136
50.0-1.019.660919.727019.591719.8622
60.0-1.019.802319.727019.591719.8622
70.5-1.018.598418.555218.419918.6905
80.5-1.018.519018.555218.419918.6905
91.0-1.015.868415.943615.807716.0794
101.0-1.016.039115.943615.807716.0794
11-1.0-0.510.923811.046710.911411.1820
12-1.0-0.511.140711.046710.911411.1820
13-0.5-0.514.813914.824614.689614.9597
14-0.5-0.514.828314.824614.689614.9597
150.0-0.516.564516.510216.375216.6452
160.0-0.516.443116.510216.375216.6452
170.5-0.514.907914.981214.846115.1162
180.5-0.515.056514.981214.846115.1162
191.0-0.510.919610.949710.814411.0850
201.0-0.510.942310.949710.814411.0850
21-1.00.09.61499.63729.50199.7724
22-1.00.09.64659.63729.50199.7724
23-0.50.014.031314.018813.883814.1538
24-0.50.014.031214.018813.883814.1538
250.00.015.774015.882215.747216.0171
260.00.016.004115.882215.747216.0171
270.50.013.996314.000613.865614.1356
280.50.014.028314.000613.865614.1356
291.00.09.55709.57699.44179.7122
301.00.09.58479.57699.44179.7122
31-1.00.511.206311.161411.026111.2967
32-1.00.511.086511.161411.026111.2967
33-0.50.514.837214.918214.783115.0532
34-0.50.514.993714.918214.783115.0532
350.00.516.554916.538616.403616.6736
360.00.516.512916.538616.403616.6736
370.50.514.984514.854914.719914.9900
380.50.514.718214.854914.719914.9900
391.00.511.145811.172711.037411.3080
401.00.511.171711.172711.037411.3080
41-1.01.015.826015.885115.749316.0210
42-1.01.015.960215.885115.749316.0210
43-0.51.018.640118.594618.459318.7299
44-0.51.018.561018.594618.459318.7299
450.01.019.543819.672919.537619.8081
460.01.019.809019.672919.537619.8081
470.51.018.568818.583218.447818.7185
480.51.018.610118.583218.447818.7185
491.01.015.865915.876115.740216.0120
501.01.015.901415.876115.740216.0120


You can also use the TEMPLATE and SGRENDER procedures to create a perspective plot for visualizing the fitted surface. Because the data in the data set Measure are very sparse, the fitted surface is not smooth. To produce a smoother surface, the following statements generate the data set pred in order to obtain a finer grid. The LOGNLAMBDA0= option requests that PROC TPSPLINE fit a model with a fixed value of –3.4762. The SCORE statement evaluates the fitted surface at those new design points.

data pred;
   do x1=-1 to 1 by 0.1;
      do x2=-1 to 1 by 0.1;
         output;
      end;
   end;
run;

proc tpspline data=measure;
   model y=(x1 x2)/lognlambda0=-3.4762;
   score data=pred out=predy;
run;

proc template;
   define statgraph surface;
      dynamic _X _Y _Z _T;
      begingraph /designheight=360;
         entrytitle _T;
         layout overlay3d/rotate=120 cube=false  xaxisopts=(label="x1")
                          yaxisopts=(label="x2") zaxisopts=(label="P_y");
            surfaceplotparm x=_X y=_Y z=_Z;
         endlayout;
      endgraph;
   end;
run;

proc sgrender data=predy template=surface;
   dynamic _X='x1' _Y='x2' _Z='P_y'
           _T='Plot of Fitted Surface on a Fine Grid';
run;

The surface plot based on the finer grid is displayed in Figure 118.6. The plot indicates that a parametric model with quadratic terms of x1 and x2 provides a reasonable fit to the data.

Figure 118.6: Plot of TPSPLINE Fit

Plot of TPSPLINE Fit


Figure 118.7 shows a panel of fit diagnostics for the selected model that indicate a reasonable fit:

  • The predicted values closely approximate the observed values.

  • The residuals are approximately normally distributed and do not show obvious systematic patterns.

  • The RFPLOT shows that much variation in the response variable is addressed by the fit and only a little remains in the residuals.

Figure 118.7: Fit Diagnostics

Fit Diagnostics