Skip to contents

Generates various visualizations for a fitted cross-validated parity regression model object. It supports plotting estimated coefficients, risk contributions, coefficient paths, and cross-validation error curves based on the specified plot_type.

Usage

# S3 method for class 'cv.savvyPR'
plot(
  x,
  plot_type = c("estimated_coefficients", "risk_contributions", "cv_coefficients",
    "cv_errors"),
  label = TRUE,
  xvar = c("norm", "lambda", "dev", "val"),
  max_vars_per_plot = 10,
  ...
)

Arguments

x

A fitted model object of class "cv.savvyPR" returned by cv.savvyPR.

plot_type

Character string specifying the type of plot to generate. Can be "estimated_coefficients", "risk_contributions", "cv_coefficients", or "cv_errors". Defaults to "estimated_coefficients".

label

Logical; if TRUE, labels are added based on the plot type:

cv_coefficients

Variable names are added to the coefficient paths.

risk_contributions

Numeric labels are added above the bars.

estimated_coefficients

Numeric values are added to the coefficient plot.

cv_errors

Numeric labels are added to the optimal error point.

Default is TRUE.

xvar

Character string specifying the x-axis variable for plotting coefficient paths. Options are "norm", "lambda", "dev", and "val". This argument is only used when plot_type = "cv_coefficients".

max_vars_per_plot

Integer specifying the maximum number of variables to plot per panel. Cannot exceed 10. Default is 10. This argument is only used when plot_type = "cv_coefficients".

...

Additional arguments passed to the underlying ggplot function.

Value

A ggplot object representing the requested plot.

Details

Plot for a Cross-Validated Parity Regression Model

This function offers four types of plots, depending on the value of plot_type:

Estimated Coefficients

Generates a line plot with points for the estimated coefficients of the optimally tuned cross-validated regression model. If an intercept term is included, it will be labeled as beta_0; otherwise, coefficients are labeled sequentially based on the covariates. If label = TRUE, numeric values are displayed on the plot. This plot helps visualize the contribution of each predictor variable to the model.

Risk Contributions

Generates two bar plots, one for the optimization variables (weights or target values) and one for risk contributions, from the risk parity model. If label = TRUE, numeric labels are added above the bars for clarity.

Coefficient Paths

Generates a plot showing the coefficient paths against the selected x-axis variable (val, lambda, norm, or dev) depending on the model type:

  • PR1/PR2: Plots coefficient paths against log(val) values.

  • PR3: Plots coefficient paths against log(lambda) values.

Invalid combinations of model_type and xvar will result in an error:

  • PR1/PR2: Cannot use "lambda" as xvar.

  • PR3: Cannot use "val" as xvar.

If max_vars_per_plot exceeds 10, it is reset to 10. The plot provides insight into how coefficients evolve across different regularization parameters.

Cross-Validation Errors

Generates a plot that shows the cross-validation error metric against the logarithm of the tuning parameter (val or lambda), depending on the model type. It adds a vertical dashed line to indicate the optimal parameter value.

  • PR1/PR2: Plots cross-validation errors against log(val) values.

  • PR3: Plots cross-validation errors against log(lambda) values.

See also

Author

Ziwei Chen, Vali Asimit and Pietro Millossovich
Maintainer: Ziwei Chen <ziwei.chen.3@citystgeorges.ac.uk>

Examples

# \donttest{
# Example usage for cv.savvyPR with Correlated Data:
set.seed(123)
n <- 100
p <- 10
# Create highly correlated predictors to demonstrate parity regression
base_var <- rnorm(n)
x <- matrix(rnorm(n * p, sd = 0.1), n, p) + base_var
beta <- matrix(rnorm(p), p, 1)
y <- x %*% beta + rnorm(n, sd = 0.5)

# Fit CV model using Budget method
cv_result1 <- cv.savvyPR(x, y, method = "budget", model_type = "PR1",
                         measure_type = "mse", intercept = FALSE)
plot(cv_result1, plot_type = "estimated_coefficients")

plot(cv_result1, plot_type = "risk_contributions", label = FALSE)

plot(cv_result1, plot_type = "cv_coefficients", xvar = "val", max_vars_per_plot = 10)

plot(cv_result1, plot_type = "cv_errors")


# Fit CV model using Target method
cv_result2 <- cv.savvyPR(x, y, method = "target", model_type = "PR2")
cv_result3 <- cv.savvyPR(x, y, method = "budget", model_type = "PR3")

plot(cv_result2, plot_type = "cv_coefficients", xvar = "val",
     max_vars_per_plot = 5, label = FALSE)



plot(cv_result3, plot_type = "cv_coefficients", xvar = "lambda",
     max_vars_per_plot = 10, label = TRUE)


plot(cv_result2, plot_type = "cv_errors", label = FALSE)

plot(cv_result3, plot_type = "cv_errors", label = TRUE)

# }