Learn R Programming

gammi (version 0.2)

spline.model.matrix: 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)

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

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\)

Author

Nathaniel E. Helwig <helwig@umn.edu>

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.

References

Helwig, N. E. (2021). Spectrally sparse nonparametric regression via elastic net regularized smoothers. Journal of Computational and Graphical Statistics, 30(1), 182-191. 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, tools:::Rd_expr_doi("10.3390/stats7010003")

Helwig, N. E. (2024b). grpnet: Group Elastic Net Regularized GLMs and GAMs. R package version 0.4. 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

Run this code
# 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)

Run the code above in your browser using DataLab