6 Convenient wrapper functions
Finally, we provide convenient wrapper functions which yield user-desired output in a single step at the price of flexibility and efficiency. cssSelect() yields a selected set of clusters and features (the same output as getCssSelections()) in a single function call. cssPredict() takes in a training/selection data set as well as a test X. It uses the labeled data to select a set of features and train an OLS model on the selected features, and then it generates predictions on the test set using the fitted model.
Besides requiring only a single function call to yield desired output, these wrapper functions also automatically select hyperparameters (lambda used for the lasso, a desired model size, and even selection and training splits for cssPredict()) in a sensible way if these values are not provided by the user. So these functions are very convenient for an end user who wants quick results without getting “under the hood.”
The two main disadvantages of these functions are flexibility and computational efficiency. For simplicity of use, these functions do not provide as many options as the component functions they call (for example, min_num_clusts is not an available argument for these models). Further, both of these functions make (computationally intensive) calls to css() internally every time they are called, so these functions are not recommended for users who want to tinker with the model size and other parameters. Instead, it would be more efficient to call css once and then work with the output as desired using the other package functions, which are very efficient given the stored output from css().
cssSelect() and cssPredict() have no new dependencies; they rely only on already-defined functions.
cssSelect():
#' Obtain a selected set of clusters and features using cluster stability
#' selection
#'
#' Takes in data X and y and returns a set of clusters (and a set of features)
#' that are useful for predicting y from the data in X. This is a wrapper
#' function for css and getCssSelections. Using cssSelect is simpler, but it
#' has fewer options, and it executes the full (computationally expensive)
#' subsampling procedured every time it is called. In contrast, css can be
#' called just once, and then getCssSelections can quickly return results using
#' different values of cutoff, max_num_clusts, etc. from the calculations done
#' in one call to css.
#' @param X An n x p numeric matrix (preferably) or a data.frame (which will
#' be coerced internally to a matrix by the function model.matrix) containing
#' the p >= 2 features/predictors. Must not contain missing (`NA`) values.
#' @param y A length-n numeric vector containing the responses; `y[i]` is the
#' response corresponding to observation `X[i, ]`. (Note that for the css
#' function, y does not have to be a numeric response, but for this function,
#' the underlying selection procedure is the lasso, so y must be a real-valued
#' response.)
#' @param clusters Optional; either an integer vector of a list of integer
#' vectors; each vector should contain the indices of a cluster of features (a
#' subset of 1:p). (If there is only one cluster, clusters can either be a list
#' of length 1 or an integer vector.) All of the provided clusters must be
#' non-overlapping. Every feature not appearing in any cluster will be assumed
#' to be unclustered (that is, they will be treated as if they are in a
#' "cluster" containing only themselves). If clusters is a list of length 0 (or
#' a list only containing clusters of length 1), then css() returns the same
#' results as stability selection (so feat_sel_mat will be identical to
#' clus_sel_mat). Names for the clusters will be needed later; any clusters that
#' are not given names in the list clusters will be given names automatically by
#' css. CAUTION: if the provided X is a data.frame that contains a categorical
#' feature with more than two levels, then the resulting matrix made from
#' model.matrix will have a different number of columns than the provided
#' data.frame, some of the feature numbers will change, and the clusters
#' argument will not work properly (in the current version of the package). To
#' get correct results in this case, please use model.matrix to convert the
#' data.frame to a numeric matrix on your own, then provide this matrix and
#' cluster assignments with respect to this matrix. Default is list() (so no
#' clusters are specified, and every feature is assumed to be in a "cluster"
#' containing only itself).
#' @param lambda Optional; the tuning parameter to be used by the lasso for
#' feature selection in each subsample. If lambda is not provided, cssSelect
#' will choose one automatically by cross-validation. Default is NA.
#' @param cutoff Numeric; cssSelect will only select those clusters with
#' selection proportions equal to at least cutoff. Must be between 0 and 1.
#' Default is NA (in which case max_num_clusts are used).
#' @param max_num_clusts Integer or numeric; the maximum number of clusters to
#' use regardless of cutoff. (That is, if the chosen cutoff returns more than
#' max_num_clusts clusters, the cutoff will be decreased until at most
#' max_num_clusts clusters are selected.) Default is NA (in which case
#' either cutoff is used to choose the number of clusters, or if cutoff was also
#' unspecified, cssSelect chooses max_num_clusts by cross-validation).
#' @param auto_select_size Logical; if TRUE, then max_num_clusts will be
#' automatically estimated using the lasso with cross-validation. Default is
#' TRUE, though this argument is ignored if either cutoff or max_num_clusts is
#' provided. (If desired output is to return all clusters, you should set
#' auto_select_size to FALSE and do not provide cutoff or max_num_clusts.)
#' @param alpha Numeric; the elastic net mixing parameter. Must be in `(0, 1]`.
#' Drives both the choice of lambda (when lambda is not provided) and the
#' elastic net fit used for feature selection in each subsample. Default is 1
#' (in which case the penalty is the lasso).
#' @return A named list with three items. \item{selected_clusts}{A named list of
#' integer vectors; each vector contains the indices of the features in one of
#' the selected clusters.} \item{selected_feats}{A named integer vector; the
#' indices of the features with nonzero weights from all of the selected
#' clusters.} \item{weights}{A named list of the same length as selected_clusts.
#' Each list element `weights[[j]]` is a numeric vector of the weights to use for
#' the jth selected cluster, and it has the same name as the cluster it
#' corresponds to.}
#' @author Gregory Faletto, Jacob Bien
#' @examples
#' set.seed(1)
#' data <- genClusteredData(n = 80, p = 11, k_unclustered = 2,
#' cluster_size = 4, n_clusters = 1, snr = 3)
#' clusters <- list(cluster1 = 1:4)
#' res <- cssSelect(X = data$X, y = data$y, clusters = clusters)
#' res$selected_feats
#' @export
cssSelect <- function(X, y, clusters = list(), lambda=NA, cutoff=NA,
max_num_clusts=NA, auto_select_size=TRUE, alpha=1){
# Check inputs (most inputs will be checked by called functions)
stopifnot(!is.na(auto_select_size))
stopifnot(length(auto_select_size) == 1)
stopifnot(is.logical(auto_select_size))
# Validate alpha up front. This must happen before the NA-lambda branch
# below (a user-supplied non-NA lambda skips getLassoLambda and therefore
# its own alpha check) and before the bundling step, so that an invalid or
# NA alpha is caught here with a clear message rather than deep inside the
# subsampling loop.
checkAlpha(alpha)
stopifnot(is.matrix(X) | is.data.frame(X))
checkNoNAs(X, "X")
# Check if x is a matrix; if it's a data.frame, convert to matrix.
X <- coerceDataFrameToMatrix(X, clusters, convert_phrase = "the data.frame X")
stopifnot(is.matrix(X))
if(!is.numeric(y) & !is.integer(y)){
stop("The provided y must be real-valued, because cssSelect uses the lasso for feature selection. (In order to use a different form of response, use the css function and provide your own selection function accommodating your choice of y.)")
}
# Reject a non-finite y here (after the domain message above, so that
# message is preserved) so the default lambda=NA -> getLassoLambda path
# fails fast rather than per-subsample later.
checkFiniteY(y, "y")
stopifnot(length(lambda) == 1)
if(is.na(lambda)){
lambda <- getLassoLambda(X, y, alpha=alpha)
}
# Bundle alpha into lambda for the elastic-net fit. When alpha == 1 (the
# default), leave lambda as a scalar so the pure-lasso path is byte-identical
# to the original behavior.
if(alpha != 1){
lambda <- c(lambda=lambda, alpha=alpha)
}
css_results <- css(X, y, lambda, clusters)
# If no indication of how to select model size was provided, choose model
# size by cross-validation
if(is.na(cutoff) & is.na(max_num_clusts)){
if(auto_select_size){
max_num_clusts <- getModelSize(X, y, css_results$clusters,
alpha=alpha)
}
}
if(is.na(cutoff)){
cutoff <- 0
}
# Get selected features
getCssSelections(css_results, weighting="sparse", cutoff=cutoff,
min_num_clusts=1, max_num_clusts=max_num_clusts)
}cssPredict():
#' Wrapper function to generate predictions from cluster stability selected
#' model in one step
#'
#' Select clusters using cluster stability selection, form cluster
#' representatives, fit a linear model, and generate predictions from a matrix
#' of unlabeled data. This is a wrapper function for css and getCssPreds. Using
#' cssPredict is simpler, but it has fewer options, and it executes the full
#' (computationally expensive) subsampling procedured every time it is called.
#' In contrast, css can be called just once, and then cssPredict can quickly
#' return results for different matrices of new data or using different values
#' of cutoff, max_num_clusts, etc. by using the calculations done in one call to
#' css.
#'
#' @param X_train_selec An n x p numeric matrix (preferably) or a data.frame
#' (which will be coerced internally to a matrix by the function model.matrix)
#' containing the p >= 2 features/predictors. The data from X_train_selec and
#' y_train_selec will be split into two parts; half of the data will be used for
#' feature selection by cluster stability selection, and half will be used for
#' estimating a linear model on the selected cluster representatives. Must not
#' contain missing (`NA`) values.
#' @param y_train_selec A length-n numeric vector containing the responses;
#' `y[i]` is the response corresponding to observation `X[i, ]`. Unlke the more
#' general setup of css, y_train_selec must be real-valued because predictions
#' will be generated by ordinary least squares.
#' @param X_test A numeric matrix (preferably) or a data.frame (which will
#' be coerced internally to a matrix by the function model.matrix) containing
#' the data that will be used to generate predictions. Must contain the same
#' features (in the same number of columns) as X_train_selec, and if the columns
#' of X_test are named, they must match the names of X_train_selec.
#' @param clusters Optional; either an integer vector of a list of integer
#' vectors; each vector should contain the indices of a cluster of features (a
#' subset of 1:p). (If there is only one cluster, clusters can either be a list
#' of length 1 or an integer vector.) All of the provided clusters must be
#' non-overlapping. Every feature not appearing in any cluster will be assumed
#' to be unclustered (that is, they will be treated as if they are in a
#' "cluster" containing only themselves). If clusters is a list of length 0 (or
#' a list only containing clusters of length 1), then css() returns the same
#' results as stability selection (so feat_sel_mat will be identical to
#' clus_sel_mat). Names for the clusters will be needed later; any clusters that
#' are not given names in the list clusters will be given names automatically by
#' css. CAUTION: if the provided X is a data.frame that contains a categorical
#' feature with more than two levels, then the resulting matrix made from
#' model.matrix will have a different number of columns than the provided
#' data.frame, some of the feature numbers will change, and the clusters
#' argument will not work properly (in the current version of the package). To
#' get correct results in this case, please use model.matrix to convert the
#' data.frame to a numeric matrix on your own, then provide this matrix and
#' cluster assignments with respect to this matrix.Default is list() (so no
#' clusters are specified, and every feature is assumed to be in a "cluster"
#' containing only itself).
#' @param lambda Optional; the tuning parameter to be used by the lasso for
#' feature selection in each subsample. If lambda is not provided, cssPredict
#' will choose one automatically by cross-validation. Default is NA.
#' @param cutoff Numeric; getCssPreds will make use only of those clusters with
#' selection proportions equal to at least cutoff. Must be between 0 and 1.
#' Default is 0 (in which case either all clusters are used, or max_num_clusts
#' are used, if max_num_clusts is specified).
#' @param max_num_clusts Integer or numeric; the maximum number of clusters to
#' use regardless of cutoff. (That is, if the chosen cutoff returns more than
#' max_num_clusts clusters, the cutoff will be decreased until at most
#' max_num_clusts clusters are selected.) Default is NA (in which case
#' max_num_clusts is ignored).
#' @param train_inds Optional; an integer or numeric vector containing the
#' indices of observations in X and y to set aside for model training after
#' feature selection. If train_inds is not provided, half of the data will be
#' used for feature selection and half for model estimation (chosen at random).
#' @param auto_select_size Logical; if TRUE, then max_num_clusts will be
#' automatically estimated using the lasso with cross-validation. Default is
#' TRUE, though this argument is ignored if either cutoff or max_num_clusts is
#' provided. (If desired output is to generate predictions using all clusters,
#' you should set auto_select_size to FALSE and do not provide cutoff or
#' max_num_clusts.)
#' @param alpha Numeric; the elastic net mixing parameter. Must be in `(0, 1]`.
#' Drives both the choice of lambda (when lambda is not provided) and the
#' elastic net fit used for feature selection in each subsample. Default is 1
#' (in which case the penalty is the lasso).
#' @return A numeric vector of length nrow(X_test) of predictions
#' corresponding to the observations from X_test.
#' @author Gregory Faletto, Jacob Bien
#' @examples
#' set.seed(1)
#' train <- genClusteredData(n = 80, p = 11, k_unclustered = 2,
#' cluster_size = 4, n_clusters = 1, snr = 3)
#' test <- genClusteredData(n = 10, p = 11, k_unclustered = 2,
#' cluster_size = 4, n_clusters = 1, snr = 3)
#' clusters <- list(cluster1 = 1:4)
#' preds <- cssPredict(X_train_selec = train$X, y_train_selec = train$y,
#' X_test = test$X, clusters = clusters)
#' preds
#' @export
cssPredict <- function(X_train_selec, y_train_selec, X_test, clusters=list(),
lambda=NA, cutoff=NA, max_num_clusts=NA, train_inds=NA,
auto_select_size=TRUE, alpha=1){
# Check inputs (most inputs will be checked by called functions)
if(!is.numeric(y_train_selec) & !is.integer(y_train_selec)){
stop("The provided y_train_selec must be real-valued, because predictions will be generated by ordinary least squares regression.")
}
stopifnot(!is.na(auto_select_size))
stopifnot(length(auto_select_size) == 1)
stopifnot(is.logical(auto_select_size))
# Validate alpha up front (before the NA-lambda branch and bundling below);
# see cssSelect for the ordering rationale.
checkAlpha(alpha)
stopifnot(is.matrix(X_train_selec) | is.data.frame(X_train_selec))
checkNoNAs(X_train_selec, "X_train_selec")
# Reject a non-finite y on the default path (lands after the numeric check
# at the top of the function, so the factor-y message there is preserved).
checkFiniteY(y_train_selec, "y_train_selec")
# Check if x is a matrix; if it's a data.frame, convert to matrix.
X_train_selec <- coerceDataFrameToMatrix(X_train_selec, clusters,
arg_name = "X_train_selec")
stopifnot(is.matrix(X_train_selec))
n <- nrow(X_train_selec)
if(any(is.na(train_inds))){
train_inds <- sample(n, size=round(n/2))
}
stopifnot(length(lambda) == 1)
if(is.na(lambda)){
lambda <- getLassoLambda(
X_train_selec[setdiff(1:n, train_inds), , drop = FALSE],
y_train_selec[setdiff(1:n, train_inds)], alpha=alpha)
}
# Bundle alpha into lambda for the elastic-net fit. When alpha == 1 (the
# default), leave lambda as a scalar so the pure-lasso path is byte-identical
# to the original behavior.
if(alpha != 1){
lambda <- c(lambda=lambda, alpha=alpha)
}
css_results <- css(X=X_train_selec, y=y_train_selec, lambda=lambda,
clusters=clusters, train_inds=train_inds)
# If no indication of how to select model size was provided, choose model
# size by cross-validation
if(is.na(cutoff) & is.na(max_num_clusts)){
if(auto_select_size){
max_num_clusts <- getModelSize(X_train_selec[train_inds, ],
y_train_selec[train_inds], css_results$clusters, alpha=alpha)
}
}
if(is.na(cutoff)){
cutoff <- 0
}
# Get predictions
getCssPreds(css_results, testX=X_test, weighting="weighted_avg",
cutoff=cutoff, max_num_clusts=max_num_clusts)
}