make_basis_fct: Generate basis function

View source: R/basis_functions.R

make_basis_fctR Documentation

Generate basis function

Description

Method for generating a 'basis function' function, that outputs a functional basis. All credit goes to Lar Lau and his pavpop package.

Usage

make_basis_fct(kts = NULL, df = NULL, type = "B-spline",
  intercept = TRUE, control = list(), ...)

Arguments

kts

a sequence of increasing points specifying the placement of the knots.

df

degrees of freedom of the spline basis. Knots are chosen equidistantly.

type

the type of basis function you want. Currently supported choices are 'B-spline', 'increasing', 'Fourier' and 'constant'. 'intercept' is equivalent to 'constant'. See details for more information.

intercept

logical. Should the basis include an intercept? Only used for types 'B-spline' and 'increasing'; all other types include intercept.

control

list of control parameters. Most importantly is boundary which contains boundary points for a B-spline basis. See details for more options.

Details

The control argument takes a list with the following entries order and constraints and sparse

boundary

boundary knots for the basis spline.

order

order of the spline, if NULL, B-splines have order 4 (cubic spline) and I-splines (type = 'increasing') have order 3.

constraints

positivity constraints, if set to 'positive', only positive weights are allowed

sparse

logical. Should sparse matrices be used?

type 'Fourier' calls make_fourier_basis with make_fourier_basis(kts, df

See Also

make_fourier_basis

Examples

# Basis function knots
kts <- seq(0, 1, length = 12)[2:11]

# Construct B-spline basis function
basis_fct <- make_basis_fct(kts = kts, control = list(boundary = c(0, 1)))

# Evaluation points
t <- seq(0, 1, length = 100)
A <- basis_fct(t)
plot(t, t, type = 'n', ylim = range(A))
for (i in 1:ncol(A)) lines(t, A[, i], col = rainbow(ncol(A))[i])

# Evaluate derivatives
Ad <- basis_fct(t, TRUE)
plot(t, t, type = 'n', ylim = range(Ad))
for (i in 1:ncol(A)) lines(t, Ad[, i], col = rainbow(ncol(Ad))[i])

# Construct I-spline
# Knots should contain the left and right endpoints
kts_inc <- seq(0, 1, length = 10)
basis_fct_inc <- make_basis_fct(kts = kts_inc, type = 'increasing')
A_inc <- basis_fct_inc(t)
plot(t, t, type = 'n', ylim = range(A_inc))
for (i in 1:ncol(A_inc)) lines(t, A_inc[, i], col = rainbow(ncol(A))[i])

# Evaluate derivatives
Ad_inc <- basis_fct_inc(t, deriv = TRUE)
plot(t, t, type = 'n', ylim = range(Ad_inc))
for (i in 1:ncol(Ad_inc)) lines(t, Ad_inc[, i], col = rainbow(ncol(Ad))[i])

# Simulate data
y <- t^2 * sin(8 * t) + t
plot(t, y, type = 'l', lwd = 2, lty = 2)

# Add noise to data
y <- y + rnorm(length(y), sd = 0.1)
points(t, y, pch = 19, cex = 0.5)

# Fit B-spline to data assuming iid. noise
weights <- spline_weights(y, t, basis_fct = basis_fct)
lines(t, A %*% weights, col = 'red', lwd = 2)

# Fit increasing spline
pos_weights <- spline_weights(y, t, basis_fct = basis_fct_inc)
lines(t, A_inc %*% pos_weights, col = 'blue', lwd = 2)

naolsen/simm.fda documentation built on June 28, 2022, 2:41 a.m.