Type: | Package |
Title: | A Novel SAFE Model for Predicting Climate-Related Extreme Losses |
Version: | 0.0.1 |
Date: | 2025-05-01 |
Maintainer: | Qian Tang <qian-tang@uiowa.edu> |
Description: | The goal of 'SAFEPG' is to predict climate-related extreme losses by fitting a frequency-severity model. It improves predictive performance by introducing a sign-aligned regularization term, which ensures consistent signs for the coefficients across the frequency and severity components. This enhancement not only increases model accuracy but also enhances its interpretability, making it more suitable for practical applications in risk assessment. |
Depends: | R (≥ 3.5.0) |
License: | GPL-2 |
Imports: | stats, Matrix, methods |
Suggests: | knitr, rmarkdown |
VignetteBuilder: | knitr |
Encoding: | UTF-8 |
RoxygenNote: | 7.2.3 |
NeedsCompilation: | yes |
Packaged: | 2025-05-07 00:14:45 UTC; qtang7 |
Author: | Qian Tang [aut, cre], Yikai Zhang [aut], Boxiang Wang [aut] |
Repository: | CRAN |
Date/Publication: | 2025-05-09 09:40:02 UTC |
Extract Model Coefficients from a 'safe' Object
Description
Retrieves the coefficients at specified values of 'lambda' from a fitted 'safe()' model.
Usage
## S3 method for class 'safe'
coef(object, s = NULL, ...)
Arguments
object |
Fitted 'safe()' object. |
s |
Values of the penalty parameter 'lambda' for which coefficients are requested. Defaults to the entire sequence used during the model fit. |
... |
Not used. |
Details
This function extracts coefficients for specified 'lambda' values from a 'safe()' object. If 's', the vector of 'lambda' values, contains values not originally used in the model fitting, the 'coef' function employs linear interpolation between the closest 'lambda' values from the original sequence to estimate coefficients at the new 'lambda' values.
Value
Returns a matrix or vector of coefficients corresponding to the specified 'lambda' values.
See Also
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_val <- 1
fit <- safe(x, y, k, 1, ind_p = c(1, 1, 1, 0, 0))
coef(fit)
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))
Make Predictions from a 'safe' Object
Description
Produces fitted values for new predictor data using a fitted 'safe()' object.
Usage
## S3 method for class 'safe'
predict(object, newx, s = NULL, ...)
Arguments
object |
Fitted 'safe()' object from which predictions are to be derived. |
newx |
Matrix of new predictor values for which predictions are desired. This must be a matrix and is a required argument. |
s |
Values of the penalty parameter 'lambda' for which predictions are requested. Defaults to the entire sequence used during the model fit. |
... |
Not used. |
Details
This function generates predictions at specified 'lambda' values from a fitted 'safe()' object. It is essential to provide a new matrix of predictor values ('newx') at which these predictions are to be made.
Value
Returns the fitted values.
See Also
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_val <- 1
fit <- safe(x, y, k, 1, ind_p = c(1, 1, 1, 0, 0))
set.seed(234)
newx <- matrix(rnorm(n * p), nrow = n, ncol = p)
predict(fit, newx)
Solve the sign-aligned frequency-severity (SAFE) model
Description
This function fits a Poisson-Gamma regression framework that aligns the signs of certain predictors in both
the Poisson frequency component and the Gamma severity component. The solution path is computed at a
sequence of tuning parameter values (lambda
).
Usage
safe(
x,
y,
k,
lambda,
alpha = 1,
eps = 1e-08,
maxit = 1e+05,
eps2 = 1e-08,
ind_p,
int_gam = NULL,
int_beta = NULL
)
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 |
alpha |
The shape parameter for the Gamma distribution (default is 1). |
eps |
Convergence tolerance for updating |
maxit |
An integer specifying the maximum number of iterations (default is |
eps2 |
Convergence tolerance for updating |
ind_p |
A user-provided vector specifying which predictors should share the same sign across frequency and severity. |
int_gam |
Optional numeric vector or matrix providing initial values for |
int_beta |
Optional numeric vector or matrix providing initial values for |
Details
The function uses an **accelerated proximal gradient descent** algorithm to simultaneously estimate
beta
(for the Poisson frequency model) and gamma
(for the Gamma severity model) under
a sign-alignment constraint for selected predictors (controlled by ind_p
).
Value
An object of class safe
, which is a list containing:
beta |
A |
gamma |
A |
lambda |
The (sorted) sequence of |
npass_beta |
Total number of iterations used to update |
npass_gamma |
Total number of iterations used to update |
jerr |
An integer flag for warnings or errors; |
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_val <- 1
fit <- safe(x, y, k, 1, ind_p = c(1, 1, 1, 0, 0))