Learn R Programming

pense (version 1.2.9)

residuals.pense: Extract Residuals from a Fitted Penalized Elastic-Net S/MM-estimator

Description

Extract Residuals from a Fitted Penalized Elastic-Net S/MM-estimator

Usage

# S3 method for pense
residuals(object, lambda, exact = FALSE, correction = TRUE, ...)

Arguments

object

a PENSE or PENSEM estimate to extract the residuals from.

lambda

the value of the penalty parameter. Default is to use the optimal lambda object$lambda_opt.

exact

if the lambda is not part of the lambda grid, should the estimates be obtained by linear interpolation between the nearest lambda values (default) or computed exactly.

correction

should a correction factor be applied to the EN estimate? See elnet for details on the applied correction.

...

currently ignored.

Value

a numeric vector of residuals for the given lambda.

Examples

Run this code
# NOT RUN {
# Generate data with highly correlated groups of variables and some outliers
set.seed(12345)
n <- 50
n_out <- 3
p <- 20
x <- 1 + matrix(rnorm(n * p), ncol = p)
x[, 2] <- x[, 1] + rnorm(n, sd = 0.01)
x[, 3] <- x[, 1] + rnorm(n, sd = 0.01)
x[, 5] <- x[, 4] + rnorm(n, sd = 0.01)
x[, 6] <- x[, 4] + rnorm(n, sd = 0.01)

y <- x %*% c(rep(c(2, 5), each = 3), numeric(p - 6)) + rnorm(n)

y[seq_len(n_out)] <- rnorm(n_out, -100, sd = 3)

# Compute the PENSE estimator
set.seed(1234)
est_en <- pense(x, y, alpha = 0.5, warm_reset = 1, cv_k = 3)

# From the fitted model we can extract the coefficients, fitted values, and
# residuals at the "optimal" lambda as chosen by CV.
coef(est_en) # Extract coefficients
predict(est_en) # Extract fitted values
predict(est_en, newdata = x) # Predict values
residuals(est_en) # Extract residuals

# We can also request the coefficients/predicitons/residuals at another lambda.
# If the requested lambda is not in the original lambda grid, the methods
# will approximate the coefficient vector by linear interpolation of
# the solutions at the surrounding lambda values.
coef(est_en, lambda = 5)

# If the exact solution is needed, this can be requested
coef(est_en, lambda = 5, exact = TRUE)
residuals(est_en, lambda = 5, exact = TRUE)
# }

Run the code above in your browser using DataLab