Learn R Programming

npreg (version 1.1.0)

residuals: Extract Model Residuals

Description

Extracts the residuals from a fit smoothing spline ("ss"), smooth model ("sm"), or generalized smooth model ("gsm") object.

Usage

# S3 method for ss
residuals(object, type = c("working", "response", "deviance", 
                           "pearson", "partial"), ...)

# S3 method for sm residuals(object, type = c("working", "response", "deviance", "pearson", "partial"), ...) # S3 method for gsm residuals(object, type = c("deviance", "pearson", "working", "response", "partial"), ...)

Value

Residuals from object

Arguments

object

an object of class "ss", "sm", or "gsm"

type

type of residuals

...

other arugments (currently ignored)

Author

Nathaniel E. Helwig <helwig@umn.edu>

Details

For objects of class ss and sm
* the working and response residuals are defined as 'observed - fitted'
* the deviance and Pearson residuals multiply the working residuals by sqrt(weights(object))

For objects of class gsm, the residual types are the same as those produced by the residuals.glm function

References

Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S. 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

ss, sm, gsm

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)
res.ss <- residuals(mod.ss)

# smooth model
mod.sm <- sm(y ~ x, knots = 10)
res.sm <- residuals(mod.sm)

# generalized smooth model (family = gaussian)
mod.gsm <- gsm(y ~ x, knots = 10)
res.gsm <- residuals(mod.gsm)

# y = fitted + residuals
mean((y - fitted(mod.ss) - res.ss)^2)
mean((y - fitted(mod.sm) - res.sm)^2)
mean((y - fitted(mod.gsm) - res.gsm)^2)

Run the code above in your browser using DataLab