The NLIN Procedure

Example 82.1 Segmented Model

(View the complete code for this example.)

Suppose you are interested in fitting a model that consists of two segments that connect in a smooth fashion. For example, the following model states that for values of x less than the mean of Y is a quadratic function in x, and for values of x greater than the mean of Y is constant:

In this model equation , , and are the coefficients of the quadratic segment, and c is the plateau of the mean function. The NLIN procedure can fit such a segmented model even when the join point, , is unknown.

We also want to impose conditions on the two segments of the model. First, the curve should be continuous—that is, the quadratic and the plateau section need to meet at . Second, the curve should be smooth—that is, the first derivative of the two segments with respect to x need to coincide at .

The continuity condition requires that

The smoothness condition requires that

If you solve for and substitute into the expression for c, the two conditions jointly imply that

Although there are apparently four unknowns, the model contains only three parameters. The continuity and smoothness restrictions together completely determine one parameter given the other three.

The following DATA step creates the SAS data set for this example:

data a;
   input y x @@;
   datalines;
.46 1  .47  2 .57  3 .61  4 .62  5 .68  6 .69  7
.78 8  .70  9 .74 10 .77 11 .78 12 .74 13 .80 13
.80 15 .78 16
;

The following PROC NLIN statements fit this segmented model:

title 'Quadratic Model with Plateau';
proc nlin data=a;
   parms alpha=.45 beta=.05 gamma=-.0025;

   x0 = -.5*beta / gamma;

   if (x < x0) then
        mean = alpha + beta*x  + gamma*x*x;
   else mean = alpha + beta*x0 + gamma*x0*x0;
   model y = mean;

   if _obs_=1 and _iter_ =.  then do;
      plateau =alpha + beta*x0 + gamma*x0*x0;
      put /  x0= plateau=  ;
   end;
   output out=b predicted=yp;
run;

The parameters of the model are , , and , respectively. They are represented in the PROC NLIN statements by the variables alpha, beta, and gamma, respectively. In order to model the two segments, a conditional statement is used that assigns the appropriate expression to the mean function depending on the value of . A PUT statement is used to print the constrained parameters every time the program is executed for the first observation. The OUTPUT statement computes predicted values for plotting and saves them to data set b.

Note that there are other ways in which you can write the conditional expressions for this model. For example, you could formulate a condition with two model statements, as follows:

proc nlin data=a;
   parms alpha=.45 beta=.05 gamma=-.0025;
   x0 = -.5*beta / gamma;
   if (x < x0) then
        model y = alpha+beta*x+gamma*x*x;
   else model y = alpha+beta*x0+gamma*x0*x0;
run;

Or you could use a single expression with a conditional evaluation, as in the following statements:

proc nlin data=a;
   parms alpha=.45 beta=.05 gamma=-.0025;
   x0 = -.5*beta / gamma;
   model y = (x <x0)*(alpha+beta*x +gamma*x*x) +
             (x>=x0)*(alpha+beta*x0+gamma*x0*x0);
run;

The results from fitting this model with PROC NLIN are shown in Output 82.1.1–Output 82.1.3. The iterative optimization converges after six iterations (Output 82.1.1). Output 82.1.1 indicates that the join point is 12.747 and the plateau value is 0.777.

Output 82.1.1: Nonlinear Least-Squares Iterative Phase

Quadratic Model with Plateau

The NLIN Procedure
Dependent Variable y
Method: Gauss-Newton

Iterative Phase
IteralphabetagammaSum of Squares
00.45000.0500-0.002500.0562
10.38810.0616-0.002340.0118
20.39300.0601-0.002340.0101
30.39220.0604-0.002370.0101
40.39210.0605-0.002370.0101
50.39210.0605-0.002370.0101
60.39210.0605-0.002370.0101

NOTE: Convergence criterion met.


Output 82.1.2: Results from Put Statement

x0=12.747669162 plateau=0.7774974276                                            


Output 82.1.3: Least-Squares Analysis for the Quadratic Model

SourceDFSum of SquaresMean SquareF ValueApprox
Pr > F
Model20.17690.0884114.22<.0001
Error130.01010.000774  
Corrected Total150.1869   

ParameterEstimateApprox
Std Error
Approximate 95% Confidence
Limits
alpha0.39210.02670.33450.4497
beta0.06050.008420.04230.0787
gamma-0.002370.000551-0.00356-0.00118


The following statements produce a graph of the observed and predicted values with reference lines for the join point and plateau estimates (Output 82.1.4):

proc sgplot data=b noautolegend;
   yaxis label='Observed or Predicted';
   refline 0.777  / axis=y label="Plateau"    labelpos=min;
   refline 12.747 / axis=x label="Join point" labelpos=min;
   scatter y=y  x=x;
   series  y=yp x=x;
run;

Output 82.1.4: Observed and Predicted Values for the Quadratic Model

 Observed and Predicted Values for the Quadratic Model


If you want to estimate the join point directly, you can use the relationship between the parameters to change the parameterization of the model in such a way that the mean function depends directly on . Using the smoothness condition that relates to ,

you can express as a function of and :

Substituting for in the model equation

yields the reparameterized model

This model is fit with the following PROC NLIN statements:

proc nlin data=a;
   parms alpha=.45 beta=.05 x0=10;
   if (x<x0) then
        mean = alpha + beta*x *(1-x/(2*x0));
   else mean = alpha + beta*x0/2;
   model y = mean;
run;

Output 82.1.5: Results from Reparameterized Model

The NLIN Procedure
Dependent Variable y
Method: Gauss-Newton

NOTE: Convergence criterion met.

SourceDFSum of SquaresMean SquareF ValueApprox
Pr > F
Model20.17690.0884114.22<.0001
Error130.01010.000774  
Corrected Total150.1869   

ParameterEstimateApprox
Std Error
Approximate 95% Confidence
Limits
alpha0.39210.02670.33450.4497
beta0.06050.008420.04230.0787
x012.74771.27819.986415.5089


The analysis of variance table in the reparameterized model is the same as in the earlier analysis (compare Output 82.1.5 and Output 82.1.3). Changing the parameterization of a model does not affect the fit. The "Parameter Estimates" table now shows x0 as a parameter in the model. The estimate agrees with the earlier result that uses the PUT statement (Output 82.1.2). Since is now a model parameter, the NLIN procedure also reports its asymptotic standard error and its approximate 95% confidence interval.