NAG Library Routine Document
G10ABF
1 Purpose
G10ABF fits a cubic smoothing spline for a given smoothing parameter.
2 Specification
SUBROUTINE G10ABF ( |
MODE, WEIGHT, N, X, Y, WT, RHO, YHAT, C, LDC, RSS, DF, RES, H, COMM, IFAIL) |
INTEGER |
N, LDC, IFAIL |
REAL (KIND=nag_wp) |
X(N), Y(N), WT(*), RHO, YHAT(N), C(LDC,3), RSS, DF, RES(N), H(N), COMM(9*N+14) |
CHARACTER(1) |
MODE, WEIGHT |
|
3 Description
G10ABF fits a cubic smoothing spline to a set of observations (, ), for . The spline provides a flexible smooth function for situations in which a simple polynomial or nonlinear regression model is unsuitable.
Cubic smoothing splines arise as the unique real-valued solution function
, with absolutely continuous first derivative and squared-integrable second derivative, which minimizes:
where
is the (optional) weight for the
th observation and
is the smoothing parameter. This criterion consists of two parts: the first measures the fit of the curve, and the second the smoothness of the curve. The value of the smoothing parameter
weights these two aspects; larger values of
give a smoother fitted curve but, in general, a poorer fit. For details of how the cubic spline can be estimated see
Hutchinson and de Hoog (1985) and
Reinsch (1967).
The fitted values,
, and weighted residuals,
, can be written as
for a matrix
. The residual degrees of freedom for the spline is
and the diagonal elements of
,
, are the leverages.
The parameter
can be chosen in a number of ways. The fit can be inspected for a number of different values of
. Alternatively the degrees of freedom for the spline, which determines the value of
, can be specified, or the (generalized) cross-validation can be minimized to give
; see
G10ACF for further details.
G10ABF requires the
to be strictly increasing. If two or more observations have the same
-value then they should be replaced by a single observation with
equal to the (weighted) mean of the
values and weight,
, equal to the sum of the weights. This operation can be performed by
G10ZAF.
The computation is split into three phases.
(i) |
Compute matrices needed to fit spline. |
(ii) |
Fit spline for a given value of . |
(iii) |
Compute spline coefficients. |
When fitting the spline for several different values of
, phase
(i) need only be carried out once and then phase
(ii) repeated for different values of
. If the spline is being fitted as part of an iterative weighted least squares procedure phases
(i) and
(ii) have to be repeated for each set of weights. In either case, phase
(iii) will often only have to be performed after the final fit has been computed.
The algorithm is based on
Hutchinson (1986).
4 References
Hastie T J and Tibshirani R J (1990) Generalized Additive Models Chapman and Hall
Hutchinson M F (1986) Algorithm 642: A fast procedure for calculating minimum cross-validation cubic smoothing splines ACM Trans. Math. Software 12 150–153
Hutchinson M F and de Hoog F R (1985) Smoothing noisy data with spline functions Numer. Math. 47 99–106
Reinsch C H (1967) Smoothing by spline functions Numer. Math. 10 177–183
5 Parameters
- 1: – CHARACTER(1)Input
-
On entry: indicates in which mode the routine is to be used.
- Initialization and fitting is performed. This partial fit can be used in an iterative weighted least squares context where the weights are changing at each call to G10ABF or when the coefficients are not required.
- Fitting only is performed. Initialization must have been performed previously by a call to G10ABF with . This quick fit may be called repeatedly with different values of RHO without re-initialization.
- Initialization and full fitting is performed and the function coefficients are calculated.
Constraint:
, or .
- 2: – CHARACTER(1)Input
-
On entry: indicates whether user-defined weights are to be used.
- User-defined weights should be supplied in WT.
- The data is treated as unweighted.
Constraint:
or .
- 3: – INTEGERInput
-
On entry: , the number of distinct observations.
Constraint:
.
- 4: – REAL (KIND=nag_wp) arrayInput
-
On entry: the distinct and ordered values
, for .
Constraint:
, for .
- 5: – REAL (KIND=nag_wp) arrayInput
-
On entry: the values
, for .
- 6: – REAL (KIND=nag_wp) arrayInput
-
Note: the dimension of the array
WT
must be at least
if
.
On entry: if
,
WT must contain the
weights. Otherwise
WT is not referenced and unit weights are assumed.
Constraint:
if , , for .
- 7: – REAL (KIND=nag_wp)Input
-
On entry: , the smoothing parameter.
Constraint:
.
- 8: – REAL (KIND=nag_wp) arrayOutput
-
On exit: the fitted values,
, for .
- 9: – REAL (KIND=nag_wp) arrayInput/Output
-
On entry: if
,
C must be unaltered from the previous call to G10ABF with
. Otherwise
C need not be set.
On exit: if
,
C contains the spline coefficients. More precisely, the value of the spline at
is given by
, where
and
.
If
or
,
C contains information that will be used in a subsequent call to G10ABF with
.
- 10: – INTEGERInput
-
On entry: the first dimension of the array
C as declared in the (sub)program from which G10ABF is called.
Constraint:
.
-
On exit: the (weighted) residual sum of squares.
- 12: – REAL (KIND=nag_wp)Output
-
On exit: the residual degrees of freedom.
- 13: – REAL (KIND=nag_wp) arrayOutput
-
On exit: the (weighted) residuals,
, for .
- 14: – REAL (KIND=nag_wp) arrayOutput
-
On exit: the leverages,
, for .
- 15: – REAL (KIND=nag_wp) arrayCommunication Array
-
On entry: if
,
COMM must be unaltered from the previous call to G10ABF with
. Otherwise
COMM need not be set.
On exit: if
or
,
COMM contains information that will be used in a subsequent call to G10ABF with
.
- 16: – INTEGERInput/Output
-
On entry:
IFAIL must be set to
,
. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is
.
When the value is used it is essential to test the value of IFAIL on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6 Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
Errors or warnings detected by the routine:
-
On entry, | , |
or | , |
or | , |
or | , or , |
or | or . |
-
On entry, | and at least one element of . |
-
On entry, | , for some , . |
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.8 in the Essential Introduction for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.7 in the Essential Introduction for further information.
Dynamic memory allocation failed.
See
Section 3.6 in the Essential Introduction for further information.
7 Accuracy
Accuracy depends on the value of and the position of the values. The values of and are scaled and is transformed to avoid underflow and overflow problems.
8 Parallelism and Performance
Not applicable.
The time taken by G10ABF is of order .
Regression splines with a small
number of knots can be fitted by
E02BAF and
E02BEF.
10 Example
The data, given by
Hastie and Tibshirani (1990), is the age,
, and C-peptide concentration (pmol/ml),
, from a study of the factors affecting insulin-dependent diabetes mellitus in children. The data is input, reduced to a strictly ordered set by
G10ZAF and a series of splines fit using a range of values for the smoothing parameter,
.
10.1 Program Text
Program Text (g10abfe.f90)
10.2 Program Data
Program Data (g10abfe.d)
10.3 Program Results
Program Results (g10abfe.r)