source_detection: Detect Transferable Source Data via Cross-Validation

View source: R/7_source_detect.R

source_detectionR Documentation

Detect Transferable Source Data via Cross-Validation

Description

Determines whether external source datasets can be effectively transferred to the target data by comparing predictive performance using LASSO regression under a transfer learning framework.

Usage

source_detection(
  target_data,
  source_data = NULL,
  Y,
  D,
  M,
  X,
  kfold = 5,
  t = 0.02,
  verbose = TRUE
)

Arguments

target_data

A data frame containing the target dataset. All variables must be numeric.

source_data

A list of data frames containing source datasets (optional, default: NULL). All variables must be numeric and have the same column names as target_data.

Y

Character string specifying the outcome variable name.

D

Character string specifying the exposure (treatment) variable name.

M

Character vector specifying mediator variable names.

X

Character vector specifying covariate variable names.

kfold

Integer (default: 5). Number of folds for cross-validation.

t

Numeric (default: 0.02). Relative tolerance for the validation mean squared error. A source is transferable when the profile negative-log-likelihood gap satisfies T \le (n/2)\log(1+t), where n is the average validation-fold size. This is the same rule as \mathrm{MSE}_{source} \le (1+t)\,\mathrm{MSE}_{target}. Larger values make the criterion more lenient.

verbose

Logical (default: TRUE). Whether to print progress messages.

Value

A list containing:

  • transfer.source.id: Indices of source datasets deemed transferable

  • source.loss: Mean validation loss for each source dataset

  • target.valid.loss: Mean validation loss using target-only model

  • T_index: Difference between source loss and target-only loss for each source

  • threshold: Transferability threshold (n/2)\log(1+t)

  • loss.cv: Full k-fold cross-validation loss matrix

Examples

## Reproducible example
set.seed(123)

# Generate synthetic target data
target_data <- data.frame(
  Y  = rnorm(200),
  D  = rnorm(200),
  M1 = rnorm(200),
  M2 = rnorm(200),
  X1 = rnorm(200)
)

# Generate synthetic source data
source1 <- data.frame(
  Y  = rnorm(300),
  D  = rnorm(300),
  M1 = rnorm(300),
  M2 = rnorm(300),
  X1 = rnorm(300)
)

source2 <- data.frame(
  Y  = rnorm(250),
  D  = rnorm(250),
  M1 = rnorm(250),
  M2 = rnorm(250),
  X1 = rnorm(250)
)

# Run source detection
result <- source_detection(
  target_data = target_data,
  source_data = list(source1, source2),
  Y = "Y",
  D = "D",
  M = c("M1", "M2"),
  X = "X1",
  kfold = 5,
  t = 0.05,
  verbose = FALSE
)

# Get Summary
summary(result)

# Transferable source indices
result$transfer.source.id

# Compare validation losses
data.frame(
  Source = c(paste0("Source", seq_along(result$source.loss)), "Target"),
  Loss   = c(result$source.loss, result$target.valid.loss)
)


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