View source: R/spline.model.matrix.R
spline.model.matrix | R Documentation |
Creates a design (or model) matrix using the spline.basis
function to expand variables via a spectral spline basis.
spline.model.matrix(object, data, ...)
rowKronecker(X, Y)
object |
a |
data |
a data frame containing the variables referenced in |
... |
additional arguments passed to the |
X |
matrix of dimension |
Y |
matrix of dimension |
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.
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 |
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)
.
Nathaniel E. Helwig <helwig@umn.edu>
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 spline.basis
for details on the spectral spline basis
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.