R/biglasso.R

Defines functions biglasso

Documented in biglasso

#' Fit lasso penalized regression path for big data
#'
#' Extend lasso model fitting to big data that cannot be loaded into memory. Fit solution paths for
#' linear, logistic or Cox regression models penalized by lasso, ridge, or elastic-net over a grid
#' of values for the regularization parameter lambda.
#'
#' The objective function for linear regression or multiple responses linear regression (`family =
#' "gaussian"` or `family = "mgaussian"`) is
#'
#' \deqn{\frac{1}{2n}\textrm{RSS} + \lambda \cdot \textrm{penalty},}{(1/(2n))*RSS+
#' \lambda*penalty,}
#'
#' where for `family = "mgaussian"`), a group-lasso type penalty is applied. For logistic regression
#' (`family = "binomial"`) it is
#'
#' \deqn{-\frac{1}{n} \ell + \lambda \cdot \textrm{penalty},}{-(1/n)*loglik+\lambda*penalty,}
#'
#' for cox regression, Breslow approximation for ties is applied.
#'
#' Several advanced feature screening rules are implemented. For lasso-penalized linear regression,
#' all the options of `screen` are applicable. Our proposal adaptive rule - `"Adaptive"` - achieves
#' highest speedup so it's the recommended one, especially for ultrahigh-dimensional large-scale
#' data sets. For cox regression and/or the elastic net penalty, only `"SSR"` is applicable for now.
#' More efficient rules are under development.
#'
#' @param X The design matrix, without an intercept. It must be a double type
#'   [bigmemory::big.matrix()] object. The function standardizes the data and includes an intercept
#'   internally by default during the model fitting.
#' @param y The response vector for `family="gaussian"` or `family="binomial"`. For `family="cox"`,
#'   `y` should be a two-column matrix with columns 'time' and 'status'. The latter is a binary
#'   variable, with '1' indicating death, and '0' indicating right censored. For
#'   `family="mgaussin"`, `y` should be a n*m matrix where n is the sample size and m is the number
#'   of responses.
#' @param row.idx The integer vector of row indices of `X` that used for fitting the model.
#'   `1:nrow(X)` by default.
#' @param penalty The penalty to be applied to the model. Either `"lasso"` (the default), `"ridge"`,
#'   or `"enet"` (elastic net).
#' @param family Either `"gaussian"`, `"binomial"`, `"cox"` or `"mgaussian"` depending on the
#'   response.
#' @param alg.logistic The algorithm used in logistic regression. If "Newton" then the exact hessian
#'   is used (default); if "MM" then a majorization-minimization algorithm is used to set an
#'   upper-bound on the hessian matrix. This can be faster, particularly in data-larger-than-RAM
#'   case.
#' @param screen The feature screening rule used at each `lambda` that discards features
#'   to speed up computation: `"SSR"` (default if `penalty="ridge"` or `penalty="enet"`)
#'   is the sequential strong rule; `"Hybrid"`, which combines the strong rule with a safe
#'   rule. `"Adaptive"` (default for `penalty="lasso"` without `penalty.factor`), which is
#'   a variant of Hybrid that avoids recalculating the safe rule when it would not be
#'   efficient to do so. Valid values are family-dependent: `"Hybrid"` is only available
#'   for `family="gaussian"`; for `family="cox"`, `"Hybrid"` is not available but three
#'   additional cox-specific safe-rule variants are: `"scox"`, `"sscox"`, and `"safe"`.
#'   **Note that:**
#'     1. For linear regression with elastic net penalty, both `"SSR"` and `"Hybrid"` are
#'        applicable since version 1.3-0
#'     2. Only `"SSR"` is applicable to elastic-net-penalized logistic regression or cox regression
#'     3. Active set cycling is incorporated into all of these screening rules
#' @param safe.thresh the threshold value between 0 and 1 that controls when to stop safe test. For
#'   example, 0.01 means to stop safe test at next lambda iteration if the number of features
#'   rejected by safe test at current lambda iteration is not larger than 1% of the total number of
#'   features. So 1 means to always turn off safe test, whereas 0 (default) means to turn off safe
#'   test if the number of features rejected by safe test is 0 at current lambda.
#' @param update.thresh the non negative threshold value that controls how often to update the
#'   reference of safe rules for "Adaptive" methods. Smaller value means updating more often.
#' @param ncores The number of OpenMP threads used for parallel computing.
#' @param alpha The elastic-net mixing parameter that controls the relative contribution from the
#'   lasso (l1) and the ridge (l2) penalty. The penalty is defined as
#'
#'   \deqn{ \alpha||\beta||_1 + (1-\alpha)/2||\beta||_2^2.}
#'
#' `alpha=1` is the lasso penalty, `alpha=0` the ridge penalty, `alpha` in between 0 and 1 is the
#' elastic-net ("enet") penalty.
#' @param lambda.min The smallest value for lambda, as a fraction of lambda.max. Default is 0.001 if
#'   the number of observations is larger than the number of covariates and 0.05 otherwise.
#' @param nlambda The number of lambda values. Default is 100.
#' @param lambda.log.scale Whether compute the grid values of lambda on log scale (default) or
#'   linear scale.
#' @param lambda A user-specified sequence of lambda values. By default, a sequence of values of
#'   length `nlambda` is computed, equally spaced on the log scale.
#' @param eps Convergence threshold for inner coordinate descent. The algorithm iterates until the
#'   maximum change in the objective after any coefficient update is less than `eps` times the null
#'   deviance. Default value is `1e-7`.
#' @param max.iter Maximum number of iterations. Default is 1000.
#' @param dfmax Upper bound for the number of nonzero coefficients. Default is no upper bound.
#'   However, for large data sets, computational burden may be heavy for models with a large number
#'   of nonzero coefficients.
#' @param penalty.factor A multiplicative factor for the penalty applied to each coefficient. If
#'   supplied, `penalty.factor` must be a numeric vector of length equal to the number of columns of
#'   `X`. The purpose of `penalty.factor` is to apply differential penalization if some coefficients
#'   are thought to be more likely than others to be in the model. Currently, the package doesn't
#'   allow unpenalized coefficients (`penalty.factor` cannot be 0). `penalty.factor` is only
#'   supported for "SSR" screen.
#' @param warn Return warning messages for failures to converge and model saturation? Default is
#'   TRUE.
#' @param output.time Whether to print out the start and end time of the model fitting. Default is
#'   FALSE.
#' @param return.time Whether to return the computing time of the model fitting. Default is TRUE.
#' @param verbose Whether to output the timing of each lambda iteration. Default is FALSE.
#'
#' @returns An object with S3 class `"biglasso"` for `"gaussian", "binomial", "cox"` families, or an
#'   object with S3 class `"mbiglasso"` for `"mgaussian"` family, with following variables.
#'
#' \describe{
#'   \item{beta}{
#'     The fitted matrix of coefficients, store in sparse matrix representation. The number of rows
#'     is equal to the number of coefficients, whereas the number of columns is equal to `nlambda`.
#'     For `"mgaussian"` family with m responses, it is a list of m such matrices.
#'   }
#'   \item{iter}{
#'     A vector of length `nlambda` containing the number of iterations until convergence at each
#'     value of `lambda`.
#'   }
#'   \item{lambda}{The sequence of regularization parameter values in the path.}
#'   \item{penalty}{Same as above.}
#'   \item{family}{Same as above.}
#'   \item{alpha}{Same as above.}
#'   \item{loss}{
#'     A vector containing either the residual sum of squares (for `"gaussian", "mgaussian"`) or
#'     negative log-likelihood (for `"binomial", "cox"`) of the fitted model at each value of
#'     `lambda`.
#'   }
#'   \item{penalty.factor}{Same as above.}
#'   \item{n}{
#'     The number of observations used in the model fitting. It's equal to `length(row.idx)`.
#'   }
#'   \item{center}{
#'     The sample mean vector of the variables, i.e., column mean of the sub-matrix of `X` used for
#'     model fitting.
#'   }
#'   \item{scale}{
#'     The sample standard deviation of the variables, i.e., column standard deviation of the
#'     sub-matrix of `X` used for model fitting.
#'   }
#'   \item{y}{
#'     The response vector used in the model fitting. Depending on `row.idx`, it could be a subset
#'     of the raw input of the response vector y.
#'   }
#'   \item{screen}{Same as above.}
#'   \item{col.idx}{
#'     The indices of features that have 'scale' value greater than 1e-6. Features with 'scale' less
#'     than 1e-6 are removed from model fitting.
#'   }
#'   \item{rejections}{The number of features rejected at each value of `lambda`.}
#'   \item{safe_rejections}{
#'     The number of features rejected by safe rules at each value of `lambda`.
#'   }
#' }
#'
#' @references
#'   Zeng Y and Breheny P. (2021) The biglasso Package: A Memory- and Computation-
#'   Efficient Solver for Lasso Model Fitting with Big Data in R. *R Journal*, 12: 6-19.
#'   \doi{10.32614/RJ-2021-001}
#' @seealso [setupX()], [cv.biglasso()], [plot.biglasso()], [ncvreg::ncvreg()]
#'
#' @examples
#' ## Linear regression
#' data(colon)
#' X <- colon$X
#' y <- colon$y
#' x_bm <- as.big.matrix(X)
#' # lasso, default
#' fit_lasso <- biglasso(x_bm, y, family = "gaussian")
#' plot(fit_lasso, log.l = TRUE, main = "lasso")
#' # elastic net
#' fit_enet <- biglasso(
#'   x_bm,
#'   y,
#'   penalty = "enet",
#'   alpha = 0.5,
#'   family = "gaussian"
#' )
#' plot(fit_enet, log.l = TRUE, main = "elastic net")
#'
#' ## Logistic regression
#' fit_bin_lasso <- biglasso(x_bm, y, penalty = "lasso", family = "binomial")
#' plot(fit_bin_lasso, log.l = TRUE, main = "lasso")
#' # elastic net
#' fit_bin_enet <- biglasso(
#'   x_bm,
#'   y,
#'   penalty = "enet",
#'   alpha = 0.5,
#'   family = "binomial"
#' )
#' plot(fit_bin_enet, log.l = TRUE, main = "elastic net")
#'
#' ## Cox regression
#' set.seed(10101)
#' n <- 1000
#' p <- 30
#' nzc <- p / 3
#' x <- matrix(rnorm(n * p), n, p)
#' beta <- rnorm(nzc)
#' fx <- x[, seq(nzc)] %*% beta / 3
#' hx <- exp(fx)
#' ty <- rexp(n, hx)
#' tcens <- quantile(ty, 0.7)
#' y <- cbind(time = pmin(ty, tcens), status = ty < tcens)
#' x_bm <- as.big.matrix(x)
#' fit <- biglasso(x_bm, y, family = "cox")
#' plot(fit, main = "cox")
#'
#' ## Multiple responses linear regression
#' set.seed(10101)
#' n <- 300
#' p <- 300
#' m <- 5
#' s <- 10
#' b <- 1
#' x <- matrix(rnorm(n * p), n, p)
#' beta <- matrix(seq(from = -b, to = b, length.out = s * m), s, m)
#' y <- x[, 1:s] %*% beta + matrix(rnorm(n * m, 0, 1), n, m)
#' x_bm <- as.big.matrix(x)
#' fit <- biglasso(x_bm, y, family = "mgaussian")
#' plot(fit, main = "mgaussian")
#'
#' @export
biglasso <- function(
  X,
  y,
  row.idx = 1:nrow(X),
  penalty = c("lasso", "ridge", "enet"),
  family = c("gaussian", "binomial", "cox", "mgaussian"),
  alg.logistic = c("Newton", "MM"),
  screen = c("Adaptive", "SSR", "Hybrid", "None"),
  safe.thresh = 0,
  update.thresh = 1,
  ncores = 1,
  alpha = 1,
  lambda.min = ifelse(nrow(X) > ncol(X), .001, .05),
  nlambda = 100,
  lambda.log.scale = TRUE,
  lambda,
  eps = 1e-7,
  max.iter = 1000,
  dfmax = ncol(X) + 1,
  penalty.factor = rep(1, ncol(X)),
  warn = TRUE,
  output.time = FALSE,
  return.time = TRUE,
  verbose = FALSE
) {
  # set up defaults -------------------------------------------------------

  family <- match.arg(family)
  penalty <- match.arg(penalty)
  alg.logistic <- match.arg(alg.logistic)

  # Valid `screen` values are family-dependent (see biglasso-dispatch.R);
  # "MM" is an internal pseudo-key (selected via alg.logistic, not `screen`),
  # not a user-facing screen value.
  valid_screen <- names(biglasso_dispatch_table[[family]])
  valid_screen <- valid_screen[valid_screen != "MM"]

  if (!identical(penalty, "lasso") || any(penalty.factor != 1) || alg.logistic == "MM") {
    if (length(screen) == 1) {
      screen <- match.arg(screen, choices = valid_screen)
    } else {
      screen <- "SSR"
    }
  } else if (family == "cox") {
    if (length(screen) == 1) {
      screen <- match.arg(screen, choices = valid_screen)
    } else {
      screen <- "SSR"
    }
  } else {
    if (length(screen) == 1) {
      screen <- match.arg(screen, choices = valid_screen)
    } else {
      screen <- "Adaptive"
    }
  }
  lambda.min <- max(lambda.min, 1.0e-6)

  if (identical(penalty, "lasso")) {
    alpha <- 1
  } else if (identical(penalty, "ridge")) {
    alpha <- 1.0e-6 ## equivalent to ridge regression
    if (screen == "Adaptive") {
      warning(
        "For now \"ridge\" does not support \"Adaptive\" screen. Automatically switching to \"SSR\"."
      )
      screen <- "SSR"
    }
  } else if (identical(penalty, "enet")) {
    if (alpha >= 1 || alpha <= 0) {
      stop("alpha must be between 0 and 1 for elastic net penalty.")
    }
    if (screen == "Adaptive") {
      warning(
        "For now \"enet\" does not support \"Adaptive\" screen. Automatically switching to \"SSR\"."
      )
      screen <- "SSR"
    }
  }

  if (!("big.matrix" %in% class(X)) || typeof(X) != "double") {
    stop("X must be a double type big.matrix.")
  }
  if (nlambda < 2) {
    stop("nlambda must be at least 2")
  }
  # subset of the response vector
  if (is.matrix(y)) {
    y <- y[row.idx, ]
  } else {
    y <- y[row.idx]
  }

  if (any(is.na(y))) {
    stop(
      "Missing data (NA's) detected.  Take actions (e.g., removing cases, removing features, imputation) to eliminate missing data before fitting the model."
    )
  }

  if (!is.double(y)) {
    if (is.matrix(y)) {
      tmp <- try(storage.mode(y) <- "numeric", silent = TRUE)
    } else {
      tmp <- try(y <- as.numeric(y), silent = TRUE)
    }
    if (class(tmp)[1] == "try-error") stop("y must numeric or able to be coerced to numeric")
  }

  # Placeholders so family-specific dispatch tokens (see biglasso-dispatch.R)
  # can be assembled unconditionally below regardless of family: e.g. 'd' is
  # only meaningful for family == "cox", but harmlessly evaluates to a
  # zero-length value for other families rather than an undefined-object
  # error, since no other family's dispatch table entry ever selects it.
  n.pos <- ylab <- d <- d_idx <- tOrder <- row.idx.cox <- NULL

  if (family == "binomial") {
    if (length(table(y)) > 2) {
      stop("Attemping to use family='binomial' with non-binary data")
    }
    if (!identical(sort(unique(y)), 0:1)) {
      y <- as.numeric(y == max(y))
    }
    n.pos <- sum(y) # number of 1's
    ylab <- ifelse(y == 0, -1, 1) # response label vector of {-1, 1}
  }

  if (family == "cox") {
    if (!is.matrix(y)) {
      stop("y must be a matrix or able to be coerced to a matrix")
    }
    if (ncol(y) != 2) {
      stop("y must have two columns for survival data: time-on-study and a censoring indicator")
    }
    if (!all(y[, 2] %in% c(0, 1))) {
      stop("Second column of y must be a binary censoring indicator")
    }
    if (!any(y[, 2] > 0)) {
      stop("Require at least one failure")
    }
    tOrder <- order(y[, 1])
    d <- as.numeric(table(y[y[, 2] == 1, 1]))
    dtime <- sort(unique(y[y[, 2] == 1, 1]))
    row.idx.cox <- which(y[tOrder, 1] >= min(dtime))
    d_idx <- integer(length(row.idx.cox))
    for (i in 1:length(row.idx.cox)) {
      d_idx[i] <- max(which(dtime <= y[tOrder[row.idx.cox[i]], 1]))
    }
  }

  if (family == "gaussian") {
    yy <- y - mean(y)
  } else if (family == "binomial") {
    yy <- y
  } else if (family == "cox") {
    yy <- y[tOrder[row.idx.cox], 2]
  } else if (family == "mgaussian") {
    yy <- t(scale(y, scale = F))
  }

  p <- ncol(X)
  if (length(penalty.factor) != p) {
    stop("penalty.factor does not match up with X")
  }
  ## for now penalty.factor is only applicable for "SSR"
  if (any(penalty.factor != 1) & screen != "SSR") {
    warning(
      "For now penalty.factor is only applicable for \"SSR\". Automatically switching to \"SSR\"."
    )
    screen <- "SSR"
  }
  storage.mode(penalty.factor) <- "double"

  n <- length(row.idx) ## subset of X. idx: indices of rows.
  if (missing(lambda)) {
    user.lambda <- FALSE
    lambda <- rep(0.0, nlambda)
  } else {
    nlambda <- length(lambda)
    user.lambda <- TRUE
  }

  ## fit model ----------------------------------------------------------
  if (output.time) {
    cat("\nStart biglasso: ", format(Sys.time()), "\n")
  }

  # alg.logistic == "MM" dispatches to a routine of its own regardless of
  # `screen`; `screen` itself is still coerced to "SSR" here purely so the
  # value reported back in the returned object is accurate (MM only ever
  # runs the SSR-equivalent routine).
  if (family == "binomial" && alg.logistic == "MM") {
    if (screen != "SSR") {
      warning(
        "For now MM algorithm only supports \"SSR\" screen. Automatically switching to \"SSR\"."
      )
      screen <- "SSR"
    }
  }
  screen_key <- if (family == "binomial" && alg.logistic == "MM") "MM" else screen
  spec <- biglasso_dispatch_lookup(family, screen_key)

  # Every token any routine's arg list might reference (see
  # biglasso-dispatch.R); only the ones named in `spec$args` are ever
  # actually used; the rest evaluate harmlessly (see the placeholder
  # comment above) for families/screens that don't need them.
  dispatch_values <- list(
    X = X@address,
    yy = yy,
    n_pos = as.integer(n.pos),
    ylab = as.integer(ylab),
    d = d,
    d_idx = as.integer(d_idx - 1),
    row_idx = as.integer(row.idx - 1),
    row_idx_cox = as.integer(row.idx[tOrder[row.idx.cox]] - 1),
    lambda = lambda,
    nlambda = as.integer(nlambda),
    lambda_log_scale = as.integer(lambda.log.scale),
    lambda_min = lambda.min,
    alpha = alpha,
    user_lambda = as.integer(user.lambda | any(penalty.factor == 0)),
    eps = eps,
    max_iter = as.integer(max.iter),
    penalty_factor = penalty.factor,
    dfmax = as.integer(dfmax),
    ncores = as.integer(ncores),
    warn = as.integer(warn),
    safe_thresh = safe.thresh,
    update_thresh = update.thresh,
    verbose = as.integer(verbose)
  )

  time <- system.time({
    res <- do.call(
      ".Call",
      c(list(spec$routine), unname(dispatch_values[spec$args]), list(PACKAGE = "biglasso"))
    )
  })

  names(res) <- spec$out
  # 'a' and 'safe_rejections' are only actually present in res for some
  # (family, screen) combinations (see the 'out' entries in
  # biglasso-dispatch.R); res$<name> is simply NULL for the rest, which
  # every downstream use of these two already accounts for.
  a <- res$a
  b <- res$b
  center <- res$center
  scale <- res$scale
  lambda <- res$lambda
  loss <- res$loss
  iter <- res$iter
  rejections <- res$rejections
  safe_rejections <- res$safe_rejections
  col.idx <- res$col.idx
  has_safe_rejections <- !is.null(safe_rejections)

  if (family == "gaussian") {
    a <- rep(mean(y), nlambda)
  }
  if (family != "mgaussian") {
    b <- Matrix::Matrix(b, sparse = TRUE)
  }
  if (output.time) {
    cat("\nEnd biglasso: ", format(Sys.time()), "\n")
  }
  # p.keep <- length(col.idx)
  col.idx <- col.idx + 1 # indices (in R) for which variables have scale > 1e-6

  ## Eliminate saturated lambda values, if any -------------------------------
  ind <- !is.na(iter)
  if (family %in% c("gaussian", "binomial")) {
    a <- a[ind]
  }
  if (!is.list(b)) {
    b <- b[, ind, drop = FALSE]
  }
  iter <- iter[ind]
  lambda <- lambda[ind]
  loss <- loss[ind]

  if (warn & any(iter == max.iter)) {
    warning("Algorithm failed to converge for some values of lambda")
  }

  ## Unstandardize coefficients --------------------------------------------
  if (family == "cox") {
    beta <- Matrix::Matrix(0, nrow = p, ncol = length(lambda), sparse = T)
    bb <- b / scale[col.idx]
    beta[col.idx, ] <- bb
  } else if (family == "mgaussian") {
    varnames <- if (is.null(colnames(X))) paste("V", 1:p, sep = "") else colnames(X)
    varnames <- c("(Intercept)", varnames)
    a <- colMeans(y)
    nclass <- ncol(y)
    beta <- list()
    lam.idx <- which(ind)
    for (class in 1:nclass) {
      beta_class <- Matrix::Matrix(0, nrow = p + 1, ncol = length(lambda), sparse = T)
      beta_class[1, ] <- a[class] - crossprod(center[col.idx], (b[[class]])[, ind] / scale[col.idx])
      beta_class[col.idx + 1, ] <- (b[[class]])[, ind] / scale[col.idx]
      dimnames(beta_class) <- list(varnames, round(lambda, digits = 4))
      beta <- append(beta, beta_class)
    }
    yy <- t(yy)
  } else {
    beta <- Matrix::Matrix(0, nrow = (p + 1), ncol = length(lambda), sparse = T)
    bb <- b / scale[col.idx]
    beta[col.idx + 1, ] <- bb
    beta[1, ] <- a - crossprod(center[col.idx], bb)
  }

  ## Names -----------------------------------------------------------
  varnames <- if (is.null(colnames(X))) paste("V", 1:p, sep = "") else colnames(X)
  if (family != "cox") {
    varnames <- c("(Intercept)", varnames)
  }
  if (family == "mgaussian") {
    nclass <- ncol(y)
    classnames <- if (is.null(colnames(y))) paste("class", 1:nclass, sep = "") else colnames(y)
    names(beta) <- classnames
  } else {
    dimnames(beta) <- list(varnames, round(lambda, digits = 4))
  }

  ## Output ---------------------------------------------------------------
  return.val <- list(
    beta = beta,
    iter = iter,
    lambda = lambda,
    penalty = penalty,
    family = family,
    alpha = alpha,
    loss = loss,
    penalty.factor = penalty.factor,
    n = n,
    center = center,
    scale = scale,
    y = yy,
    screen = screen,
    col.idx = col.idx,
    rejections = rejections
  )

  if (has_safe_rejections) {
    return.val$safe_rejections <- safe_rejections
  }
  if (return.time) {
    return.val$time <- as.numeric(time["elapsed"])
  }
  if (family == "mgaussian") {
    val <- structure(return.val, class = c("mbiglasso"))
  } else {
    val <- structure(return.val, class = c("biglasso", "ncvreg"))
  }
  val
}

Try the biglasso package in your browser

Any scripts or data that you put into this service are public.

biglasso documentation built on Aug. 25, 2026, 5:08 p.m.