spline.model.matrix: Construct Design Matrices via Spectral Splines

View source: R/spline.model.matrix.R

spline.model.matrixR Documentation

Construct Design Matrices via Spectral Splines

Description

Creates a design (or model) matrix using the spline.basis function to expand variables via a spectral spline basis.

Usage

spline.model.matrix(object, data, ...)

rowKronecker(X, Y)

Arguments

object

a formula or terms object describing the fit model

data

a data frame containing the variables referenced in object

...

additional arguments passed to the spline.basis function, e.g., df, knots, m, etc. Arguments must be passed as a named list, see Examples.

X

matrix of dimension n \times p

Y

matrix of dimension n \times q

Details

This is a reproduction of the rk.model.matrix function in the grpnet package (Helwig, 2024b).

Designed to be a more flexible alternative to the model.matrix function. The spline.basis function is used to construct a marginal basis for each variable that appears in the input object. Tensor product interactions are formed by taking a rowwise Kronecker product of marginal basis matrices. Interactions of any order are supported using standard formulaic conventions, see Note.

Value

The design matrix corresponding to the input formula and data, which has the following attributes:

assign

an integer vector with an entry for each column in the matrix giving the term in the formula which gave rise to the column

term.labels

a character vector containing the labels for each of the terms in the model

knots

a named list giving the knots used for each variable in the formula

m

a named list giving the penalty order used for each variable in the formula

periodic

a named list giving the periodicity used for each variable in the formula

xlev

a named list giving the factor levels used for each variable in the formula

Note

For formulas of the form y ~ x + z, the constructed model matrix has the form cbind(spline.basis(x), spline.basis(z)), which simply concatenates the two marginal basis matrices. For formulas of the form y ~ x : z, the constructed model matrix has the form rowKronecker(spline.basis(x), spline.basis(z)), where rowKronecker denotes the row-wise kronecker product. The formula y ~ x * z is a shorthand for y ~ x + z + x : z, which concatenates the two previous results. Unless it is suppressed (using 0+), the first column of the basis will be a column of ones named (Intercept).

Author(s)

Nathaniel E. Helwig <helwig@umn.edu>

References

Helwig, N. E. (2021). Spectrally sparse nonparametric regression via elastic net regularized smoothers. Journal of Computational and Graphical Statistics, 30(1), 182-191. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1080/10618600.2020.1806855")}

Helwig, N. E. (2024a). Precise tensor product smoothing via spectral splines. Stats, 7(1), 34-53, \Sexpr[results=rd]{tools:::Rd_expr_doi("10.3390/stats7010003")}

Helwig, N. E. (2024b). grpnet: Group Elastic Net Regularized GLMs and GAMs. R package version 0.4. \Sexpr[results=rd]{tools:::Rd_expr_doi("https://doi.org/10.32614/CRAN.package.grpnet")}

See Also

See spline.basis for details on the spectral spline basis

Examples

# load 'gammi' package
library(gammi)

# load data
data(exam)

# header of data
head(exam)

# make basis matrix 
x <- spline.model.matrix(Exam.score ~ ., data = exam)

# check dimension (= 3435 by 178)
dim(x)

# check term labels
attr(x, "term.labels")

# check which columns of x belong to which terms
attr(x, "assign")             # note: 0 = (Intercept)

gammi documentation built on April 4, 2025, 4:48 a.m.