Package {spareg}


Type: Package
Title: Sparse Projected Averaged Regression
Version: 1.2.0
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.
License: GPL-3
Imports: Matrix, ROCR, Rdpack, ggplot2, rlang, glmnet, methods
RdMacros: Rdpack
Encoding: UTF-8
URL: https://github.com/lauravana/spareg
BugReports: https://github.com/lauravana/spareg/issues
Suggests: testthat (≥ 3.0.0), foreach, doParallel, doRNG, robustbase, cellWise, VariableScreening, ggpubr, R.matlab
Config/testthat/edition: 3
Depends: R (≥ 4.0.0)
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-09-25 15:22:35 UTC; lauravanagur
Author: Laura Vana-Gür ORCID iD [aut, cre], Roman Parzer ORCID iD [aut], Peter Filzmoser ORCID iD [aut]
Maintainer: Laura Vana-Gür <laura.vana.guer@tuwien.ac.at>
Repository: CRAN
Date/Publication: 2026-09-25 16:50:02 UTC

Coef Method for 'spar' Object

Description

Extracts coefficients from 'spar' object

Usage

## S3 method for class 'spar'
coef(
  object,
  nummod = NULL,
  nu = NULL,
  aggregate = c("mean", "median", "none"),
  ...
)

Arguments

object

result of spar of class 'spar'.

nummod

number of models used to form coefficients; value with minimal validation measure is used if not provided.

nu

threshold level used to compute the coefficients; value with minimal validation measure is used if not provided.

aggregate

character, one of c("mean", "median", "none"), giving the method of aggregating the coefficients over the marginal models. If set to "none", the coefficients are not aggregated over the marginal models and a matrix of coefficients, one column for each marginal model, is returned. Otherwise the coefficients are aggregated using the specified method (mean or median). Defaults to mean aggregation.

...

further arguments passed to or from other methods.

Value

Returns an object of class 'coefspar', which is a list with elements

See Also

print.coefspar, summary.coefspar

Examples

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)

Coef Method for 'spar.cv' Object

Description

Extract 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 M and \nu 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 M and \nu values.

Usage

## S3 method for class 'spar.cv'
coef(
  object,
  nummod = NULL,
  nu = NULL,
  opt_par = c("best", "1se"),
  aggregate = c("mean", "median", "none"),
  ...
)

Arguments

object

result of spar.cv function of class 'spar.cv'. Refitting can be done using get_model().

nummod

optional number of models used to form coefficients.

nu

optional threshold level used to form coefficients.

opt_par

one of c("1se","best"), chooses whether to select the best pair of nus and nummods according to cross-validated (CV) measure, or the sparsest solution within one sd of that optimal CV measure; ignored when nummod and nu are given.

aggregate

character one of c("mean", "median", "none"). If set to "none" the coefficients are not aggregated over the marginal models, otherwise the coefficients are aggregated using the specified method (mean or median). Defaults to mean aggregation.

...

further arguments passed to or from other methods.

Value

List with elements

See Also

predict.spar.cv, get_model.spar.cv

Examples


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)


Constructor Function for Building 'randomprojection' Object

Description

Creates an object class 'randomprojection' using arguments passed by user.

Usage

constructor_randomprojection(
  name = NULL,
  generate_fun,
  update_fun = update_rp_default,
  update_rpm_w_data = update_rpm_identity,
  control = list()
)

Arguments

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:

  • object: An object of class randomprojection.

  • x: A matrix of standardized predictors.

  • y: A vector of standardized responses.

  • m: The target dimension for the projection.

  • included_vector: A vector of column indices for the variables to be included in the projection.

  • ...: Additional arguments passed from spar() or other functions.' due to the fact that screening is employed pre-projection.

update_fun

A function for updating the randomprojection object with data-specific information. This function must accept:

  • object: An object of class randomprojection.

  • 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_rp_default is used.

update_rpm_w_data

A function for updating an already-generated random projection matrix with data-dependent information. This function must accept:

  • rpm: The random projection matrix to update.

  • object: An object of class randomprojection.

  • included_vector: A vector of column indices for the variables included in the projection.

  • 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_rpm_identity is used, which leaves the matrix unchanged.

control

A list of control parameters for the random projection. Default: list(). These parameters are passed to generate_fun, update_fun, and update_rpm_w_data.

Details

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.

Value

Returns a function that, when called, creates and returns an object of class randomprojection.

Examples

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_res

Constructor Function for Building screencoef Objects

Description

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

Usage

constructor_screencoef(
  name = NULL,
  generate_fun,
  update_fun = update_screen_default
)

Arguments

name

character

generate_fun

function responsible for computing the screening coefficients. This function should have arguments x, y (the predictor matrix and response vector supplied by spar() where both have already been standardized by spar()), 'screencoef' object and ..., whereas all other potentially relevant arguments of spar() are passed internally to this function through the ellipsis.

update_fun

optional function for updating the screencoef object with information from the data passed to spar(). This function should have arguments object, which is a screencoef object, x, y (the predictor matrix and response vector supplied by spar() where both have already been standardized by spar()), family and ..., whereas all other potentially relevant arguments of spar() are passed internally to this function through the ellipsis. If update_fun is not provided, the object remains unchanged.

Details

Creates an object class screencoef using arguments passed by user.

Value

Returns a function that, when called, creates and returns an object of class 'screencoef'.

Examples

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_example

Constructor Function for Building sparmodel Object

Description

The created function will return a object of class sparmodel which constitutes of a list.

Usage

constructor_sparmodel(
  name = NULL,
  generate_fun,
  update_fun = update_sparmodel_default
)

Arguments

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 sparmodel object, x and y (matrix of predictors and vector of responses supplied by spar(), which have already been standardized), z (the matrix of projected predictors) and ..., whereas all other potentially relevant arguments of spar() are passed internally to this function through the ellipsis.

update_fun

optional function for updating the sparmodel object. This function should have arguments object, which is a 'screencoef' object, x, y (the predictor matrix and response vector supplied by spar() where both have already been standardized by spar()), family and ..., whereas all other potentially relevant arguments of spar() are passed internally to this function through the ellipsis.

Details

Creates an object of class sparmodel using arguments passed by user.

Value

Returns a function that, when called, creates and returns an object of class sparmodel.

Examples

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_res

Sparse Embedding Matrix

Description

Sparse Embedding Matrix

Usage

generate_cw(object, x, y, m, included_vector, ...)

Arguments

object

object of class 'randomprojection'

x

matrix of standardized predictors

y

vector of standardized response variable

m

goal dimension, which will be randomly sampled in the SPAR algorithm

included_vector

integer vector of column indices for the variables to be included in the random projection. These indices are produced in the screening step of the SPAR algorithm.

Value

(possibly sparse) matrix with m rows and length(included_vector) columns.


Gaussian Random Projection Matrix

Description

Gaussian Random Projection Matrix

Usage

generate_gaussian(object, x = NULL, y = NULL, m, included_vector, ...)

Arguments

object

object of class 'randomprojection'.

x

matrix of standardized predictors.

y

vector of standardized response variable.

m

goal dimension, which will be randomly sampled in the SPAR algorithm

included_vector

integer vector of column indices for the variables to be included in the random projection. These indices are produced in the screening step of the SPAR algorithm.

Value

Returns a matrix with m rows and length(included_vector) columns sampled from the normal distribution.


Generate screening coefficient based on correlation

Description

Generate screening coefficient based on correlation

Usage

generate_scrcoef_cor(object, x, y, ...)

Arguments

object

'screencoef' object

x

matrix of predictors

y

vector of responses

Value

vector of screening coefficients of length p


Screening coefficient based on glmnet coefficients

Description

Screening coefficient based on glmnet coefficients

Usage

generate_scrcoef_glmnet(object, x, y, ...)

Arguments

object

'screencoef' object

x

matrix of predictors

y

vector of responses

Value

vector of screening coefficients of length p


Generate screening coefficient based on marginal likelihood in univariate GLMs

Description

Generate screening coefficient based on marginal likelihood in univariate GLMs

Usage

generate_scrcoef_marglik(object, x, y, ...)

Arguments

object

'screencoef' object

x

matrix of predictors

y

vector of responses

Value

vector of screening coefficients of length p


Sparse Random Projection Matrix

Description

Sparse Random Projection Matrix

Usage

generate_sparse(object, x = NULL, y = NULL, m, included_vector, ...)

Arguments

object

object of class 'randomprojection'

x

matrix of standardized predictors

y

vector of standardized response variable

m

goal dimension, which will be randomly sampled in the SPAR algorithm

included_vector

integer vector of column indices for the variables to be included in the random projection. These indices are produced in the screening step of the SPAR algorithm.

Value

Returns a (possibly sparse) matrix with m rows and length(included_vector) columns.


Extractor for Model Coefficients from 'coefspar' Object

Description

Extractor for Model Coefficients from 'coefspar' Object

Usage

get_coef(x)

Arguments

x

A 'coefspar' object.

Value

A numeric vector or matrix of coefficients.

See Also

coef.spar, coef.spar.cv, print.coefspar, summary.coefspar

Examples

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)

Extractor for Model Intercept from 'coefspar' Object

Description

Extractor for Model Intercept from 'coefspar' Object

Usage

get_intercept(x)

Arguments

x

A 'coefspar' object.

Value

Intercept (numeric or vector).

Examples

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)

Extractor for (Cross-)Validation Measure from 'spar' or 'spar.cv' Object

Description

Extractor for (Cross-)Validation Measure from 'spar' or 'spar.cv' Object

Usage

get_measure(object)

Arguments

object

A fitted 'spar' or 'spar.cv' model

Value

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.

See Also

spar, spar.cv, get_model

Examples

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)


get_model

Description

Retrieve the Best Model from a Fitted Object

Usage

get_model(object, ...)

Arguments

object

An object of class spar or spar.cv, typically the result of a model fitting function.

...

Additional arguments passed to the specific method.

Details

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.

Value

The modified object with the best model selected.

Examples

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


Get Best Model for spar Objects

Description

Extracts the best model from a fitted spar object based on validation results. The best model is determined by the minimum validation measure.

Usage

## S3 method for class 'spar'
get_model(object, ...)

Arguments

object

An object of class spar, typically the result of spar().

...

Additional arguments (currently unused).

Value

An updated spar object containing:

Examples


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)


Get Best Model for spar.cv Objects

Description

Extracts 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.

Usage

## S3 method for class 'spar.cv'
get_model(
  object,
  opt_par = c("best", "1se"),
  x = NULL,
  y = NULL,
  xval = NULL,
  yval = NULL,
  ...
)

Arguments

object

An object of class spar.cv, typically the result of spar.cv().

opt_par

A character string specifying the selection criterion:

  • "best": Selects the model with the minimum validation measure.

  • "1se": Selects the simplest model within 1 standard error of the best model.

x

A matrix or data frame of predictors. Required if precompute_mode != "precompute_all".

y

A response vector. Required if precompute_mode != "precompute_all".

xval

A matrix or data frame of validation predictors. If NULL, x is used.

yval

A validation response vector. If NULL, y is used.

...

Additional arguments passed to spar() for re-estimation.

Value

An updated spar or spar.cv object containing:

Examples


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


Plot Method for 'spar' Object

Description

Creates diagnostic plots for a 'spar'] object, including:

Usage

## 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,
  ...
)

Arguments

x

A 'spar' object.

plot_type

The type of plot. Options:

  • "val_measure" (default): Validation measure vs. nus or nummods.

  • "val_numactive": Number of active variables vs. nus or nummods.

  • "res_vs_fitted": Residuals vs. fitted values.

  • "coefs": Heatmap of coefficients.

plot_along

The variable to plot along the x-axis. Options:

  • "nu" (default): Threshold values.

  • "nummod": Number of marginal models. Ignored if plot_type = "res_vs_fitted" or plot_type = "coefs".

nummod

The number of models to fix when plot_along = "nu". If NULL, the optimal value is used. The value will be used in predict.spar when plot_type="res_vs_fitted".

nu

The threshold value to fix when plot_along = "nummod". If NULL, the optimal value is used. The value will be used in predict.spar when plot_type="res_vs_fitted".

xfit

The predictor data for fitted values (required if plot_type = "res_vs_fitted").

yfit

The response data for fitted values (required if plot_type = "res_vs_fitted").

prange

A vector of length 2 specifying the range of predictors to plot (for plot_type = "coefs"). Default: c(1, p).

coef_order

An optional vector specifying the order of coefficients for plot_type = "coefs". Default: 1:p (original order).

digits

The number of significant digits for axis labels. Default: 2L.

...

Additional arguments passed to or from other methods.

Value

Returns a 'ggplot2::ggplot' object.

Examples

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

Plot Method for 'spar.cv' Object

Description

Plot 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.

Usage

## 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,
  ...
)

Arguments

x

result of spar.cv function of class 'spar.cv'.

plot_type

one of c("val_measure","val_numactive", "res_vs_fitted", "coefs").

plot_along

one of c("nu","nummod").

nummod

fixed value for nummod when plot_along="nu" for plot_type="val_measure" or "val_numactive";

nu

fixed value for \nu when plot_along="nummod" for plot_type="val_measure" or "val_numactive"; same as for predict.spar.cv when plot_type="res_vs_fitted".

xfit

optional vector of fitted values to be used for plot_type="res_vs_fitted"; if not provided, the fitted values are computed using the best parameters. Argument is used only if precompute_mode = "precompute_all" and plot_type = "res_vs_fitted".

yfit

optional vector of response values to be used for plot_type="res_vs_fitted"; if not provided, the response values are computed using the best parameters. Argument is used only if precompute_mode = "precompute_all" and plot_type = "res_vs_fitted".

opt_par

one of c("1se","best"), chooses whether to select the best pair of nus and nummods according to cross-validated (CV) measure, or the sparsest solution within one sd of that optimal CV measure. Argument is used only if precompute_mode = "precompute_all" and plot_type = "res_vs_fitted".

prange

optional vector of length 2 indicating the range of predictors to be plotted for plot_type = "coefs"; defaults to c(1, p) where p is the number of predictors. Argument can be used only if precompute_mode = "precompute_all".

coef_order

optional vector of length p indicating the order of predictors to be plotted for plot_type = "coefs"; defaults to seq_len(p) where p is the number of predictors. Argument can be used only if precompute_mode = "precompute_all".

digits

number of significant digits to be displayed in the axis; defaults to 2L.

...

further arguments passed to or from other methods

Value

'ggplot2::ggplot' object

Examples


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)


Predict Method for 'spar.cv' Object

Description

Predict responses for new predictors from 'spar' object

Usage

## 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"),
  ...
)

Arguments

object

result of spar function of class 'spar'.

xnew

matrix of new predictor variables; must have same number of columns as x.

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

Value

Returns a vector of predictions.

Examples

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)

Predict Method for 'spar.cv' Object

Description

Predict 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.

Usage

## 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"),
  ...
)

Arguments

object

result of spar function of class 'spar.cv'.

xnew

matrix of new predictor variables; must have same number of columns as x.

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 c("best","1se"), chooses whether to select the best pair of nus and nummods according to CV measure, or the sparsest solution within one sd of that optimal CV measure; ignored when nummod and nu, or coef are given

nummod

number of models used to form coefficients; value with minimal validation Meas is used if not provided.

nu

threshold level used to form coefficients; value with minimal validation Meas 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

Value

Vector of predictions

See Also

coef.spar.cv, get_model.spar.cv

Examples


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)


Print Method for 'coefspar' Object

Description

Prints a summary of coefficients from a 'coefspar' object, including the selected M and \nu, and the number of active variables.

Usage

## S3 method for class 'coefspar'
print(x, digits = 4L, show = 6L, ...)

Arguments

x

A 'coefspar' object.

digits

The number of significant digits for numeric output. Default: 4L.

show

The number of coefficients to display. Default: 6L.

...

Additional arguments passed to or from other methods.

Value

Invisibly returns the input object x.

Examples

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)

Print Method for a 'randomprojection' Object

Description

Prints a summary of a randomprojection object, including its name, and key attributes such as mslow and msup.

Usage

## S3 method for class 'randomprojection'
print(x, ...)

Arguments

x

An object of class randomprojection.

...

Additional arguments (ignored).

Value

Invisibly returns the input object x.

Examples

rp <- rp_gaussian(mslow = 5, msup = 10)
print(rp)


Print Method for 'screencoef' Object

Description

Print method for a 'screencoef' object

Usage

## S3 method for class 'screencoef'
print(x, ...)

Arguments

x

description

...

further arguments passed to or from other methods

Value

text summary


Print summary of 'spar' Object

Description

Prints a summary of a spar object, including the validation measure, optimal M and \nu, and the number of active predictors.

Usage

## S3 method for class 'spar'
print(x, digits = 4L, ...)

Arguments

x

A 'spar' object.

digits

The number of significant digits for numeric output. Default: 4L.

...

Additional arguments passed to or from other methods.

Value

Invisibly returns the input object x.

Examples

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)

Print Method for 'spar.cv' Object

Description

Print summary of 'spar.cv' object

Usage

## S3 method for class 'spar.cv'
print(x, digits = 4L, ...)

Arguments

x

result of spar.cv function of class 'spar.cv'.

digits

integer digits to be printed, defaults to 4L.

...

further arguments passed to or from other methods

Value

text summary

Examples


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)


Random Projection Object Class

Description

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:

Details

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:

Attributes

The following attributes are commonly used in randomprojection objects:

Usage

'randomprojection' objects are typically created using predefined constructors like:

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.

References

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

See Also

constructor_randomprojection, rp_gaussian, rp_sparse, rp_cw, spar

Examples

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
)


Sparse Embedding Matrix

Description

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

Usage

rp_cw(..., control = list())

Arguments

...

includes arguments which can be passed as attributes to the random projection matrix

control

list of arguments to be used in functions generate_fun, update_fun, update_rpm_w_data

Details

The entries of the matrix are generated based on (Clarkson and Woodruff 2013). This matrix is constructed as \Phi=BD\in \mathbb{R}^{m\times p}, where B is a (p\times p) binary matrix, where for each column j an index is uniformly sampled from \{1,\ldots,m\} and the corresponding entry is set to one, and D is a (p\times p) diagonal matrix, with entries d_j \sim \text{Unif}(\{-1, 1\}). 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:

Value

object of class 'randomprojection' which is a list with elements name, generate_fun, update_fun, control

References

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

Examples

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


Gaussian Random Projection Matrix

Description

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

Usage

rp_gaussian(..., control = list())

Arguments

...

includes arguments which can be passed as attributes to the random projection matrix

control

list of arguments to be used in functions generate_fun, update_fun, update_rpm_w_data

Details

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:

Value

Returns an object of class 'randomprojection' which is a list with elements name, generate_fun, update_fun, control

Examples

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


Sparse Random Projection Matrix

Description

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

Usage

rp_sparse(..., control = list())

Arguments

...

includes arguments which can be passed as attributes to the random projection matrix.

control

list of arguments to be used in functions generate_fun, update_fun, update_rpm_w_data

Details

The sparse matrix used in (Achlioptas 2003) with entries equal to \Psi_{ij} = \pm 1/\sqrt{\psi} with probability \psi/2 and zero otherwise for \psi\in (0,1]. 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:

Value

object of class 'randomprojection' which is a list with elements name, generate_fun, update_fun, control

References

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.

Examples

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


Screening Coefficient Based on Correlation

Description

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.

Usage

screen_cor(..., control = list())

Arguments

...

includes arguments which can be passed as attributes to the 'screencoef' object

control

list of controls to be passed to the screening function

Details

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.

Value

object of class 'screencoef' which is a list with elements

See Also

constructor_screencoef, screencoef

Examples

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


Screening Coefficient Based on glmnet Coefficients

Description

Creates an object class 'screencoef' using arguments passed by user, where the screening coefficient should be computed based on penalized coefficients.

Usage

screen_glmnet(..., control = list())

Arguments

...

includes arguments which can be passed as attributes to the 'screencoef' object

control

list of controls to be passed to the screening function

Details

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.

Value

object of class 'screencoef' which is a list with elements

See Also

constructor_screencoef, screencoef

Examples

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


Screening Coefficient Based on Marginal GLMs

Description

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.

Usage

screen_marglik(..., control = list())

Arguments

...

includes arguments which can be passed as attributes to the 'screencoef' object

control

list of controls to be passed to the screening function

Details

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.

Value

object of class 'screencoef' which is a list with elements:

See Also

constructor_screencoef, screencoef

Examples

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


Screening Coefficient Object Class

Description

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:

Screening coefficients are used to reduce the dimensionality of the predictor space by selecting the most relevant variables before applying random projections.

Details

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:

Attributes

The following attributes are commonly used in screencoef objects:

Screening Methods

The following predefined screening methods are available:

Users can also define custom screening methods by providing their own generate_fun and update_fun functions to constructor_screencoef.

See Also

constructor_screencoef, screen_marglik, screen_cor, screen_glmnet, spar

Examples

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
)


Simulate Sparse Regression Data

Description

Generates synthetic data for sparse linear regression problems. Returns training and test sets along with model parameters.

Usage

simulate_spareg_data(
  n,
  p,
  ntest,
  a = min(100, p/4),
  snr = 10,
  rho = 0.5,
  mu = 1,
  beta_vals = NULL,
  seed = NULL
)

Arguments

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.

Value

A list with the following components:

x

Training design matrix ⁠(n x p)⁠.

y

Training response vector (length n).

xtest

Test design matrix (⁠ntest x p⁠).

ytest

Test response vector (length ntest).

mu

Intercept used in data generation.

beta

True coefficient vector (length p).

sigma2

Noise variance used in data generation. Equals \beta' \Sigma \beta/snr.

Examples

set.seed(123)
data <- simulate_spareg_data(n = 200, p = 2000, ntest = 100)
str(data)


Sparse Projected Averaged Regression (SPAR)

Description

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.

Usage

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

Arguments

x

An ⁠n x p⁠ numeric matrix of predictor variables.

y

A quantitative response vector of length n.

family

A family object used for the marginal generalized linear model, default gaussian("identity").

model

A function that creates a 'sparmodel' object. Defaults to:

  • spar_glm() for gaussian family with identity link.

  • spar_glmnet() for all other family-link combinations.

rp

A function that creates a 'randomprojection' object. Defaults to NULL. If NULL, rp_cw(data = TRUE) is used.

screencoef

function that creates a 'screencoef' object. Defaults to NULL. If NULL, no screening is used.

xval

An optional matrix of predictor variables used for validation. If NULL, x is used.

yval

An optional response vector for validation. If NULL, y is used.

nnu

number of different threshold values \nu for thresholding. Ignored if nus is provided. Defaults to 20L.

nus

An optional vector of thresholds \nu. If NULL, nnu values between 0 and the maximum absolute marginal coefficient equally spaced on the probability scale are used (i.e., we use as grid for the thresholds zero and nnu-1 quantiles of the absolute values of the estimated non-zero coefficients from the marginal models).

nummods

A vector of integers specifying the number of marginal models to consider for validation. Defaults to 20L.

measure

The loss function for validation. Options:

  • "deviance" (default, available for all families).

  • "mse" or "mae" (mean squared/absolute error, for all families).

  • "class" (misclassification error, for binomial family only).

  • "1-auc" (one minus area under the ROC curve for binomial family only).

avg_type

The type of averaging for marginal models. Options:

  • "link" (default): Averaging on the link scale.

  • "response": Averaging on the response scale.

parallel

A logical indicating whether to use parallel estimation of the marginal models. Defaults to FALSE.

inds

An optional list of index vectors corresponding to variables retained after screening for each marginal model. Must have length max(nummods).

RPMs

An optional list of projection matrices for each marginal model. Must have length max(nummods).

seed

An optional integer seed for reproducibility. Default: NULL.

...

Additional arguments for backward compatibility.

Details

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.

Value

An object of class spar with the following components:

References

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.

See Also

spar.cv, coef.spar, predict.spar, plot.spar, print.spar

Examples

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

Sparse Projected Averaged Regression with Cross-Validation

Description

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.

Usage

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"),
  ...
)

Arguments

x

n x p numeric matrix of predictor variables.

y

quantitative response vector of length n.

family

a 'family' object used for the marginal generalized linear model; defaults to gaussian("identity").

model

function creating a 'sparmodel' object; defaults to spar_glm() for gaussian family with identity link and to spar_glmnet() for all other family-link combinations.

rp

function creating a 'randomprojection' object. If not specified by user it defaults to rp_cw(data = TRUE).

screencoef

function creating a 'screeningcoef' object. If not specified by user it defaults to no screening.

nfolds

number of folds to use for cross-validation; should be at least 2, defaults to 10.

nnu

number of different threshold values \nu to consider for thresholding; ignored when nus is provided; defaults to 20.

nus

optional vector of \nu's to consider for thresholding; if not provided, nnu values ranging from 0 to the maximum absolute marginal coefficient are used.

nummods

vector of numbers of marginal models to consider for validation; defaults to c(20).

measure

loss to use for validation; defaults to "deviance" available for all families. Other options are "mse" or "mae" (between responses and predicted means, for all families), "class" (misclassification error) and "1-auc" (one minus area under the ROC curve) both just for binomial family.

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 c("precompute_all", "precompute_rpm", "full_cv"), indicating whether to use the same random projection matrix and/or the same indices for screening across folds. Defaults to "precompute_all".

...

further arguments mainly to ensure back-compatibility

Value

object of class 'spar.cv' with elements

See Also

spar, coef.spar.cv, predict.spar.cv, plot.spar.cv, print.spar.cv

Examples


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


GLM Marginal sparmodel

Description

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.

Usage

spar_glm(..., control = list())

Arguments

...

includes arguments which can be passed as attributes to the sparmodel object

control

list of controls to be passed to the model function

Details

Relies on glm.

Value

Returns an object of class sparmodel.

See Also

sparmodel, stats::glm

Examples

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

Penalized GLM Marginal sparmodel

Description

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 \alpha=0 and return the coefficients obtained with the penalty \lambda_\text{min}.

Usage

spar_glmnet(..., control = list())

Arguments

...

includes arguments which can be passed as attributes to the sparmodel object

control

list of controls to be passed to the model function

Details

Relies on glmnet.

Value

Returns an object of class sparmodel.

See Also

sparmodel, glmnet::glmnet

Examples

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


SPAR Model Object Class

Description

The sparmodel class represents a configuration for fitting marginal models in the SPAR (Sparse Projected Averaged Regression) framework. Objects of this class encapsulate:

Marginal models are fitted to projected predictors (after screening and random projection) to compute coefficients and intercepts for each model in the ensemble.

Details

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:

Model Types

The following predefined model types are available:

See Also

constructor_sparmodel, spar_glm, spar_glmnet, spar

Examples

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
)


Summary Method for 'coefspar' Object

Description

Provides a detailed summary of a coefspar object, including coefficient statistics.

Usage

## S3 method for class 'coefspar'
summary(object, digits = 4L, ...)

Arguments

object

An object of class coefspar.

digits

Number of digits to be printed. Defaults to 4L.

...

Additional arguments passed to or from other methods.

Value

Invisibly returns input object.

Examples

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


Default Update Function for Random Projection Objects

Description

A default function to update a randomprojection object with the family information. This function is used internally by constructor_randomprojection if no custom update_fun is provided.

Usage

update_rp_default(object, x, y, family, ...)

Arguments

object

An object of class randomprojection.

x

A matrix of standardized predictors (unused in this default function).

y

A vector of standardized responses (unused in this default function).

family

A stats::family object specifying the GLM family and link function.

...

Additional arguments (ignored).

Value

The updated 'randomprojection' object with the family_string attribute set.


Default Function to Update Random Projection Matrices

Description

A default function that returns the input random projection matrix unchanged. This function is used internally by constructor_randomprojection if no custom update_rpm_w_data is provided.

Usage

update_rpm_identity(rpm, object, included_vector, x, y, family, ...)

Arguments

rpm

A random projection matrix.

object

An object of class randomprojection (unused in this default function).

included_vector

A vector of column indices for variables included in the projection (unused in this default function).

x

A matrix of standardized predictors (unused in this default function).

y

A vector of standardized responses (unused in this default function).

family

A stats::family object (unused in this default function).

...

Additional arguments (ignored).

Value

The input rpm matrix, unchanged.


Update screening glmnet object

Description

Update screening glmnet object

Usage

update_screen_glmnet(object, x, y, family, ...)

Arguments

object

'screencoef' object

x

matrix of predictors

y

vector of responses

family

family object to be passed from spar()

Value

vector of screening coefficients of length p