G10ABF (PDF version)
G10 Chapter Contents
G10 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G10ABF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

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 n observations (xi, yi), for i=1,2,,n. 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 f, with absolutely continuous first derivative and squared-integrable second derivative, which minimizes:
i=1nwiyi-fxi2+ρ -fx2dx,  
where wi is the (optional) weight for the ith 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, y^ = y^1,y^2,,y^nT , and weighted residuals, ri, can be written as
y^=Hy  and  ri=wiyi-y^i  
for a matrix H. The residual degrees of freedom for the spline is traceI-H and the diagonal elements of H, hii, 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 xi to be strictly increasing. If two or more observations have the same xi-value then they should be replaced by a single observation with yi equal to the (weighted) mean of the y values and weight, wi, 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:     MODE – CHARACTER(1)Input
On entry: indicates in which mode the routine is to be used.
MODE='P'
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.
MODE='Q'
Fitting only is performed. Initialization must have been performed previously by a call to G10ABF with MODE='P'. This quick fit may be called repeatedly with different values of RHO without re-initialization.
MODE='F'
Initialization and full fitting is performed and the function coefficients are calculated.
Constraint: MODE='P', 'Q' or 'F'.
2:     WEIGHT – CHARACTER(1)Input
On entry: indicates whether user-defined weights are to be used.
WEIGHT='W'
User-defined weights should be supplied in WT.
WEIGHT='U'
The data is treated as unweighted.
Constraint: WEIGHT='W' or 'U'.
3:     N – INTEGERInput
On entry: n, the number of distinct observations.
Constraint: N3.
4:     XN – REAL (KIND=nag_wp) arrayInput
On entry: the distinct and ordered values xi, for i=1,2,,n.
Constraint: Xi<Xi+1, for i=1,2,,n-1.
5:     YN – REAL (KIND=nag_wp) arrayInput
On entry: the values yi, for i=1,2,,n.
6:     WT* – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least N if WEIGHT='W'.
On entry: if WEIGHT='W', WT must contain the n weights. Otherwise WT is not referenced and unit weights are assumed.
Constraint: if WEIGHT='W', WTi>0.0, for i=1,2,,n.
7:     RHO – REAL (KIND=nag_wp)Input
On entry: ρ, the smoothing parameter.
Constraint: RHO0.0.
8:     YHATN – REAL (KIND=nag_wp) arrayOutput
On exit: the fitted values, y^i, for i=1,2,,n.
9:     CLDC3 – REAL (KIND=nag_wp) arrayInput/Output
On entry: if MODE='Q', C must be unaltered from the previous call to G10ABF with MODE='P'. Otherwise C need not be set.
On exit: if MODE='F', C contains the spline coefficients. More precisely, the value of the spline at t is given by Ci3 × d + Ci2 × d + Ci1 × d + y^i , where xit<xi+1 and d=t-xi.
If MODE='P' or 'Q', C contains information that will be used in a subsequent call to G10ABF with MODE='Q'.
10:   LDC – INTEGERInput
On entry: the first dimension of the array C as declared in the (sub)program from which G10ABF is called.
Constraint: LDCN-1.
11:   RSS – REAL (KIND=nag_wp)Output
On exit: the (weighted) residual sum of squares.
12:   DF – REAL (KIND=nag_wp)Output
On exit: the residual degrees of freedom.
13:   RESN – REAL (KIND=nag_wp) arrayOutput
On exit: the (weighted) residuals, ri, for i=1,2,,n.
14:   HN – REAL (KIND=nag_wp) arrayOutput
On exit: the leverages, hii, for i=1,2,,n.
15:   COMM9×N+14 – REAL (KIND=nag_wp) arrayCommunication Array
On entry: if MODE='Q', COMM must be unaltered from the previous call to G10ABF with MODE='P'. Otherwise COMM need not be set.
On exit: if MODE='P' or 'Q', COMM contains information that will be used in a subsequent call to G10ABF with MODE='Q'.
16:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. 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 -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL=1
On entry,N<3,
orLDC<N-1,
orRHO<0.0,
orMODE'Q', 'P' or 'F',
orWEIGHT'W' or 'U'.
IFAIL=2
On entry,WEIGHT='W' and at least one element of WT0.0.
IFAIL=3
On entry,XiXi+1, for some i, i=1,2,,n-1.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
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 x values. The values of xi-xi-1 and wi are scaled and ρ is transformed to avoid underflow and overflow problems.

8  Parallelism and Performance

Not applicable.

9  Further Comments

The time taken by G10ABF is of order n.
Regression splines with a small <n number of knots can be fitted by E02BAF and E02BEF.

10  Example

The data, given by Hastie and Tibshirani (1990), is the age, xi, and C-peptide concentration (pmol/ml), yi, 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)

GnuplotProduced by GNUPLOT 4.6 patchlevel 3 3 3.5 4 4.5 5 5.5 6 6.5 7 0 2 4 6 8 10 12 14 16 C-peptide concentration (pmol/ml) Age (years) Example Program Cubic Smoothing Spline Study of the factors affecting insulin-dependent diabetes mellitus in children Hastie and Tibshirani (1990) gnuplot_plot_1 raw data gnuplot_plot_2 ρ = 1 gnuplot_plot_3 ρ = 10 gnuplot_plot_4 ρ = 100

G10ABF (PDF version)
G10 Chapter Contents
G10 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015