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 n \times p, where n is the number of observations and p is the number of predictors.

y

A numeric vector of length n, representing the loss values (severity). These are assumed to follow a Gamma distribution with shape parameter alpha and scale parameter \theta = \exp(x^\top \gamma) / \alpha.

k

A numeric vector of length n, representing the number of claims (frequency), assumed to follow a Poisson distribution with mean \mu = x^\top \beta.

lambda

A user-supplied numeric vector of tuning parameters. The function will compute the solution for each value in lambda.

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 folds, cross-validation is performed for all lambda values, and a "vote" is cast for the lambda with the lowest error in that cycle.

nfolds

The number of folds in cross-validation. Default is 5.

...

Additional arguments passed to safe.

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 value that won the most votes (i.e., achieved the minimum cross-validation error most often).

lambda

The full sequence of candidate lambda values provided to the function.

ncvmat

A numeric matrix of dimension rep * the length of lambda, containing the cross-validation errors for each lambda across all rep runs.

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))

[Package SAFEPG version 0.0.1 Index]