View source: R/naturalSpline.R
naturalSpline | R Documentation |
Functions naturalSpline()
and nsk()
generate the natural cubic
spline basis functions, the corresponding derivatives or integrals (from the
left boundary knot). Both of them are different from splines::ns()
.
However, for a given model fitting procedure, using different variants of
spline basis functions should result in identical prediction values. The
coefficient estimates of the spline basis functions returned by nsk()
are more interpretable compared to naturalSpline()
or
splines::ns()
.
naturalSpline(
x,
df = NULL,
knots = NULL,
intercept = FALSE,
Boundary.knots = NULL,
trim = 0,
derivs = 0L,
integral = FALSE,
...
)
nsp(
x,
df = NULL,
knots = NULL,
intercept = FALSE,
Boundary.knots = NULL,
trim = 0,
derivs = 0L,
integral = FALSE,
...
)
nsk(
x,
df = NULL,
knots = NULL,
intercept = FALSE,
Boundary.knots = NULL,
trim = 0,
derivs = 0L,
integral = FALSE,
...
)
x |
The predictor variable. Missing values are allowed and will be returned as they are. |
df |
Degree of freedom that equals to the column number of returned
matrix. One can specify |
knots |
The internal breakpoints that define the splines. The default
is |
intercept |
If |
Boundary.knots |
Boundary points at which to anchor the splines. By
default, they are the range of |
trim |
The fraction (0 to 0.5) of observations to be trimmed from each
end of |
derivs |
A nonnegative integer specifying the order of derivatives of
natural splines. The default value is |
integral |
A logical value. The default value is |
... |
Optional arguments that are not used. |
The constructed spline basis functions from naturalSpline()
are
nonnegative within boundary with the second derivatives being zeros at
boundary knots. The implementation utilizes the close-form null space that
can be derived from the recursive formula for the second derivatives of
B-splines. The function nsp()
is an alias of naturalSpline()
to encourage the use in a model formula.
The function nsk()
produces another variant of natural cubic spline
matrix such that only one of the basis functions is nonzero and takes a
value of one at every boundary and internal knot. As a result, the
coefficients of the resulting fit are the values of the spline function at
the knots, which makes it easy to interpret the coefficient estimates. In
other words, the coefficients of a linear model will be the heights of the
function at the knots if intercept = TRUE
. If intercept =
FALSE
, the coefficients will be the change in function value between each
knot. This implementation closely follows the function nsk()
of the
survival package (version 3.2-8). The idea corresponds directly to
the physical implementation of a spline by passing a flexible strip of wood
or metal through a set of fixed points, which is a traditional way to create
smooth shapes for things like a ship hull.
The returned basis matrix can be obtained by transforming the corresponding
B-spline basis matrix with the matrix H
provided in the attribute of
the returned object. Each basis is assumed to follow a linear trend for
x
outside of boundary. A similar implementation is provided by
splines::ns
, which uses QR decomposition to find the null space of
the second derivatives of B-spline basis at boundary knots. See
Supplementray Materials of Wang and Yan (2021) for a more detailed
introduction.
A numeric matrix of length(x)
rows and df
columns if df
is specified or length(knots) + 1 +
as.integer(intercept)
columns if knots
are specified instead.
Attributes that correspond to the arguments specified are returned for
usage of other functions in this package.
bSpline
for B-splines;
mSpline
for M-splines;
iSpline
for I-splines.
library(splines2)
x <- seq.int(0, 1, 0.01)
knots <- c(0.3, 0.5, 0.6)
## naturalSpline()
nsMat0 <- naturalSpline(x, knots = knots, intercept = TRUE)
nsMat1 <- naturalSpline(x, knots = knots, intercept = TRUE, integral = TRUE)
nsMat2 <- naturalSpline(x, knots = knots, intercept = TRUE, derivs = 1)
nsMat3 <- naturalSpline(x, knots = knots, intercept = TRUE, derivs = 2)
op <- par(mfrow = c(2, 2), mar = c(2.5, 2.5, 0.2, 0.1), mgp = c(1.5, 0.5, 0))
plot(nsMat0, ylab = "basis")
plot(nsMat1, ylab = "integral")
plot(nsMat2, ylab = "1st derivative")
plot(nsMat3, ylab = "2nd derivative")
par(op) # reset to previous plotting settings
## nsk()
nskMat <- nsk(x, knots = knots, intercept = TRUE)
plot(nskMat, ylab = "nsk()", mark_knots = "all")
abline(h = 1, col = "red", lty = 3)
## use the deriv method
all.equal(nsMat0, deriv(nsMat1), check.attributes = FALSE)
all.equal(nsMat2, deriv(nsMat0))
all.equal(nsMat3, deriv(nsMat2))
all.equal(nsMat3, deriv(nsMat0, 2))
## a linear model example
fit1 <- lm(weight ~ -1 + nsk(height, df = 4, intercept = TRUE), data = women)
fit2 <- lm(weight ~ nsk(height, df = 3), data = women)
## the knots (same for both fits)
knots <- unlist(attributes(fit1$model[[2]])[c("Boundary.knots", "knots")])
## predictions at the knot points
predict(fit1, data.frame(height = sort(unname(knots))))
unname(coef(fit1)) # equal to the coefficient estimates
## different interpretation when "intercept = FALSE"
unname(coef(fit1)[-1] - coef(fit1)[1]) # differences: yhat[2:4] - yhat[1]
unname(coef(fit2))[-1] # ditto
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.