Last data update: 2014.03.03

R: B-Spline Basis for Polynomial Splines
bsR Documentation

B-Spline Basis for Polynomial Splines

Description

Generate the B-spline basis matrix for a polynomial spline.

Usage

bs(x, df = NULL, knots = NULL, degree = 3, intercept = FALSE,
   Boundary.knots = range(x))

Arguments

x

the predictor variable. Missing values are allowed.

df

degrees of freedom; one can specify df rather than knots; bs() then chooses df-degree (minus one if there is an intercept) knots at suitable quantiles of x (which will ignore missing values). The default, NULL, corresponds to no inner knots, i.e., degree - intercept.

knots

the internal breakpoints that define the spline. The default is NULL, which results in a basis for ordinary polynomial regression. Typical values are the mean or median for one knot, quantiles for more knots. See also Boundary.knots.

degree

degree of the piecewise polynomial—default is 3 for cubic splines.

intercept

if TRUE, an intercept is included in the basis; default is FALSE.

Boundary.knots

boundary points at which to anchor the B-spline basis (default the range of the non-NA data). If both knots and Boundary.knots are supplied, the basis parameters do not depend on x. Data can extend beyond Boundary.knots.

Details

bs is based on the function spline.des. It generates a basis matrix for representing the family of piecewise polynomials with the specified interior knots and degree, evaluated at the values of x. A primary use is in modeling formulas to directly specify a piecewise polynomial term in a model.

When Boundary.knots are set inside range(x), bs() now uses a ‘pivot’ inside the respective boundary knot which is important for derivative evaluation. In R versions <= 3.2.2, the boundary knot itself had been used as pivot, which lead to somewhat wrong extrapolations.

Value

A matrix of dimension c(length(x), df), where either df was supplied or if knots were supplied, df = length(knots) + degree plus one if there is an intercept. Attributes are returned that correspond to the arguments to bs, and explicitly give the knots, Boundary.knots etc for use by predict.bs().

Author(s)

Douglas Bates and Bill Venables. Tweaks by R Core, and a patch fixing extrapolation “outside” Boundary.knots by Trevor Hastie.

References

Hastie, T. J. (1992) Generalized additive models. Chapter 7 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.

See Also

ns, poly, smooth.spline, predict.bs, SafePrediction

Examples

require(stats); require(graphics)
bs(women$height, df = 5)
summary(fm1 <- lm(weight ~ bs(height, df = 5), data = women))

## example of safe prediction
plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
ht <- seq(57, 73, length.out = 200)
lines(ht, predict(fm1, data.frame(height = ht)))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(splines)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/splines/bs.Rd_%03d_medium.png", width=480, height=480)
> ### Name: bs
> ### Title: B-Spline Basis for Polynomial Splines
> ### Aliases: bs
> ### Keywords: smooth
> 
> ### ** Examples
> 
> require(stats); require(graphics)
> bs(women$height, df = 5)
                 1           2           3            4           5
 [1,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 0.000000000
 [2,] 4.534439e-01 0.059857872 0.001639942 0.000000e+00 0.000000000
 [3,] 5.969388e-01 0.203352770 0.013119534 0.000000e+00 0.000000000
 [4,] 5.338010e-01 0.376366618 0.044278426 0.000000e+00 0.000000000
 [5,] 3.673469e-01 0.524781341 0.104956268 0.000000e+00 0.000000000
 [6,] 2.001640e-01 0.595025510 0.204719388 9.110787e-05 0.000000000
 [7,] 9.110787e-02 0.566326531 0.336734694 5.830904e-03 0.000000000
 [8,] 3.125000e-02 0.468750000 0.468750000 3.125000e-02 0.000000000
 [9,] 5.830904e-03 0.336734694 0.566326531 9.110787e-02 0.000000000
[10,] 9.110787e-05 0.204719388 0.595025510 2.001640e-01 0.000000000
[11,] 0.000000e+00 0.104956268 0.524781341 3.673469e-01 0.002915452
[12,] 0.000000e+00 0.044278426 0.376366618 5.338010e-01 0.045553936
[13,] 0.000000e+00 0.013119534 0.203352770 5.969388e-01 0.186588921
[14,] 0.000000e+00 0.001639942 0.059857872 4.534439e-01 0.485058309
[15,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 1.000000000
attr(,"degree")
[1] 3
attr(,"knots")
33.33333% 66.66667% 
 62.66667  67.33333 
attr(,"Boundary.knots")
[1] 58 72
attr(,"intercept")
[1] FALSE
attr(,"class")
[1] "bs"     "basis"  "matrix"
> summary(fm1 <- lm(weight ~ bs(height, df = 5), data = women))

Call:
lm(formula = weight ~ bs(height, df = 5), data = women)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.31764 -0.13441  0.03922  0.11096  0.35086 

Coefficients:
                    Estimate Std. Error t value Pr(>|t|)    
(Intercept)         114.8799     0.2167 530.146  < 2e-16 ***
bs(height, df = 5)1   3.4657     0.4595   7.543 3.53e-05 ***
bs(height, df = 5)2  13.0300     0.3965  32.860 1.10e-10 ***
bs(height, df = 5)3  27.6161     0.4571  60.415 4.70e-13 ***
bs(height, df = 5)4  40.8481     0.3866 105.669 3.09e-15 ***
bs(height, df = 5)5  49.1296     0.3090 158.979  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.2276 on 9 degrees of freedom
Multiple R-squared:  0.9999,	Adjusted R-squared:  0.9998 
F-statistic: 1.298e+04 on 5 and 9 DF,  p-value: < 2.2e-16

> 
> ## example of safe prediction
> plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
> ht <- seq(57, 73, length.out = 200)
> lines(ht, predict(fm1, data.frame(height = ht)))
Warning message:
In bs(height, degree = 3L, knots = c(62.6666666666667, 67.3333333333333 :
  some 'x' values beyond boundary knots may cause ill-conditioned bases
> ## Don't show: 
> ## Consistency:
> x <- c(1:3, 5:6)
> stopifnot(identical(bs(x), bs(x, df = 3)),
+           identical(bs(x, df = 4), bs(x, df = 4, knots = NULL)), # not true till 2.15.2
+           !is.null(kk <- attr(bs(x), "knots")), # not true till 1.5.1
+           length(kk) == 0)
> ## End(Don't show)
> 
> 
> 
> 
> dev.off()
null device 
          1 
>