Learn R Programming

gratia (version 0.9.0)

lp_matrix: Return the linear prediction matrix of a fitted GAM

Description

lp_matrix() is a wrapper to predict(..., type = "lpmatrix") for returning the linear predictor matrix for the model training data (when data = NULL), or user-specified data values supplied via data.

Usage

lp_matrix(model, ...)

# S3 method for gam lp_matrix(model, data = NULL, ...)

Value

The linear prediction matrix is returned as a matrix. The object returned is of class "lp_matrix", which inherits from classes "matrix"

and "array". The special class allows the printing of the matrix to be controlled, which we do by printing the matrix as a tibble.

Arguments

model

a fitted model

...

arguments passed to other methods and predict methods including mgcv::predict.gam() and mgcv::predict.bam()

data

a data frame of values at which to return the linear prediction matrix.

Details

The linear prediction matrix \(\mathbf{X}_p\) is a matrix that maps values of parameters \(\hat{\mathbf{\beta}}_p\) to values on the linear predictor of the model \(\hat{\eta}_p = \mathbf{X}_p \hat{\mathbf{\beta}}_p\). \(\mathbf{X}_p\) is the model matrix where spline covariates have been replaced by the values of the basis functions evaluated at the respective covariates. Parametric covariates are also included.

Examples

Run this code
load_mgcv()
# \dontshow{
op <- options(pillar.sigfig = 3, cli.unicode = FALSE)
# }
df <- data_sim("eg1", seed = 1)
m <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = df)

# linear prediction matrix for observed data
xp <- lp_matrix(m)
## IGNORE_RDIFF_BEGIN
xp
## IGNORE_RDIFF_END

# the object `xp` *is* a matrix
class(xp)
# but we print like a tibble to avoid spamming the R console

# linear predictor matrix for new data set
ds <- data_slice(m, x2 = evenly(x2))
xp <- lp_matrix(m, data = ds)
## IGNORE_RDIFF_BEGIN
xp
## IGNORE_RDIFF_END
# \dontshow{
options(op)
# }

Run the code above in your browser using DataLab