NAG Library Function Document
nag_smooth_spline_fit (g10abc)
1 Purpose
nag_smooth_spline_fit (g10abc) fits a cubic smoothing spline for a given smoothing parameter.
2 Specification
| #include <nag.h> |
| #include <nagg10.h> |
| void |
nag_smooth_spline_fit (Nag_SmoothFitType mode,
Integer n,
const double x[],
const double y[],
const double weights[],
double rho,
double yhat[],
double coeff[],
double *rss,
double *df,
double res[],
double h[],
double comm_ar[],
NagError *fail) |
|
3 Description
nag_smooth_spline_fit (g10abc) 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
nag_smooth_spline_estim (g10acc) for further details.
nag_smooth_spline_fit (g10abc) 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
nag_order_data (g10zac).
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 Arguments
- 1:
mode – Nag_SmoothFitTypeInput
On entry: indicates in which mode the function 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 nag_smooth_spline_fit (g10abc) or when the coefficients are not required.
- Fitting only is performed. Initialization must have been performed previously by a call to nag_smooth_spline_fit (g10abc) 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:
n – IntegerInput
On entry:
, the number of distinct observations.
Constraint:
.
- 3:
x[n] – const doubleInput
On entry: the distinct and ordered values , for .
Constraint:
, for .
- 4:
y[n] – const doubleInput
-
On entry: the values , for .
- 5:
weights[n] – const doubleInput
-
On entry:
weights must contain the
weights, if they are required. Otherwise,
weights must be set to the null pointer
(double *)0.
Constraint:
if
weights are required, then
, for
.
- 6:
rho – doubleInput
On entry: , the smoothing parameter.
Constraint:
.
- 7:
yhat[n] – doubleOutput
-
On exit: the fitted values, , for .
- 8:
coeff[] – doubleInput/Output
On entry: if
,
coeff must be unaltered from the previous call to nag_smooth_spline_fit (g10abc) with
. Otherwise
coeff need not be set.
On exit: if
,
coeff contains the spline coefficients. More precisely, the value of the spline at
is given by
, where
and
.
If
or
,
coeff contains information that will be used in a subsequent call to nag_smooth_spline_fit (g10abc) with
.
-
On exit: the (weighted) residual sum of squares.
- 10:
df – double *Output
On exit: the residual degrees of freedom.
- 11:
res[n] – doubleOutput
-
On exit: the (weighted) residuals, , for .
- 12:
h[n] – doubleOutput
-
On exit: the leverages, , for .
- 13:
comm_ar[] – doubleInput/Output
On entry: if
,
comm_ar must be unaltered from the previous call to nag_smooth_spline_fit (g10abc) with
. Otherwise
comm_ar is used as workspace and need not be set.
On exit: if
or
,
comm_ar contains information that will be used in a subsequent call to nag_smooth_spline_fit (g10abc) with
.
- 14:
fail – NagError *Input/Output
-
The NAG error argument (see
Section 3.6 in the Essential Introduction).
6 Error Indicators and Warnings
- NE_BAD_PARAM
On entry, argument
mode had an illegal value.
- NE_INT_ARG_LT
On entry, .
Constraint: .
- NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact
NAG for assistance.
- NE_NOT_STRICTLY_INCREASING
The sequence
x is not strictly increasing:
,
.
- NE_REAL_ARG_LT
On entry, .
Constraint: .
- NE_REAL_ARRAY_CONS
On entry, .
Constraint: , for .
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.
The time taken by nag_smooth_spline_fit (g10abc) is of order .
Regression splines with a small
number of knots can be fitted by
nag_1d_spline_fit_knots (e02bac) and
nag_1d_spline_fit (e02bec).
9 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
nag_order_data (g10zac) and a series of splines fit using a range of values for the smoothing parameter,
.
9.1 Program Text
Program Text (g10abce.c)
9.2 Program Data
Program Data (g10abce.d)
9.3 Program Results
Program Results (g10abce.r)