eccv.safe {SAFEPG} | R Documentation |
Cross-validation for selecting the tuning parameter of the SAFE model
Description
Performs "electoral college" cross-validation for the safe
function. Unlike a standard
cross-validation approach, this method repeats the random partitioning into folds multiple times
(controlled by rep
), then selects the tuning parameter lambda
via a majority vote
across these repeated cross-validation runs.
This function is largely similar [glmnet::cv.glmnet()].
Usage
eccv.safe(x, y, k, lambda, ind_p, rep = 24, nfolds = 5L, ...)
Arguments
x |
A numeric matrix of dimensions |
y |
A numeric vector of length |
k |
A numeric vector of length |
lambda |
A user-supplied numeric vector of tuning parameters. The function will compute the solution
for each value in |
ind_p |
A user-provided index or indicator specifying which predictors should share the same sign across frequency and severity. |
rep |
The number of repeated cross-validation cycles (default is 24). In each cycle, observations
are randomly assigned to |
nfolds |
The number of folds in cross-validation. Default is 5. |
... |
Additional arguments passed to |
Details
The function computes the average cross-validation error and reports the best lambda
that achieves the
smallest cross-validation error.
Value
An object of class "safe"
, which is a list containing:
lambda.min |
The tuning parameter |
lambda |
The full sequence of candidate |
ncvmat |
A numeric matrix of dimension |
Examples
set.seed(1)
n <- 100
p <- 5
x <- matrix(rnorm(n * p), nrow = n, ncol = p)
beta_true <- rep(0.1, 5)
gamma_true <- c(rep(1, 3), -1, -1)
mu <- x %*% beta_true
k <- rpois(n, lambda = exp(mu))
alpha_val <- 1
theta <- exp(x %*% gamma_true) / alpha_val
y <- rgamma(n, shape = alpha_val, scale = theta)
lambda_seq <- 10^seq(2, -8, length.out = 5)
fit <- eccv.safe(x, y, k, lambda=lambda_seq, ind_p = c(1, 1, 1, 0, 0))