Coding linear splines

Attention conservation notice: anyone who would actually use this could just sit down and do the algebra almost as quickly.

The best-known splines are cubic: a cubic spline with knots $$x_1,\;x_2,\dots,\;x_m$$ is a piecewise-cubic polynomial $$f(x)$$ where $$f$$, $$f’$$, and $$f’’$$ are continuous at the knots.  The name is from the engineer’s drafting tool, a flexible metal strip that – in the infinitely-thin, uniformly flexible asymptote – will form a curve held down at the knots and otherwise minimising bending energy $$\int f’’(x)^2\,dx$$ to give a cubic spline.

Polynomial splines of degree $$k$$ generalise the cubic spline: they are piecewise polynomial, with $$f$$ and $$k-1$$ derivatives continuous at the knots. They have the nice feature of being expressible as linear combinations of a set of basis functions, so that the best-fitting spline with a given set of knots can be computed by ordinary regression. The degenerate case with $$k=1$$ is the linear spline, a continuous piecewise-linear function[1].

Linear splines don’t fit as well as quadratic or cubic, and don’t look as pretty: they have visible corners at the knots.  However, they fit a lot better than step functions, and the linear coefficients are easily interpreted.

There are two useful parametrisations for linear regression spline bases. In the first, the coefficients are the slopes in the segments $$[-\infty,\,x_1]$$, $$[x_1,x_2]$$ and so on.  Alternatively, we can take the first coefficient to be the slope in $$[-\infty, x_1]$$, the second to be the difference in slope at $$x_1$$, the third to be the difference in slope at $$x_2$$, and so on.

For the first parametrisation, the basis is

> z1(t) = min(t, x1)
> z2(t) = max(x1, min(t, x2))
> z3(t) = max(x_2, min(t, x3))  

and so on

For the second, the basis is

> z1(t) = t
> z2(t) = max(0, t-x1))
> z3(t) = max(0, t-x2))  

and so on

Neither of these is the b-spline basis given by the R function bs(), but both are probably more useful in practice.

[1] One could make a case for step functions as 0-degree splines, where 0 of $$f$$, $$f’$$, $$f’’$$,$$\dots$$ are continuous. Mathematicians might agree or might think this was trolling.