NAG FL Interface
g10acf (fit_spline_parest)
1
Purpose
g10acf estimates the values of the smoothing parameter and fits a cubic smoothing spline to a set of data.
2
Specification
Fortran Interface
Subroutine g10acf ( |
method, weight, n, x, y, wt, yhat, c, ldc, rss, df, res, h, crit, rho, u, tol, maxcal, wk, ifail) |
Integer, Intent (In) |
:: |
n, ldc, maxcal |
Integer, Intent (Inout) |
:: |
ifail |
Real (Kind=nag_wp), Intent (In) |
:: |
x(n), y(n), wt(*), u, tol |
Real (Kind=nag_wp), Intent (Inout) |
:: |
c(ldc,3), crit |
Real (Kind=nag_wp), Intent (Out) |
:: |
yhat(n), rss, df, res(n), h(n), rho, wk(7*(n+2)) |
Character (1), Intent (In) |
:: |
method, weight |
|
C Header Interface
#include <nag.h>
void |
g10acf_ (const char *method, const char *weight, const Integer *n, const double x[], const double y[], const double wt[], double yhat[], double c[], const Integer *ldc, double *rss, double *df, double res[], double h[], double *crit, double *rho, const double *u, const double *tol, const Integer *maxcal, double wk[], Integer *ifail, const Charlen length_method, const Charlen length_weight) |
|
C++ Header Interface
#include <nag.h> extern "C" {
void |
g10acf_ (const char *method, const char *weight, const Integer &n, const double x[], const double y[], const double wt[], double yhat[], double c[], const Integer &ldc, double &rss, double &df, double res[], double h[], double &crit, double &rho, const double &u, const double &tol, const Integer &maxcal, double wk[], Integer &ifail, const Charlen length_method, const Charlen length_weight) |
}
|
The routine may be called by the names g10acf or nagf_smooth_fit_spline_parest.
3
Description
For a set of observations , for , the spline provides a flexible smooth function for situations in which a simple polynomial or nonlinear regression model is not suitable.
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 fitted 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 estimated in a number of ways.
-
(i)The degrees of freedom for the spline can be specified, i.e., find such that for given .
-
(ii)Minimize the cross-validation (CV), i.e., find such that the CV is minimized, where
-
(iii)Minimize the generalized cross-validation (GCV), i.e., find such that the GCV is minimized, where
g10acf 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 algorithm is based on
Hutchinson (1986).
c05azf is used to solve for
given
and the method of
e04abf/e04aba is used to minimize the GCV or CV.
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
Arguments
-
1:
– Character(1)
Input
-
On entry: indicates whether the smoothing parameter is to be found by minimization of the CV or GCV functions, or by finding the smoothing parameter corresponding to a specified degrees of freedom value.
- Cross-validation is used.
- The degrees of freedom are specified.
- Generalized cross-validation is used.
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:
– Integer
Input
-
On entry: , the number of observations.
Constraint:
.
-
4:
– Real (Kind=nag_wp) array
Input
-
On entry: the distinct and ordered values
, for .
Constraint:
, for .
-
5:
– Real (Kind=nag_wp) array
Input
-
On entry: the values
, for .
-
6:
– Real (Kind=nag_wp) array
Input
-
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) array
Output
-
On exit: the fitted values,
, for .
-
8:
– Real (Kind=nag_wp) array
Output
-
On exit: the spline coefficients. More precisely, the value of the spline approximation at is given by , where and .
-
9:
– Integer
Input
-
On entry: the first dimension of the array
c as declared in the (sub)program from which
g10acf is called.
Constraint:
.
-
On exit: the (weighted) residual sum of squares.
-
11:
– Real (Kind=nag_wp)
Output
-
On exit: the residual degrees of freedom. If this will be to the required accuracy.
-
12:
– Real (Kind=nag_wp) array
Output
-
On exit: the (weighted) residuals,
, for .
-
13:
– Real (Kind=nag_wp) array
Output
-
On exit: the leverages,
, for .
-
14:
– Real (Kind=nag_wp)
Input/Output
-
On entry: if
, the required degrees of freedom for the spline.
If
or
,
crit need not be set.
Constraint:
.
On exit: if
, the value of the cross-validation, or if
, the value of the generalized cross-validation function, evaluated at the value of
returned in
rho.
-
15:
– Real (Kind=nag_wp)
Output
-
On exit: the smoothing parameter, .
-
16:
– Real (Kind=nag_wp)
Input
-
On entry: the upper bound on the smoothing parameter. If
,
will be used instead. See
Section 9 for details on how this argument is used.
-
17:
– Real (Kind=nag_wp)
Input
-
On entry: the accuracy to which the smoothing parameter
rho is required.
tol should preferably be not much less than
, where
is the
machine precision. If
,
will be used instead.
-
18:
– Integer
Input
-
On entry: the maximum number of spline evaluations to be used in finding the value of . If , will be used instead.
-
19:
– Real (Kind=nag_wp) array
Workspace
-
-
20:
– Integer
Input/Output
-
On entry:
ifail must be set to
,
. If you are unfamiliar with this argument you should refer to
Section 4 in the Introduction to the NAG Library FL Interface 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 argument, 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, .
Constraint: if , .
On entry, .
Constraint: if , .
On entry, and .
Constraint: .
On entry,
method is not valid:
.
On entry, .
Constraint: .
On entry, .
Constraint: or .
-
On entry, at least one element of .
-
On entry,
x is not a strictly ordered array.
-
For the specified degrees of freedom, : .
-
Accuracy of
tol cannot be achieved:
.
-
maxcal iterations have been performed.
-
Optimum value of
rho lies above
u:
.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 7 in the Introduction to the NAG Library FL Interface for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 8 in the Introduction to the NAG Library FL Interface for further information.
Dynamic memory allocation failed.
See
Section 9 in the Introduction to the NAG Library FL Interface for further information.
7
Accuracy
When minimizing the cross-validation or generalized cross-validation, the error in the estimate of should be within . When finding for a fixed number of degrees of freedom the error in the estimate of should be within .
Given the value of , the accuracy of the fitted spline 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
g10acf is not threaded in any implementation.
The time to fit the spline for a given value of is of order .
When finding the value of
that gives the required degrees of freedom, the algorithm examines the interval
to
u. For small degrees of freedom the value of
can be large, as in the theoretical case of two degrees of freedom when the spline reduces to a straight line and
is infinite. If the CV or GCV is to be minimized then the algorithm searches for the minimum value in the interval
to
u. If the function is decreasing in that range then the boundary value of
u will be returned. In either case, the larger the value of
u the more likely is the interval to contain the required solution, but the process will be less efficient.
Regression splines with a small
number of knots can be fitted by
e02baf and
e02bef.
10
Example
This example uses the data given by
Hastie and Tibshirani (1990), which consists of 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 spline with
degrees of freedom is fitted by
g10acf. The fitted values and residuals are printed.
10.1
Program Text
10.2
Program Data
10.3
Program Results