Learn R Programming

npreg (version 1.1.0)

model.matrix: Construct Design Matrix for Fit Model

Description

model.matrix returns the design (or model) matrix used by the input object to produce the fitted values (for objects of class ss or sm) or the linear predictors (for objects of class gsm).

Usage

# S3 method for ss
model.matrix(object, ...)

# S3 method for sm model.matrix(object, ...)

# S3 method for gsm model.matrix(object, ...)

Value

The design matrix that is post-multiplied by the coefficients to produce the fitted values (or linear predictors).

Arguments

object

an object of class ss, sm, or gsm

...

additional arguments (currently ignored)

Author

Nathaniel E. Helwig <helwig@umn.edu>

Details

For ss objects, the basis.poly function is used to construct the design matrix.

For sm objects, the predict.sm function with option design = TRUE is used to construct the design matrix.

For gsm objects, the predict.gsm function with option design = TRUE is used to construct the design matrix.

References

Chambers, J. M. (1992) Data for models. Chapter 3 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.

Helwig, N. E. (2020). Multiple and Generalized Nonparametric Regression. In P. Atkinson, S. Delamont, A. Cernat, J. W. Sakshaug, & R. A. Williams (Eds.), SAGE Research Methods Foundations. tools:::Rd_expr_doi("10.4135/9781526421036885885")

See Also

basis.poly for the smoothing spline basis

predict.sm for predicting from smooth models

predict.gsm for predicting from generalized smooth models

Examples

Run this code
# generate data
set.seed(1)
n <- 100
x <- seq(0, 1, length.out = n)
fx <- 2 + 3 * x + sin(2 * pi * x)
y <- fx + rnorm(n, sd = 0.5)

# smoothing spline
mod.ss <- ss(x, y, nknots = 10)
X.ss <- model.matrix(mod.ss)
mean((mod.ss$y - X.ss %*% mod.ss$fit$coef)^2)


# smooth model
mod.sm <- sm(y ~ x, knots = 10)
X.sm <- model.matrix(mod.sm)
mean((mod.sm$fitted.values - X.sm %*% mod.sm$coefficients)^2)

# generalized smooth model (family = gaussian)
mod.gsm <- gsm(y ~ x, knots = 10)
X.gsm <- model.matrix(mod.gsm)
mean((mod.gsm$linear.predictors - X.gsm %*% mod.gsm$coefficients)^2)

Run the code above in your browser using DataLab