| Title: | Sparse Projected Averaged Regression |
|---|---|
| Description: | A flexible framework combining variable screening and random projection techniques for fitting ensembles of predictive generalized linear models to high-dimensional data. Designed for extensibility, the package implements key techniques as S3 classes with user-friendly constructors, enabling easy integration and development of new procedures for high-dimensional applications. |
| Authors: | Laura Vana-Gür [aut, cre] (ORCID: <https://orcid.org/0000-0002-9613-7604>), Roman Parzer [aut] (ORCID: <https://orcid.org/0000-0003-0893-3190>), Peter Filzmoser [aut] (ORCID: <https://orcid.org/0000-0002-8014-4682>) |
| Maintainer: | Laura Vana-Gür <[email protected]> |
| License: | GPL-3 |
| Version: | 1.2.0 |
| Built: | 2026-10-23 14:15:52 UTC |
| Source: | https://github.com/lauravana/spareg |
'spar' ObjectExtracts coefficients from 'spar' object
## S3 method for class 'spar' coef( object, nummod = NULL, nu = NULL, aggregate = c("mean", "median", "none"), ... )## S3 method for class 'spar' coef( object, nummod = NULL, nu = NULL, aggregate = c("mean", "median", "none"), ... )
object |
result of spar of class |
nummod |
number of models used to form coefficients; value with minimal
validation |
nu |
threshold level used to compute the coefficients; value with minimal
validation |
aggregate |
character, one of |
... |
further arguments passed to or from other methods. |
Returns an object of class 'coefspar', which is a list with elements
intercept: The average intercept value or vector intercepts (one for
each marginal model) if aggregate = "none".
beta: Vector of length of averaged coefficients or a
x max(nummods) matrix of coefficients if agregate = "none".
nummod: Number of models based on which the coefficients are computed.
nu: Threshold value based on which the coefficients are computed.
print.coefspar, summary.coefspar
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) coef(spar_res) coef(spar_res, aggregate = "median") coef(spar_res, aggregate = "none") coef(spar_res, nummod = 5, nu = 0)example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) coef(spar_res) coef(spar_res, aggregate = "median") coef(spar_res, aggregate = "none") coef(spar_res, nummod = 5, nu = 0)
'spar.cv' ObjectExtract coefficients from 'spar.cv' object.
This function extracts coefficients only if
precompute_mode = "precompute_all". In this case, coefficients
are derived from the initial run on the full dataset using the optimal
and combination identified via cross-validation
(best or 1se rule). For other modes ("precompute_proj" or
"full_cv"), coefficients cannot be directly extracted because
screening or projections vary across folds. To obtain coefficients in
these cases, refit the model on the full dataset using spar()
with the selected and values.
## S3 method for class 'spar.cv' coef( object, nummod = NULL, nu = NULL, opt_par = c("best", "1se"), aggregate = c("mean", "median", "none"), ... )## S3 method for class 'spar.cv' coef( object, nummod = NULL, nu = NULL, opt_par = c("best", "1se"), aggregate = c("mean", "median", "none"), ... )
object |
result of spar.cv function of class |
nummod |
optional number of models used to form coefficients. |
nu |
optional threshold level used to form coefficients. |
opt_par |
one of |
aggregate |
character one of |
... |
further arguments passed to or from other methods. |
List with elements
intercept average intercept value or vector intercepts
(one for each marginal model) if aggregate = "none".
beta vector of length of averaged coefficients or a
x max(nummods) matrix of coefficients if agregate = "none".
nummod number of models based on which the coefficient is computed.
nu threshold based on which the coefficients is computed.
predict.spar.cv, get_model.spar.cv
example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spar.cv(example_data$x, example_data$y, nfolds = 3L, nummods = c(5, 10)) coef(spar_res)example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spar.cv(example_data$x, example_data$y, nfolds = 3L, nummods = c(5, 10)) coef(spar_res)
'randomprojection' ObjectCreates an object class 'randomprojection' using arguments passed by user.
constructor_randomprojection( name = NULL, generate_fun, update_fun = update_rp_default, update_rpm_w_data = update_rpm_identity, control = list() )constructor_randomprojection( name = NULL, generate_fun, update_fun = update_rp_default, update_rpm_w_data = update_rpm_identity, control = list() )
name |
optional string describing the random projection method. This is used for printing. |
generate_fun |
A function for generating the random projection matrix. This function must accept the following arguments:
|
update_fun |
A function for updating the
|
update_rpm_w_data |
A function for updating an already-generated random projection matrix with data-dependent information. This function must accept:
|
control |
A list of control parameters for the random projection. Default: |
The update_rpm_w_data function is particularly relevant for cross-validation procedures where
random projection matrices are precomputed (e.g., precompute_mode = "precompute_all" or precompute_mode = "precompute_rpm").
In such cases, the cross-validation procedure uses the precomputed matrices, but you may want to update
data-dependent entries (e.g., diagonal elements) with the training data in each fold.
For example, in rp_cw(data = TRUE), the diagonal elements of the projection matrices are updated
to reflect screening coefficients computed on the training data for each fold, while the random elements remain unchanged.
Returns a function that, when called, creates and returns an object of class randomprojection.
generate_cauchy <- function(object, x, y, m, included_vector, ...) { p <- length(included_vector) control_rcauchy <- c(object$control[names(object$control) %in% names(formals(rcauchy))], attributes(object)[names(attributes(object)) %in% names(formals(rcauchy))]) control_rcauchy <- control_rcauchy[!duplicated(names(control_rcauchy))] vals <- do.call(function(...) rcauchy(m * p, ...), control_rcauchy) RM <- matrix(vals, nrow = m, ncol = p) return(RM) } rp_cauchy <- constructor_randomprojection( generate_fun = generate_cauchy, name = "rp_cauchy") example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, rp = rp_cauchy(scale = 1/400)) spar_resgenerate_cauchy <- function(object, x, y, m, included_vector, ...) { p <- length(included_vector) control_rcauchy <- c(object$control[names(object$control) %in% names(formals(rcauchy))], attributes(object)[names(attributes(object)) %in% names(formals(rcauchy))]) control_rcauchy <- control_rcauchy[!duplicated(names(control_rcauchy))] vals <- do.call(function(...) rcauchy(m * p, ...), control_rcauchy) RM <- matrix(vals, nrow = m, ncol = p) return(RM) } rp_cauchy <- constructor_randomprojection( generate_fun = generate_cauchy, name = "rp_cauchy") example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, rp = rp_cauchy(scale = 1/400)) spar_res
screencoef ObjectsThe created function will return a object of class screencoef which
constitutes of a list. The attributes of the generating object will include by
default type, which can take one of two values "prob" (indicating
probabilistic screening should be employed),
"fixed" (indicating that the top nscreen variables should be employed).
constructor_screencoef( name = NULL, generate_fun, update_fun = update_screen_default )constructor_screencoef( name = NULL, generate_fun, update_fun = update_screen_default )
name |
character |
generate_fun |
function responsible for computing the screening coefficients. This
function should have arguments
|
update_fun |
optional function for updating the |
Creates an object class screencoef using arguments passed by user.
Returns a function that, when called, creates and returns an object of class 'screencoef'.
generate_scr_sirs <- function(y, x, object, ...) { ctrl <- object$control[names(object$control) %in% names(formals(VariableScreening::screenIID))] res_screen <- do.call(function(...) VariableScreening::screenIID(x, y, ...), ctrl) res_screen$measurement } screen_sirs <- constructor_screencoef("screen_sirs", generate_fun = generate_scr_sirs) example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_example <- spar(example_data$x, example_data$y, screencoef = screen_sirs(control = list(method = "SIRS")), rp = rp_sparse()) spar_examplegenerate_scr_sirs <- function(y, x, object, ...) { ctrl <- object$control[names(object$control) %in% names(formals(VariableScreening::screenIID))] res_screen <- do.call(function(...) VariableScreening::screenIID(x, y, ...), ctrl) res_screen$measurement } screen_sirs <- constructor_screencoef("screen_sirs", generate_fun = generate_scr_sirs) example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_example <- spar(example_data$x, example_data$y, screencoef = screen_sirs(control = list(method = "SIRS")), rp = rp_sparse()) spar_example
sparmodel ObjectThe created function will return a object of class sparmodel which
constitutes of a list.
constructor_sparmodel( name = NULL, generate_fun, update_fun = update_sparmodel_default )constructor_sparmodel( name = NULL, generate_fun, update_fun = update_sparmodel_default )
name |
optional string describing the model employed. This is used for printing. |
generate_fun |
function for estimating the marginal models which returns the
function should have arguments a
|
update_fun |
optional function for updating the |
Creates an object of class sparmodel using arguments passed by user.
Returns a function that, when called, creates and returns an object of class sparmodel.
model_glmrob <- function(object, x, y, z, ...) { requireNamespace("robustbase") glmrob_res <- do.call(function(...) robustbase::glmrob(y ~ as.matrix(z), ...), object$control) intercept <- coef(glmrob_res)[1] gammas <- coef(glmrob_res)[-1] list(gammas = gammas, intercept = intercept) } spar_glmrob <- constructor_sparmodel( generate_fun = model_glmrob,name = "glmrob") example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, model = spar_glmrob()) spar_resmodel_glmrob <- function(object, x, y, z, ...) { requireNamespace("robustbase") glmrob_res <- do.call(function(...) robustbase::glmrob(y ~ as.matrix(z), ...), object$control) intercept <- coef(glmrob_res)[1] gammas <- coef(glmrob_res)[-1] list(gammas = gammas, intercept = intercept) } spar_glmrob <- constructor_sparmodel( generate_fun = model_glmrob,name = "glmrob") example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, model = spar_glmrob()) spar_res
'coefspar' ObjectExtractor for Model Coefficients from 'coefspar' Object
get_coef(x)get_coef(x)
x |
A ' |
A numeric vector or matrix of coefficients.
coef.spar, coef.spar.cv, print.coefspar, summary.coefspar
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) coefs <- coef(spar_res) get_coef(coefs)example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) coefs <- coef(spar_res) get_coef(coefs)
'coefspar' ObjectExtractor for Model Intercept from 'coefspar' Object
get_intercept(x)get_intercept(x)
x |
A ' |
Intercept (numeric or vector).
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) coefs <- coef(spar_res) get_coef(coefs)example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) coefs <- coef(spar_res) get_coef(coefs)
spar' or 'spar.cv' ObjectExtractor for (Cross-)Validation Measure from 'spar' or 'spar.cv' Object
get_measure(object)get_measure(object)
object |
A fitted ' |
data.frame containing the (cross-)validation measure for the considered threshold and number of model combinations.
For 'spar' objects it contains information about the measure calculated on the validation set (or on the training sample if
xval and yval are missing) and the number of active variables. For 'spar.cv' objects it contains information
on the average measure obtained across folds together with the standard deviation across the folds and the average number of active variables.
the nfolds of the training set.
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) get_measure(spar_res)example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) get_measure(spar_res)
Retrieve the Best Model from a Fitted Object
get_model(object, ...)get_model(object, ...)
object |
An object of class |
... |
Additional arguments passed to the specific method. |
This generic function retrieves the best model from a fitted object of class spar or spar.cv.
The method dispatched depends on the class of the input object.
The modified object with the best model selected.
# Example usage with a fitted object example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) fitted_obj <- spar(example_data$x, example_data$y, family = gaussian(), nfolds = 3L) best_model <- get_model(fitted_obj)# Example usage with a fitted object example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) fitted_obj <- spar(example_data$x, example_data$y, family = gaussian(), nfolds = 3L) best_model <- get_model(fitted_obj)
spar ObjectsExtracts the best model from a fitted spar object based on validation results.
The best model is determined by the minimum validation measure.
## S3 method for class 'spar' get_model(object, ...)## S3 method for class 'spar' get_model(object, ...)
object |
An object of class |
... |
Additional arguments (currently unused). |
An updated spar object containing:
betas: Coefficients of the best model, with values below nu set to 0.
intercepts: Intercepts for the selected number of models (nummod).
val_res: Validation results filtered for the best nummod and nu.
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) fitted_spar <- spar(example_data$x, example_data$y, family = gaussian()) best_spar <- get_model(fitted_spar)example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) fitted_spar <- spar(example_data$x, example_data$y, family = gaussian()) best_spar <- get_model(fitted_spar)
spar.cv ObjectsExtracts the best or 1SE model from a fitted spar.cv object.
The selection is based on cross-validation results, and the model can be re-estimated if required.
## S3 method for class 'spar.cv' get_model( object, opt_par = c("best", "1se"), x = NULL, y = NULL, xval = NULL, yval = NULL, ... )## S3 method for class 'spar.cv' get_model( object, opt_par = c("best", "1se"), x = NULL, y = NULL, xval = NULL, yval = NULL, ... )
object |
An object of class |
opt_par |
A character string specifying the selection criterion:
|
x |
A matrix or data frame of predictors. Required if |
y |
A response vector. Required if |
xval |
A matrix or data frame of validation predictors. If |
yval |
A validation response vector. If |
... |
Additional arguments passed to |
An updated spar or spar.cv object containing:
betas: Coefficients of the selected model.
intercepts: Intercepts for the selected model.
val_res: Validation results filtered for the selected nummod and nu.
Additional fields like xscale, yscale, xcenter, and ycenter if precompute_mode == "precompute_all".
example_data <- simulate_spareg_data(n = 50, p = 200, ntest = 50) fitted_spar_cv <- spar.cv(example_data$x, example_data$y, family = gaussian(), nfolds = 3L) best_spar_cv <- get_model(fitted_spar_cv, opt_par = "best")example_data <- simulate_spareg_data(n = 50, p = 200, ntest = 50) fitted_spar_cv <- spar.cv(example_data$x, example_data$y, family = gaussian(), nfolds = 3L) best_spar_cv <- get_model(fitted_spar_cv, opt_par = "best")
'spar' ObjectCreates diagnostic plots for a 'spar'] object, including:
Validation measure vs. nus or nummods.
Number of active variables vs. nus or nummods.
Residuals vs. fitted values.
Coefficient heatmap.
## S3 method for class 'spar' plot( x, plot_type = c("val_measure", "val_numactive", "res_vs_fitted", "coefs"), plot_along = c("nu", "nummod"), nummod = NULL, nu = NULL, xfit = NULL, yfit = NULL, prange = NULL, coef_order = NULL, digits = 2L, ... )## S3 method for class 'spar' plot( x, plot_type = c("val_measure", "val_numactive", "res_vs_fitted", "coefs"), plot_along = c("nu", "nummod"), nummod = NULL, nu = NULL, xfit = NULL, yfit = NULL, prange = NULL, coef_order = NULL, digits = 2L, ... )
x |
A |
plot_type |
The type of plot. Options:
|
plot_along |
The variable to plot along the x-axis. Options:
|
nummod |
The number of models to fix when |
nu |
The threshold value to fix when |
xfit |
The predictor data for fitted values (required if |
yfit |
The response data for fitted values (required if |
prange |
A vector of length 2 specifying the range of predictors to plot (for |
coef_order |
An optional vector specifying the order of coefficients for |
digits |
The number of significant digits for axis labels. Default: |
... |
Additional arguments passed to or from other methods. |
Returns a 'ggplot2::ggplot' object.
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) plot(spar_res) plot(spar_res, plot_type = "val_measure", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_measure", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "val_numactive", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_numactive", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "res_vs_fitted", xfit = example_data$xtest, yfit = example_data$ytest) plot(spar_res, plot_type = "coefs", prange = c(1,400))example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) plot(spar_res) plot(spar_res, plot_type = "val_measure", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_measure", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "val_numactive", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_numactive", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "res_vs_fitted", xfit = example_data$xtest, yfit = example_data$ytest) plot(spar_res, plot_type = "coefs", prange = c(1,400))
'spar.cv' ObjectPlot cross-validation measure or number of active variables over different thresholds or number
of models of 'spar.cv' object, produce a residuals vs fitted plot,
or a plot of the estimated coefficients in each marginal model, sorted by their absolute value.
## S3 method for class 'spar.cv' plot( x, plot_type = c("val_measure", "val_numactive", "res_vs_fitted", "coefs"), plot_along = c("nu", "nummod"), nummod = NULL, nu = NULL, xfit = NULL, yfit = NULL, opt_par = c("best", "1se"), prange = NULL, coef_order = NULL, digits = 2L, ... )## S3 method for class 'spar.cv' plot( x, plot_type = c("val_measure", "val_numactive", "res_vs_fitted", "coefs"), plot_along = c("nu", "nummod"), nummod = NULL, nu = NULL, xfit = NULL, yfit = NULL, opt_par = c("best", "1se"), prange = NULL, coef_order = NULL, digits = 2L, ... )
x |
result of spar.cv function of class |
plot_type |
one of |
plot_along |
one of |
nummod |
fixed value for |
nu |
fixed value for |
xfit |
optional vector of fitted values to be used for |
yfit |
optional vector of response values to be used for |
opt_par |
one of |
prange |
optional vector of length 2 indicating the range of predictors to be plotted for |
coef_order |
optional vector of length |
digits |
number of significant digits to be displayed in the axis; defaults to 2L. |
... |
further arguments passed to or from other methods |
'ggplot2::ggplot' object
example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spar.cv(example_data$x, example_data$y, nfolds = 3L, screencoef = screen_cor(), rp = rp_gaussian(), nummods = c(5, 10)) plot(spar_res) plot(spar_res, plot_type = "val_measure", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_measure", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "val_numactive", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_numactive", plot_along = "nu", nummod = 10)example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spar.cv(example_data$x, example_data$y, nfolds = 3L, screencoef = screen_cor(), rp = rp_gaussian(), nummods = c(5, 10)) plot(spar_res) plot(spar_res, plot_type = "val_measure", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_measure", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "val_numactive", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_numactive", plot_along = "nu", nummod = 10)
'spar.cv' ObjectPredict responses for new predictors from 'spar' object
## S3 method for class 'spar' predict( object, xnew = NULL, type = c("response", "link"), avg_type = c("link", "response"), nummod = NULL, nu = NULL, aggregate = c("mean", "median"), ... )## S3 method for class 'spar' predict( object, xnew = NULL, type = c("response", "link"), avg_type = c("link", "response"), nummod = NULL, nu = NULL, aggregate = c("mean", "median"), ... )
object |
result of spar function of class |
xnew |
matrix of new predictor variables; must have same number of columns as |
type |
the type of required predictions; either on response level (default) or on link level |
avg_type |
type of averaging the marginal models; either on link (default) or on response level |
nummod |
number of models used to form coefficients; value with minimal validation measure is used if not provided. |
nu |
threshold level used to form coefficients; value with minimal validation measure is used if not provided. |
aggregate |
character one of c("mean", "median"); the aggregation over the ensembles is done using the specified method (mean or median). Defaults to mean aggregation. |
... |
further arguments passed to or from other methods |
Returns a vector of predictions.
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) pred <- predict(spar_res, xnew = example_data$xtest)example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) pred <- predict(spar_res, xnew = example_data$xtest)
'spar.cv' ObjectPredict responses for new predictors from 'spar.cv' object.
Only allowed if precompute_mode = "precompute_all" is used when calling spar.cv.
In this case the ensemble obtained on the whole data is used for the
combination of threshold and number of models using the best or the 1se identified through cross-validation.
Otherwise predictions cannot be generated without refitting on whole data with
best or 1se parameters. To be able to generate predictions in case precompute_mode = "precompute_rpm"
or precompute_mode = "full_cv", use spar() to refit with the desired (nu, M) combination.
## S3 method for class 'spar.cv' predict( object, xnew = NULL, type = c("response", "link"), avg_type = c("link", "response"), opt_par = c("best", "1se"), nummod = NULL, nu = NULL, aggregate = c("mean", "median"), ... )## S3 method for class 'spar.cv' predict( object, xnew = NULL, type = c("response", "link"), avg_type = c("link", "response"), opt_par = c("best", "1se"), nummod = NULL, nu = NULL, aggregate = c("mean", "median"), ... )
object |
result of spar function of class |
xnew |
matrix of new predictor variables; must have same number of columns as |
type |
the type of required predictions; either on response level (default) or on link level |
avg_type |
type of averaging the marginal models; either on link (default) or on response level |
opt_par |
one of |
nummod |
number of models used to form coefficients; value with
minimal validation |
nu |
threshold level used to form coefficients; value with minimal
validation |
aggregate |
character one of c("mean", "median"); the aggregation over the ensembles is done using the specified method (mean or median). Defaults to mean aggregation. |
... |
further arguments passed to or from other methods |
Vector of predictions
coef.spar.cv, get_model.spar.cv
example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spar.cv(example_data$x, example_data$y, nfolds = 3L, rp = rp_gaussian(), nummods = c(5, 10)) pred <- predict(spar_res, example_data$x)example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spar.cv(example_data$x, example_data$y, nfolds = 3L, rp = rp_gaussian(), nummods = c(5, 10)) pred <- predict(spar_res, example_data$x)
'coefspar' ObjectPrints a summary of coefficients from a 'coefspar' object, including the selected and ,
and the number of active variables.
## S3 method for class 'coefspar' print(x, digits = 4L, show = 6L, ...)## S3 method for class 'coefspar' print(x, digits = 4L, show = 6L, ...)
x |
A |
digits |
The number of significant digits for numeric output. Default: |
show |
The number of coefficients to display. Default: |
... |
Additional arguments passed to or from other methods. |
Invisibly returns the input object x.
example_data <- simulate_spareg_data(n = 100, p = 2000, ntest = 100) spar_res <- spareg(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) coef(spar_res) coef(spar_res, aggregate = "median") coef(spar_res, aggregate = "none") print(coef(spar_res), show = 10L, digits = 6L)example_data <- simulate_spareg_data(n = 100, p = 2000, ntest = 100) spar_res <- spareg(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) coef(spar_res) coef(spar_res, aggregate = "median") coef(spar_res, aggregate = "none") print(coef(spar_res), show = 10L, digits = 6L)
'randomprojection' ObjectPrints a summary of a randomprojection object, including its name, and key attributes
such as mslow and msup.
## S3 method for class 'randomprojection' print(x, ...)## S3 method for class 'randomprojection' print(x, ...)
x |
An object of class |
... |
Additional arguments (ignored). |
Invisibly returns the input object x.
rp <- rp_gaussian(mslow = 5, msup = 10) print(rp)rp <- rp_gaussian(mslow = 5, msup = 10) print(rp)
'screencoef' ObjectPrint method for a 'screencoef' object
## S3 method for class 'screencoef' print(x, ...)## S3 method for class 'screencoef' print(x, ...)
x |
description |
... |
further arguments passed to or from other methods |
text summary
'spar' ObjectPrints a summary of a spar object, including the validation
measure, optimal and , and the number of active predictors.
## S3 method for class 'spar' print(x, digits = 4L, ...)## S3 method for class 'spar' print(x, digits = 4L, ...)
x |
A |
digits |
The number of significant digits for numeric output. Default: |
... |
Additional arguments passed to or from other methods. |
Invisibly returns the input object x.
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) print(spar_res)example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) print(spar_res)
'spar.cv' ObjectPrint summary of 'spar.cv' object
## S3 method for class 'spar.cv' print(x, digits = 4L, ...)## S3 method for class 'spar.cv' print(x, digits = 4L, ...)
x |
result of spar.cv function of class |
digits |
integer digits to be printed, defaults to 4L. |
... |
further arguments passed to or from other methods |
text summary
example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spareg.cv(example_data$x, example_data$y, nfolds = 3L, screencoef = screen_cor(), rp = rp_gaussian(), nummods = c(5, 10)) print(spar_res)example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spareg.cv(example_data$x, example_data$y, nfolds = 3L, screencoef = screen_cor(), rp = rp_gaussian(), nummods = c(5, 10)) print(spar_res)
The 'randomprojection' class' represents a configuration for generating and managing random projection matrices in the SPAR (Sparse Projected Averaged Regression) framework. Objects of this class encapsulate:
Functions for generating, updating, and modifying random projection matrices.
Control parameters for the random projection process.
Metadata (e.g., name, attributes) for customization.
Objects of class 'randomprojection' are created using the constructor_randomprojection function. They are used in the spar and spar.cv functions to define how predictors are projected into a lower-dimensional space.
The class includes the following components:
name: A character string describing the random projection method (e.g., "rp_gaussian", "rp_sparse").
generate_fun: A function to generate the random projection matrix. This function must accept arguments like object, x, y, m, included_vector, and ....
update_fun: A function to update the randomprojection object with data-specific information. This function must accept arguments like object, x, y, family, and ....
update_rpm_w_data: A function to update an already-generated random projection matrix with data-dependent information. This function must accept arguments like rpm, object, included_vector, x, y, family, and ....
control: A list of control parameters for the random projection process (e.g., mslow, msup, psi).
The following attributes are commonly used in randomprojection objects:
mslow: Integer. The minimum dimension for projection. Default: \eqn{\log(p)}.
msup: Integer. The maximum dimension for projection. Default: \eqn{n/2}.
data: Logical. If TRUE, the projection matrix is updated with data-dependent coefficients (e.g., for rp_cw).
'randomprojection' objects are typically created using predefined constructors like:
rp_gaussian(): Gaussian random projection.
rp_sparse(): Sparse random projection (Achlioptas, 2003).
rp_cw(): Sparse Embedding (Clarkson-Woodruff) random projection.
Users can also define custom random projection methods by providing their own generate_fun, update_fun, and update_rpm_w_data functions to constructor_randomprojection.
Achlioptas D (2003). “Database-Friendly Random Projections: Johnson-Lindenstrauss with Binary Coins.” Journal of Computer and System Sciences, 66(4), 671-687. ISSN 0022-0000. doi:10.1016/S0022-0000(03)00025-4. Special Issue on PODS 2001.
Clarkson KL, Woodruff DP (2013). “Low Rank Approximation and Regression in Input Sparsity Time.” In Proceedings of the Forty-Fifth Annual ACM Symposium on Theory of Computing, STOC '13, 81–90. ISBN 9781450320290. doi:10.1145/2488608.2488620.
Parzer R, Filzmoser P, Vana-Gür L (2025). “Data-Driven Random Projection and Screening for High-Dimensional Generalized Linear Models.” Statistical Modelling. doi:10.1177/1471082X251392705..
constructor_randomprojection, rp_gaussian, rp_sparse, rp_cw, spar
rp_gauss <- rp_gaussian(mslow = 5, msup = 10) example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar( example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, rp = rp_gauss )rp_gauss <- rp_gaussian(mslow = 5, msup = 10) example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar( example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, rp = rp_gauss )
Creates an object class 'randomprojection' using arguments passed by
user which in turn can be employed to generate a sparse embedding matrix as
in (Clarkson and Woodruff 2013).
rp_cw(..., control = list())rp_cw(..., control = list())
... |
includes arguments which can be passed as attributes to the random projection matrix |
control |
list of arguments to be used in functions
|
The entries of the matrix are generated based on (Clarkson and Woodruff 2013).
This matrix is constructed as , where
is a binary matrix, where for each column
an index is uniformly sampled from and the corresponding
entry is set to one, and is a diagonal matrix,
with entries .
If specified as rp_cw(data = TRUE), the random elements on the diagonal
are replaced by the ridge coefficients with a small penalty, as introduced in
(Parzer et al. 2025).
Arguments related to the random projection procedure can
be passed to the rp_cw() function through ..., and
will be saved as attributes of the 'randomprojection' object.
The following attributes are relevant for spar and spar.cv:
mslow: integer giving the minimum dimension to which the predictors
should be projected; defaults to .
msup: integer giving the maximum dimension to which the predictors
should be projected; defaults to .
object of class 'randomprojection' which is a list with
elements name,
generate_fun, update_fun, control
Clarkson KL, Woodruff DP (2013). “Low Rank Approximation and Regression in Input Sparsity Time.” In Proceedings of the Forty-Fifth Annual ACM Symposium on Theory of Computing, STOC '13, 81–90. ISBN 9781450320290. doi:10.1145/2488608.2488620.
Parzer R, Filzmoser P, Vana-Gür L (2025). “Data-Driven Random Projection and Screening for High-Dimensional Generalized Linear Models.” Statistical Modelling. doi:10.1177/1471082X251392705..
example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), rp = rp_cw(data = TRUE))example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), rp = rp_cw(data = TRUE))
Creates an object class 'randomprojection' using arguments passed by
user which in turn can be employed to generate a random matrix with normally
distributed entries (mean 0 and standard deviation 1 by default).
rp_gaussian(..., control = list())rp_gaussian(..., control = list())
... |
includes arguments which can be passed as attributes to the random projection matrix |
control |
list of arguments to be used in functions
|
Arguments related to the random projection procedure can
be passed to the rp_gaussian() function through ..., and
will be saved as attributes of the 'randomprojection' object.
The following attributes are relevant for spar and spar.cv:
mslow: integer giving the minimum dimension to which the predictors
should be projected; defaults to .
msup: integer giving the maximum dimension to which the predictors
should be projected; defaults to .
Returns an object of class 'randomprojection' which is a list with
elements name,
generate_fun, update_fun, control
example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), rp = rp_gaussian(control = list(sd = 1/sqrt(ncol(example_data$x)))))example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), rp = rp_gaussian(control = list(sd = 1/sqrt(ncol(example_data$x)))))
Creates an object class 'randomprojection' using arguments passed by
user which in turn can be employed to generate a sparse embedding matrix as
in (Achlioptas 2003).
rp_sparse(..., control = list())rp_sparse(..., control = list())
... |
includes arguments which can be passed as attributes to the random projection matrix. |
control |
list of arguments to be used in functions
|
The sparse matrix used in (Achlioptas 2003) with entries equal to
with probability and zero otherwise
for . Default is psi = 1.
Arguments related to the random projection procedure can
be passed to the rp_gaussian() function through ..., and
will be saved as attributes of the 'randomprojection' object.
The following attributes are relevant for spar and spar.cv:
mslow: integer giving the minimum dimension to which the predictors
should be projected; defaults to .
msup: integer giving the maximum dimension to which the predictors
should be projected; defaults to .
object of class 'randomprojection' which is a list with
elements name,
generate_fun, update_fun, control
Achlioptas D (2003). “Database-Friendly Random Projections: Johnson-Lindenstrauss with Binary Coins.” Journal of Computer and System Sciences, 66(4), 671-687. ISSN 0022-0000. doi:10.1016/S0022-0000(03)00025-4. Special Issue on PODS 2001.
example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), rp = rp_sparse(control = list(psi = 1/3)))example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), rp = rp_sparse(control = list(psi = 1/3)))
Creates an object class 'screencoef' using arguments passed by user,
where the screening coefficient should be computed based on the correlation
coefficient of response and each predictor separately.
screen_cor(..., control = list())screen_cor(..., control = list())
... |
includes arguments which can be passed as attributes to the
|
control |
list of controls to be passed to the screening function |
Creates an object class 'screencoef' using arguments passed by user.
The function generate_fun relies on cor.
Arguments related to the screening procedure can
be passed to the screen_cor() function through ..., and
will be saved as attributes of the 'screencoef' object.
object of class 'screencoef' which is a list with elements
name (character, optional, used for printing)
control (list of controls passed as an argument)
generate_fun for generating the screening coefficient.
This function should have arguments and y (vector of (standardized for Gaussian) responses),
x (the matrix of standardized predictors) and a 'screencoef' object.
constructor_screencoef, screencoef
example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), screencoef = screen_cor(control = list(method = "kendall")))example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), screencoef = screen_cor(control = list(method = "kendall")))
Creates an object class 'screencoef' using arguments passed by user,
where the screening coefficient should be computed based on penalized coefficients.
screen_glmnet(..., control = list())screen_glmnet(..., control = list())
... |
includes arguments which can be passed as attributes to the
|
control |
list of controls to be passed to the screening function |
Creates an object class 'screencoef' using arguments passed by user.
The function generate_fun relies on glmnet.
Arguments related to the screening procedure can
be passed to the screen_glmnet() function through ..., and
will be saved as attributes of the 'screencoef' object.
object of class 'screencoef' which is a list with elements
name (character, optional, used for printing)
control (list of controls passed as an argument)
generate_fun for generating the screening coefficient.
This function should have arguments and y (vector of (standardized for Gaussian) responses),
x (the matrix of standardized predictors) and a 'screencoef' object.
constructor_screencoef, screencoef
example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), screencoef = screen_glmnet(control = list(alpha = 0.1)))example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), screencoef = screen_glmnet(control = list(alpha = 0.1)))
Creates an object class 'screencoef' using arguments passed by user,
where the screening coefficient should be computed based on the marginal
likelihood of the univariate GLM where the response is regressed on
each predictor separately.
screen_marglik(..., control = list())screen_marglik(..., control = list())
... |
includes arguments which can be passed as attributes to the
|
control |
list of controls to be passed to the screening function |
The function generate_fun relies on glm.
Arguments related to the screening procedure can
be passed to the screen_marglik() function through ..., and
will be saved as attributes of the 'screencoef' object.
Note that if family is not provided in control,
the family used in spar or spar.cv will be used.
object of class 'screencoef' which is a list with elements:
name (character, optional, used for printing)
control (list of controls passed as an argument)
generate_fun for generating the screening coefficient.
This function should have arguments and y (vector of (standardized for Gaussian) responses),
x (the matrix of standardized predictors) and a 'screencoef' object.
constructor_screencoef, screencoef
example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), screencoef = screen_marglik(nscreen = 500))example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30), screencoef = screen_marglik(nscreen = 500))
The 'screencoef' class represents a configuration for computing and managing screening coefficients
in the SPAR (Sparse Projected Averaged Regression) framework. Objects of this class encapsulate:
Functions for generating and updating screening coefficients.
Control parameters for the screening process.
Metadata (e.g., name, attributes) for customization.
Screening coefficients are used to reduce the dimensionality of the predictor space by selecting the most relevant variables before applying random projections.
Objects of class screencoef are created using the constructor_screencoef function. They are used in the
spar and spar.cv functions to define how predictors are screened.
The class includes the following components:
name: A character string describing the screening method (e.g., "screen_marglik", "screen_cor", "screen_glmnet").
generate_fun: A function to compute the screening coefficients. This function must accept:
x: A matrix of standardized predictors.
y: A vector of standardized responses.
object: An object of class screencoef.
...: Additional arguments passed from spar().
update_fun: A function to update the screencoef object with data-specific information. This function must accept:
object: An object of class screencoef.
x: A matrix of standardized predictors.
y: A vector of standardized responses.
family: A stats::family object.
...: Additional arguments passed from spar().
If not provided, the default update_screen_default is used, which leaves the object unchanged.
control: A list of control parameters for the screening process (e.g., nscreen, split_data_prop).
The following attributes are commonly used in screencoef objects:
type: Character. The type of screening to employ:
"prob": Probabilistic screening (variables are selected probabilistically based on their screening coefficients).
"fixed": Fixed screening (the top nscreen variables are selected).
Default: "prob".
nscreen: Integer. The number of variables to retain after screening. Default: 2n (twice the number of observations).
split_data_prop: Numeric. The proportion of data to use for computing screening coefficients.
The remaining data is used for fitting the marginal models. Default: 1 (use all data).
reuse_in_rp: Logical. If TRUE, the screening coefficients are reused in the construction of the random projection.
Default: FALSE.
importance: Numeric vector. The screening coefficients computed from the data.
inc_prob: Numeric vector. The inclusion probabilities for probabilistic screening (normalized screening coefficients).
The following predefined screening methods are available:
screen_marglik: Screening based on marginal likelihood in univariate GLMs.
Uses stats::glm to fit a separate GLM for each predictor and extracts the coefficients.
screen_cor: Screening based on correlation between predictors and the response.
Uses stats::cor to compute correlation coefficients.
screen_glmnet: Screening based on penalized regression coefficients from glmnet::glmnet.
Uses Lasso or Ridge regression to compute coefficients.
Users can also define custom screening methods by providing their own generate_fun and update_fun functions
to constructor_screencoef.
constructor_screencoef, screen_marglik, screen_cor, screen_glmnet, spar
screen_marglik_obj <- screen_marglik(nscreen = 500, type = "prob") example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar( example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, screencoef = screen_marglik_obj )screen_marglik_obj <- screen_marglik(nscreen = 500, type = "prob") example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar( example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, screencoef = screen_marglik_obj )
Generates synthetic data for sparse linear regression problems. Returns training and test sets along with model parameters.
simulate_spareg_data( n, p, ntest, a = min(100, p/4), snr = 10, rho = 0.5, mu = 1, beta_vals = NULL, seed = NULL )simulate_spareg_data( n, p, ntest, a = min(100, p/4), snr = 10, rho = 0.5, mu = 1, beta_vals = NULL, seed = NULL )
n |
Integer. Number of training samples. |
p |
Integer. Number of predictors (features). |
ntest |
Integer. Number of test samples. |
a |
Integer. Number of non-zero coefficients in the true beta vector. Default is min(100, p/4). |
snr |
Numeric. Signal-to-noise ratio. Default is 10. |
rho |
Numeric between 0 and 1. Pairwise correlation coefficient among predictors. Default is 0.5. A compound symmetry correlation matrix is used. The variance of the predictors is fixed to 1. |
mu |
Numeric. Intercept term (mean of response). Default is 1. |
beta_vals |
Numeric. Possible values for non-zero coefficients in the true beta vector. Default to NULL, in which case the values -3, -2, -1, 1, 2, 3 will be used. |
seed |
Integer. Random seed for reproducibility. Default is NULL. |
A list with the following components:
Training design matrix (n x p).
Training response vector (length n).
Test design matrix (ntest x p).
Test response vector (length ntest).
Intercept used in data generation.
True coefficient vector (length p).
Noise variance used in data generation. Equals /snr.
set.seed(123) data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) str(data)set.seed(123) data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) str(data)
Fits a Sparse Projected Averaged Regression (SPAR) model to high-dimensional data.
SPAR builds an ensemble of generalized linear models (GLMs) where high-dimensional predictors
are first screened (using a screening coefficient) and then projected using random projection matrices.
This function evaluates the model over a grid of thresholds (nus) and a grid of the number of marginal models (nummods).
It is also used internally by the cross-validated procedure spar.cv.
spar( x, y, family = gaussian("identity"), model = NULL, rp = NULL, screencoef = NULL, xval = NULL, yval = NULL, nnu = 20L, nus = NULL, nummods = 20L, measure = c("deviance", "mse", "mae", "class", "1-auc"), avg_type = c("link", "response"), parallel = FALSE, inds = NULL, RPMs = NULL, seed = NULL, ... ) spareg( x, y, family = gaussian("identity"), model = NULL, rp = NULL, screencoef = NULL, xval = NULL, yval = NULL, nnu = 20L, nus = NULL, nummods = 20L, measure = c("deviance", "mse", "mae", "class", "1-auc"), avg_type = c("link", "response"), parallel = FALSE, inds = NULL, RPMs = NULL, seed = NULL, ... )spar( x, y, family = gaussian("identity"), model = NULL, rp = NULL, screencoef = NULL, xval = NULL, yval = NULL, nnu = 20L, nus = NULL, nummods = 20L, measure = c("deviance", "mse", "mae", "class", "1-auc"), avg_type = c("link", "response"), parallel = FALSE, inds = NULL, RPMs = NULL, seed = NULL, ... ) spareg( x, y, family = gaussian("identity"), model = NULL, rp = NULL, screencoef = NULL, xval = NULL, yval = NULL, nnu = 20L, nus = NULL, nummods = 20L, measure = c("deviance", "mse", "mae", "class", "1-auc"), avg_type = c("link", "response"), parallel = FALSE, inds = NULL, RPMs = NULL, seed = NULL, ... )
x |
An |
y |
A quantitative response vector of length |
family |
A family object used for the marginal generalized linear model,
default |
model |
A function that creates a
|
rp |
A function that creates a |
screencoef |
function that creates a |
xval |
An optional matrix of predictor variables used for
validation. If |
yval |
An optional response vector for validation.
If |
nnu |
number of different threshold values |
nus |
An optional vector of thresholds |
nummods |
A vector of integers specifying the number of marginal models to consider for validation.
Defaults to |
measure |
The loss function for validation. Options:
|
avg_type |
The type of averaging for marginal models. Options:
|
parallel |
A logical indicating whether to use parallel
estimation of the marginal models. Defaults to |
inds |
An optional list of index vectors corresponding to variables retained after screening for each marginal model.
Must have length |
RPMs |
An optional list of projection matrices for each marginal model.
Must have length |
seed |
An optional integer seed for reproducibility. Default: |
... |
Additional arguments for backward compatibility. |
If a parallel backend (e.g., doParallel) is registered and parallel = TRUE,
the foreach package is used to parallelize the estimation of marginal models.
If a parallel backend is registered and parallel = TRUE,
the foreach function
is used to estimate the marginal models in parallel.
An object of class spar with the following components:
betas: A p x max(nummods) sparse matrix of standardized coefficients for each marginal model.
intercepts: Intercepts for each marginal model.
scr_coef: A vector of length p with screening coefficients for standardized predictors.
inds: A list of index vectors for variables retained after screening.
RPMs: A list of projection matrices for each marginal model.
val_res: A data.frame with validation results (measure and number of active variables) for each and .
val_set: A logical flag indicating whether validation data were provided.
family: The family object used for the GLM.
nus: The vector of thresholds considered.
nummods: The vector of numbers of marginal models considered.
ycenter: The empirical mean of the initial response vector.
yscale: The empirical standard deviation of the initial response vector.
xcenter: A vector of empirical means for the initial predictors.
xscale: A vector of empirical standard deviations for the initial predictors.
avg_type: The averaging type used for validation.
measure: The validation measure used.
rp: The 'randomprojection' object.
screencoef: The 'screencoef' object.
x_rows_for_fitting_marginal_models: A vector of row indices from x used for fitting marginal models (if screening splits data).
Parzer R, Filzmoser P, Vana-Gür L (2025). “Sparse Data-Driven Random Projection in Regression for High-Dimensional Data.” Journal of Data Science, Statistics, and Visualisation, 5(5). doi:10.52933/jdssv.v5i5.138.
Parzer R, Filzmoser P, Vana-Gür L (2025). “Data-Driven Random Projection and Screening for High-Dimensional Generalized Linear Models.” Statistical Modelling. doi:10.1177/1471082X251392705.
Clarkson KL, Woodruff DP (2013). “Low Rank Approximation and Regression in Input Sparsity Time.” In Proceedings of the Forty-Fifth Annual ACM Symposium on Theory of Computing, STOC '13, 81–90. ISBN 9781450320290. doi:10.1145/2488608.2488620.
Achlioptas D (2003). “Database-Friendly Random Projections: Johnson-Lindenstrauss with Binary Coins.” Journal of Computer and System Sciences, 66(4), 671-687. ISSN 0022-0000. doi:10.1016/S0022-0000(03)00025-4. Special Issue on PODS 2001.
spar.cv, coef.spar, predict.spar, plot.spar, print.spar
example_data <- simulate_spareg_data(n = 200, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30)) coefs <- coef(spar_res) pred <- predict(spar_res, xnew = example_data$x) plot(spar_res) plot(spar_res, plot_type = "val_measure", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_measure", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "val_numactive", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_numactive", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "res_vs_fitted", xfit = example_data$xtest, yfit = example_data$ytest) plot(spar_res, plot_type = "coefs", prange = c(1,400)) spar_res <- spareg(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30))example_data <- simulate_spareg_data(n = 200, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30)) coefs <- coef(spar_res) pred <- predict(spar_res, xnew = example_data$x) plot(spar_res) plot(spar_res, plot_type = "val_measure", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_measure", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "val_numactive", plot_along = "nummod", nu = 0) plot(spar_res, plot_type = "val_numactive", plot_along = "nu", nummod = 10) plot(spar_res, plot_type = "res_vs_fitted", xfit = example_data$xtest, yfit = example_data$ytest) plot(spar_res, plot_type = "coefs", prange = c(1,400)) spar_res <- spareg(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10, 15, 20, 25, 30))
sparmodel
Creates an object class sparmodel using arguments passed by user.
The generating function computes the coefficients of the marginal models
based on a GLM. Computation relies on stats::glm.
spar_glm(..., control = list())spar_glm(..., control = list())
... |
includes arguments which can be passed as attributes to the
|
control |
list of controls to be passed to the model function |
Relies on glm.
Returns an object of class sparmodel.
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, model = spar_glm())example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, model = spar_glm())
sparmodel
Creates an object class sparmodel using arguments passed by user, where
the generating function computes the coefficients of the marginal models
based on a penalized GLM. Computation relies on glmnet::glmnet.
By default, the models assume and return the coefficients
obtained with the penalty .
spar_glmnet(..., control = list())spar_glmnet(..., control = list())
... |
includes arguments which can be passed as attributes to the
|
control |
list of controls to be passed to the model function |
Relies on glmnet.
Returns an object of class sparmodel.
example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, model = spar_glmnet(alpha = 0.1))example_data <- simulate_spareg_data(n = 100, p = 400, ntest = 100) spar_res <- spar(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, model = spar_glmnet(alpha = 0.1))
Apply Sparse Projected Averaged Regression to High-Dimensional Data, where the number of models and the threshold parameter is chosen using a cross-validation procedure.
spar.cv( x, y, family = gaussian("identity"), model = spar_glmnet(), rp = NULL, screencoef = NULL, nfolds = 10, nnu = 20, nus = NULL, nummods = c(20), measure = c("deviance", "mse", "mae", "class", "1-auc"), avg_type = c("link", "response"), parallel = FALSE, seed = NULL, precompute_mode = c("precompute_all", "precompute_rpm", "full_cv"), ... ) spareg.cv( x, y, family = gaussian("identity"), model = spar_glmnet(), rp = NULL, screencoef = NULL, nfolds = 10, nnu = 20, nus = NULL, nummods = c(20), measure = c("deviance", "mse", "mae", "class", "1-auc"), avg_type = c("link", "response"), parallel = FALSE, seed = NULL, precompute_mode = c("precompute_all", "precompute_rpm", "full_cv"), ... )spar.cv( x, y, family = gaussian("identity"), model = spar_glmnet(), rp = NULL, screencoef = NULL, nfolds = 10, nnu = 20, nus = NULL, nummods = c(20), measure = c("deviance", "mse", "mae", "class", "1-auc"), avg_type = c("link", "response"), parallel = FALSE, seed = NULL, precompute_mode = c("precompute_all", "precompute_rpm", "full_cv"), ... ) spareg.cv( x, y, family = gaussian("identity"), model = spar_glmnet(), rp = NULL, screencoef = NULL, nfolds = 10, nnu = 20, nus = NULL, nummods = c(20), measure = c("deviance", "mse", "mae", "class", "1-auc"), avg_type = c("link", "response"), parallel = FALSE, seed = NULL, precompute_mode = c("precompute_all", "precompute_rpm", "full_cv"), ... )
x |
n x p numeric matrix of predictor variables. |
y |
quantitative response vector of length n. |
family |
a |
model |
function creating a |
rp |
function creating a |
screencoef |
function creating a |
nfolds |
number of folds to use for cross-validation; should be at least 2, defaults to 10. |
nnu |
number of different threshold values |
nus |
optional vector of |
nummods |
vector of numbers of marginal models to consider for
validation; defaults to |
measure |
loss to use for validation; defaults to |
avg_type |
type of averaging the marginal models; either on link (default) or on response level. This is used in computing the validation measure. |
parallel |
assuming a parallel backend is loaded and available, a logical indicating whether the function should use it in parallelizing the estimation of the marginal models. Defaults to FALSE. |
seed |
integer seed to be set at the beginning of the SPAR algorithm. Default to NULL, in which case no seed is set. |
precompute_mode |
character, one of |
... |
further arguments mainly to ensure back-compatibility |
object of class 'spar.cv' with elements
val_res a data.frame with CV results for each fold and for each element of nus and nummods
fitted_objects list of fitted objects for the whole data (first element) and for each fold, each element is a list with elements
betas_std p x max(nummods) sparse matrix of class
'Matrix::dgCMatrix' containing the
standardized coefficients from each marginal model computed with the SPAR
algorithm on the training data.
intercepts in each marginal model, vector of length max(nummods)
computed with the SPAR algorithm on the training data.
scr_coef p-vector of coefficients used for screening for standardized predictors
inds list of index-vectors corresponding to variables kept after
screening in each marginal model of length max(nummods). If kept fixed, only the first element corresponding to the indicators computed on the whole data is populated.
RPMs list of projection matrices used in each marginal model of length max(nummods). If kept fixed, only the first element corresponding to the projection matrices generated once on the whole data is populated.
ycenter empirical mean of response vector in training data
yscale empirical standard deviation of response vector in training data
.
xcenter p-vector of empirical means of predictor variables in training data
xscale p-vector of empirical standard deviations of predictor variables in training data
seed integer seed used at the beginning of the algorithm. In each fold the seed is modified as seed + fold_id.
nus vector of 's considered for thresholding
nummods vector of numbers of marginal models considered for validation
family a character corresponding to family object used for the marginal generalized linear model e.g.,
"gaussian(identity)"
measure character, type of validation measure used
avg_type character, averaging type for computing the validation measure
rp an object of class 'randomprojection'
screencoef an object of class 'screeningcoef'
model an object of class 'sparmodel'
precompute_mode character, one of c("precompute_all", "precompute_rpm", "full_cv"), indicating whether the same random projection matrix and/or the same indices for screening across folds were used.
seed integer seed used at the beginning of the algorithm.
spar, coef.spar.cv, predict.spar.cv, plot.spar.cv, print.spar.cv
example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spar.cv(example_data$x, example_data$y, nfolds = 3L, rp = rp_gaussian(), nummods = c(5, 10)) spar_res spar_res <- spareg.cv(example_data$x, example_data$y, nummods=c(5, 10, 15, 20, 25, 30))example_data <- simulate_spareg_data(n = 80, p = 200, ntest = 100) spar_res <- spar.cv(example_data$x, example_data$y, nfolds = 3L, rp = rp_gaussian(), nummods = c(5, 10)) spar_res spar_res <- spareg.cv(example_data$x, example_data$y, nummods=c(5, 10, 15, 20, 25, 30))
The sparmodel class represents a configuration for fitting marginal models in the
SPAR (Sparse Projected Averaged Regression) framework. Objects of this class encapsulate:
Functions for estimating marginal models (e.g., GLM, GLMNET, or custom models).
Control parameters for the model fitting process.
Metadata (e.g., name, attributes) for customization.
Marginal models are fitted to projected predictors (after screening and random projection) to compute coefficients and intercepts for each model in the ensemble.
Objects of class sparmodel are created using the constructor_sparmodel function or predefined constructors
like spar_glm and spar_glmnet. They are used in the spar and spar.cv functions to define how marginal models
are fitted to the projected data.
The class includes the following components:
name: A character string describing the model type (e.g., "glm", "glmnet", "glmrob").
generate_fun: A function to estimate the marginal model coefficients and intercept. This function must accept:
object: An object of class sparmodel.
x: A matrix of standardized predictors (unused in most cases, as models are fitted on projected data).
y: A vector of standardized responses.
z: A matrix of projected predictors (the design matrix for the marginal model).
...: Additional arguments passed from spar().
The function must return a list with two elements:
gammas: A vector of regression coefficients for the projected predictors.
intercept: The intercept of the model.
update_fun: A function to update the sparmodel object with data-specific information before fitting.
This function must accept:
object: An object of class sparmodel.
x: A matrix of standardized predictors.
y: A vector of standardized responses.
family: A stats::family object.
...: Additional arguments passed from spar().
If not provided, the default update_sparmodel_default is used, which only updates the family if missing.
control: A list of control parameters for the model fitting process (e.g., family, alpha for glmnet).
The following predefined model types are available:
spar_glm: Fits marginal models using stats::glm. Supports all standard GLM families.
For Gaussian models with identity link, it uses a fast OLS solver.
spar_glmnet: Fits marginal models using glmnet::glmnet. Supports penalized regression (Ridge, Lasso, or Elastic Net).
Automatically converts family objects to strings for compatibility with glmnet.
Custom Models: Users can define their own model types by providing custom generate_fun and update_fun
functions to constructor_sparmodel. For example, spar_glmrob (not exported by default) uses
robustbase::glmrob for robust regression.
constructor_sparmodel, spar_glm, spar_glmnet, spar
model_glmnet <- spar_glmnet(alpha = 0.5) example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar( example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, model = model_glmnet )model_glmnet <- spar_glmnet(alpha = 0.5) example_data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100) spar_res <- spar( example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, model = model_glmnet )
'coefspar' ObjectProvides a detailed summary of a coefspar object, including coefficient statistics.
## S3 method for class 'coefspar' summary(object, digits = 4L, ...)## S3 method for class 'coefspar' summary(object, digits = 4L, ...)
object |
An object of class |
digits |
Number of digits to be printed. Defaults to |
... |
Additional arguments passed to or from other methods. |
Invisibly returns input object.
example_data <- simulate_spareg_data(n = 100, p = 2000, ntest = 100) spar_res <- spareg(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) summary(coef(spar_res)) summary(coef(spar_res, aggregate = "none"))example_data <- simulate_spareg_data(n = 100, p = 2000, ntest = 100) spar_res <- spareg(example_data$x, example_data$y, xval = example_data$xtest, yval = example_data$ytest, nummods=c(5, 10)) summary(coef(spar_res)) summary(coef(spar_res, aggregate = "none"))