Skip to content

natural splines

1 message · Eilers, P. (MStat)

#
Indeed truncated power functions (TPF) are very useful to compute B-splines.
But why take chances with their bad numerical condition? Simple differences
will
turn the TPF basis into a B-spline basis. See the functions at the end. Try

 B = bbase(1:100, 0, 101, 10, 3)
 matplot(B, type = 'b')

to see it work.

Here I use equally spaced knots (as I always do, adding a penalty to tune
smoothness and avoid singularity problems). But with divided differences
this scheme will work for arbitrary knots (if no knots are the same).

Paul Eilers
Department of Medical Statistics
Leiden University Medical Centre


======= R functions ===================

tpower <- function(x, t, p) {
# Truncated p-th power function
    (x - t) ^ p * (x > t)
}

bbase <- function(x, xl, xr, ndx, deg){
# Construct B-spline basis from differences of truncated power functions
# Input
#   x:      x for which to compute basis
#   xl, xr: left and right boundary
#   nseg:   number of B-spline segments between xl and xr
#   deg:    degree (of the polynomial segments) of the B-spline
#
# Paul Eilers, 2003

    dx <- (xr - xl) / ndx
    knots <- seq(xl - deg * dx, xr + deg * dx, by = dx)
    P <- outer(x, knots, tpower, deg)
    n <- dim(P)[2]
    D <- diff(diag(n), differences = deg + 1) / (gamma(deg + 1) * dx ^ deg)
    B <- (-1) ^ (deg + 1) * P %*% t(D)
    B
}

==========================================


On Thu, 8 May 2003 15:07:56 +0100 (BST)
iwhite at staffmail.ed.ac.uk wrote:

            
This is also a vote for using the truncated power basis which is extremely
simple and exceptionally fast for large datasets (see the rcspline.eval
function in the Hmisc package).  With modern matrix arithmetic (as in S),
the collinearity of the bases produced by these simple regression splines is
a moot point.
---
Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help