Skip to contents

This function makes predictions from a cross-validated cv.sparsegl() object, using the stored sparsegl.fit object, and the value chosen for lambda.

Usage

# S3 method for cv.sparsegl
predict(
  object,
  newx,
  s = c("lambda.1se", "lambda.min"),
  type = c("link", "response", "coefficients", "nonzero", "class"),
  ...
)

Arguments

object

Fitted cv.sparsegl() object.

newx

Matrix of new values for x at which predictions are to be made. Must be a matrix. This argument is mandatory.

s

Value(s) of the penalty parameter lambda at which coefficients are desired. Default is the single value s = "lambda.1se" stored in the CV object (corresponding to the largest value of lambda such that CV error estimate is within 1 standard error of the minimum). Alternatively s = "lambda.min" can be used (corresponding to the minimum of cross validation error estimate). If s is numeric, it is taken as the value(s) of lambda to be used.

type

Type of prediction required. Type "link" gives the linear predictors for "binomial"; for "gaussian" models it gives the fitted values. Type "response" gives predictions on the scale of the response (for example, fitted probabilities for "binomial"); for "gaussian" type "response" is equivalent to type "link". Type "coefficients" computes the coefficients at the requested values for s. Type "class" applies only to "binomial" models, and produces the class label corresponding to the maximum probability. Type "nonzero" returns a list of the indices of the nonzero coefficients for each value of s.

...

Not used.

Value

A matrix or vector of predicted values.

Examples

n <- 100
p <- 20
X <- matrix(rnorm(n * p), nrow = n)
eps <- rnorm(n)
beta_star <- c(rep(5, 5), c(5, -5, 2, 0, 0), rep(-5, 5), rep(0, (p - 15)))
y <- X %*% beta_star + eps
groups <- rep(1:(p / 5), each = 5)
fit1 <- sparsegl(X, y, group = groups)
cv_fit <- cv.sparsegl(X, y, groups)
predict(cv_fit, newx = X[50:60, ], s = "lambda.min")
#>                s1
#>  [1,]  -0.6017936
#>  [2,] -17.6587573
#>  [3,] -10.1142969
#>  [4,]  -6.3310266
#>  [5,]   6.9781128
#>  [6,] -30.9760711
#>  [7,]   1.6237528
#>  [8,]   5.7211677
#>  [9,]  -5.7791044
#> [10,]  22.9530510
#> [11,] -31.7711507