Compute fitted values for a model fitted by rcv.glmnet.

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

Arguments

object

rcv.glmnet object.

newx

matrix, of new values for x at which predictions are to be made.

s

character/numeric, value(s) of the penality parameter lambda. See glmnet::predict.cv.glmnet() for details.

type

character, type of prediction. For "survival" the predicted survival is returned for each timepoint in times. All other types are passed to predict.cv.glmnet See glmnet::predict.cv.glmnet() for details.

times

numeric, vector of times, the returned matrix will contain one row for each time. See survival::summary.survfit() for details.

...

further arguments passed to predict.cv.glmnet.

Value

The object returned depends on the ... arguments. See glmnet::predict.cv.glmnet() for details. For type = "survival" the returned value is a matrix with one row per times element and one column for each row in newx.

Author

Sebastian Gibb

Examples

# Example adapted from ?"glmnet::cv.glmnet"
set.seed(10101)
n <- 500
p <- 30
nzc <- trunc(p / 10)
x <- matrix(rnorm(n * p), n, p)
beta <- rnorm(nzc)
fx <- x[, seq(nzc)] %*% beta / 3
hx <- exp(fx)
ty <- rexp(n, hx)
tcens <- rbinom(n = n, prob = 0.3, size = 1)  # censoring indicator
# y <- Surv(ty, 1-tcens) with library("survival")
y <- cbind(time = ty, status = 1 - tcens)
# nrepcv should usually be higher but to keep the runtime of the example low
# we choose 2 here
rcvob <- rcv.glmnet(x, y, family = "cox", nrepcv = 2, nfolds = 3)
predict(
    rcvob,
    newx = x[1:5,], x = x, y = survival::Surv(y[, "time"], y[, "status"]),
    type = "survival", times = c(0, 7), s = "lambda.1se"
)
#>              1          2           3           4          5
#> [1,] 1.0000000 1.00000000 1.000000000 1.000000000 1.00000000
#> [2,] 0.1447802 0.04872118 0.001133545 0.006851513 0.01952228