R/5_dblasso.R

Defines functions dblasso

Documented in dblasso

#' Fit Debiased LASSO with Transfer Learning
#'
#' Fits a debiased LASSO regression model under transfer learning framework, supporting feature
#' selection and coefficient estimation by combining target and source data.
#'
#' @details
#' The variance estimator follows Algorithm 3 of Tian and Feng (2023).
#' For Gaussian linear models, it assumes unit error variance.
#' Under the required source-target heterogeneity conditions, pooled
#' source and target design information is used to estimate the
#' correlation structure, while the debiasing correction is based
#' on target-domain data. This assumption is not automatically
#' verified by the function.
#'
#' @param target A list containing two elements:
#'   \itemize{
#'     \item{x: Feature matrix of target data (numeric matrix)}
#'     \item{y: Response vector of target data (numeric vector)}
#'   } Required.
#' @param source A list (optional, default: NULL) containing two elements:
#'   \itemize{
#'     \item{x: Feature matrix of source data (numeric matrix)}
#'     \item{y: Response vector of source data (numeric vector)}
#'   } Used when transfer = TRUE.
#' @param transfer A logical value (default: FALSE) indicating whether to enable transfer learning
#'   (combining source data with target data for estimation).
#' @param level A numeric value (default: 0.95) specifying confidence level for confidence intervals.
#' @param lambda A string specifying criterion for selecting regularization parameter:
#'   \itemize{
#'     \item{'lambda.min': Lambda value giving minimum cross-validation error}
#'     \item{'lambda.1se': Largest lambda within 1 standard error of minimum error}
#'   }
#'
#' @return A list containing:
#' \itemize{
#'   \item{dbcoef.hat: Debiased LASSO coefficient vector (including intercept)}
#'   \item{coef.hat: Original LASSO coefficient vector}
#'   \item{CI: Data frame with confidence intervals (lb = lower bound, ub = upper bound)}
#'   \item{var.est: Variance estimates for debiased coefficients}
#'   \item{se.est: Standard errors for debiased coefficients}
#'   \item{P.value: Vector of p-values for coefficients}
#' }
#'
#' @import glmnet
#' @import foreach
#' @keywords internal
dblasso <- function(
    target,
    source = NULL,
    transfer = FALSE,
    level = 0.95,
    lambda = "lambda.1se"
) {

  ## ---------- internal: nodewise regression ----------
  gamma_est <- function(target, source = NULL, transfer = FALSE, lambda) {

    p_x <- ncol(target$x)

    if (!transfer) {

      target_data <- data.frame(Y = target$y, target$x)
      colnames(target_data) <- c("Y", paste0("X", seq_len(p_x)))

      cv_glm <- glmnet::cv.glmnet(
        x = as.matrix(target_data[, -1, drop = FALSE]),
        y = target_data$Y,
        alpha = 1,
        intercept = FALSE,
        family = "gaussian"
      )

      beta_TL <- as.vector(
        cv_glm$glmnet.fit$beta[, which(cv_glm$lambda == cv_glm[[lambda]])]
      )

    } else {

      target_data <- data.frame(Y = target$y, target$x)
      source_data <- data.frame(Y = source$y, source$x)

      colnames(target_data) <- colnames(source_data) <-
        c("Y", paste0("X", seq_len(p_x)))

      data_all <- rbind(target_data, source_data)

      cv_glm <- glmnet::cv.glmnet(
        x = as.matrix(data_all[, -1, drop = FALSE]),
        y = data_all$Y,
        alpha = 1,
        intercept = FALSE,
        family = "gaussian"
      )

      w <- as.vector(
        cv_glm$glmnet.fit$beta[, which(cv_glm$lambda == cv_glm[[lambda]])]
      )

      offset <- as.numeric(as.matrix(target_data[, -1, drop = FALSE]) %*% w)

      cv_glm <- glmnet::cv.glmnet(
        x = as.matrix(target_data[, -1, drop = FALSE]),
        y = target_data$Y,
        alpha = 1,
        intercept = FALSE,
        offset = offset,
        family = "gaussian"
      )

      delta <- as.vector(
        cv_glm$glmnet.fit$beta[, which(cv_glm$lambda == cv_glm[[lambda]])]
      )

      beta_TL <- w + delta
    }

    beta_TL
  }

  ## ---------- CI level ----------
  r.level <- level + (1 - level) / 2
  j<-NULL

  ## ---------- initial lasso ----------
  coef.hat <- as.vector(
    lasso(
      target = target,
      source = source,
      transfer = transfer,
      lambda = lambda
    )
  )

  ## ---------- build design matrix with FIXED names ----------
  orig_xnames <- colnames(target$x)
  if (is.null(orig_xnames)) {
    orig_xnames <- paste0("X", seq_len(ncol(target$x)))
  }

  D <- list(target = target, source = source)

  D$target$x <- cbind("(Intercept)" = 1, as.matrix(target$x))
  colnames(D$target$x) <- c("(Intercept)", orig_xnames)

  if (transfer) {
    D$source$x <- cbind("(Intercept)" = 1, as.matrix(source$x))
    colnames(D$source$x) <- colnames(D$target$x)
  }

  coef.name <- colnames(D$target$x)

  ## ---------- dimensions ----------
  p <- ncol(D$target$x)

  ## ---------- combined design ----------
  X.comb <- if (transfer) {
    rbind(D$source$x, D$target$x)
  } else {
    D$target$x
  }

  ## ---------- Sigma ----------
  Sigma.hat <- crossprod(X.comb) / nrow(X.comb)

  ## ---------- Theta ----------
  L <- foreach::foreach(j = seq_len(p), .combine = "rbind") %do% {

    D1 <- D
    D1$target$y <- D1$target$x[, j]
    D1$target$x <- D1$target$x[, -j, drop = FALSE]

    if (transfer) {
      D1$source$y <- D1$source$x[, j]
      D1$source$x <- D1$source$x[, -j, drop = FALSE]
      gamma <- gamma_est(D1$target, D1$source, TRUE, lambda = lambda)
    } else {
      gamma <- gamma_est(D1$target, lambda = lambda)
    }

    tau2 <- Sigma.hat[j, j] -
      Sigma.hat[j, -j, drop = FALSE] %*% gamma

    theta <- numeric(p)
    theta[j] <- 1
    theta[-j] <- -gamma

    c(theta, tau2)
  }

  # Theta.hat <- solve(diag(L[, p + 1])) %*% L[, 1:p]
  Theta.hat <- L[, 1:p] / L[, p + 1]

  ## ---------- debiased estimator ----------
  residuals <- D$target$y - D$target$x %*% coef.hat

  dbcoef.hat <- as.vector(
    coef.hat +
      Theta.hat %*% crossprod(D$target$x, residuals) / length(residuals)
  )

  ## ---------- inference ----------
  var.est <- diag(Theta.hat %*% Sigma.hat %*% t(Theta.hat))
  se.est  <- sqrt(var.est / length(residuals))

  CI <- data.frame(
    lb = dbcoef.hat - qnorm(r.level) * se.est,
    ub = dbcoef.hat + qnorm(r.level) * se.est
  )

  Z <- dbcoef.hat / se.est
  P.value <- 2 * pnorm(abs(Z), lower.tail = FALSE)

  ## ---------- assign names ----------
  names(coef.hat)   <- coef.name
  names(dbcoef.hat) <- coef.name
  rownames(CI)      <- coef.name
  names(var.est)    <- coef.name
  names(se.est)     <- coef.name
  names(P.value)    <- coef.name

  res <- list(
    dbcoef.hat = dbcoef.hat,
    coef.hat   = coef.hat,
    CI         = CI,
    var.est    = var.est,
    se.est     = se.est,
    P.value    = P.value
  )

  class(res) <- "dblasso"
  res
}

Try the TransHDM package in your browser

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

TransHDM documentation built on Nov. 24, 2026, 9:06 a.m.