pbs: Periodic B-Spline Basis for Polynomial Splines

Description Usage Arguments Details Value Examples

Description

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

Usage

1
2
pbs(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'; 'pbs()' then chooses 'df - 1' knots at suitable quantiles of 'x' (which will ignore missing values).

knots

the _internal_ breakpoints that define the spline. The number of internal knots must be greater than or equal to degree polynomial regression. 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 set the period of the perodic B-spline basis(default the range of the data). If both 'knots' and 'Boundary.knots' are supplied, the basis parameters do not depend on 'x'. Data CAN NOT be extended beyond 'Boundary.knots'. Typical Bourday knots are start and end values of period.

Details

This function and documentation is copied from the CRAN package pbs by Shuangcai Wang <swang1@gmail.com>

Value

A matrix of dimension 'length(x) * (df)', where either 'df' was supplied or if 'knots' were supplied, 'df = length(knots) + intercept'. Attributes are returned that correspond to the arguments to 'pbs', and explicitly give the 'knots', 'Boundary.knots' etc for use by 'predict.pbs()'.

pbs()' is based on the function 'spline.des()' in package splines. 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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require(stats); require(graphics); require(splines)
x = seq(1,628)/100
z = rep(seq(1, 314)/100, 2)

pbs(x, df = 5, Boundary.knots = c(0, 2*pi))
pbs(x, knots=c(pi/2, pi, pi*3/2), Boundary.knots = c(0, 2*pi))
#### example of one periodic functions
y= sin(x) + cos(2*x) +
rnorm(628, 0, 0.1) ## x has a period of 2*pi
## df method, need to use large enough df to get a better fit.
## May use max loglik to choose optimal df
summary( fm1 <- lm(y ~ pbs(x, df = 10, Boundary.knots = c(0, 2*pi))) )
plot(x, y, xlab = "x", ylab = "sin(x)", pch="x", cex=.5)

lines(x, predict(fm1, data.frame(x=x, z=z)), col='blue')
lines(x, sin(x) + cos(2*x), col='red')

## knots methods, usually selected at turning points
summary( fm2 <- lm(y ~ pbs(x, knots=c(pi/2, pi, pi*3/2),
          Boundary.knots = c(0, 2*pi)))
)
plot(x, y, xlab = "x", ylab = "sin(x)", pch="x", cex=.5)

lines(x, predict(fm2, data.frame(x=x, z=z)), col='blue')
lines(x, sin(x) + cos(2*x), col='red')

#### example of two periodic functions
x0 = seq(1,628, by=4)/100
z0 = seq(1, 314, by=3)/100
x = rep(x0, each=length(z0))
z = rep(z0, length(x0))
y = sin(x) + cos(2*z) +
   rnorm(length(x), 0, 0.1) ## x has a period of 2*pi and z of pi

summary( fm3 <- lm(y ~ pbs(x, df = 5, Boundary.knots = c(0, 2*pi))+
                       pbs(z, df = 5, Boundary.knots = c(0, pi)))
)

plot(sin(x) + cos(2*3), predict(fm3, data.frame(x=x, z=3)))
summary(sin(x) + cos(2*3)- predict(fm3, data.frame(x=x, z=3)))
## End(Not run)

FluMoDL documentation built on Sept. 13, 2019, 5:10 p.m.