glmtrans R quickstart: when does borrowing data help?
Compare target-only fitting, all-source transfer, and automatic source selection on the same target test set with the glmtrans R package.
Transfer learning overview · Original paper · CRAN package and manual
Use glmtrans when you have a small target dataset and one or more source datasets that could help predict outcomes in the target population. The key question is not how much data you can borrow, but whether borrowing improves target predictions. Start with a target-only baseline.
1. Install glmtrans
install.packages("glmtrans")
2. Compare three fits on the same test set
This Gaussian regression example simulates 80 predictors, 60 labeled target training observations, and two source datasets of 180 observations each. Source 1 has coefficients closer to the target than source 2 by construction. All three fits use the same target training rows. The 200 target test rows are held out of model fitting, tuning, and source selection.
set.seed(2026, kind = "L'Ecuyer-CMRG")
sim <- glmtrans::models(
family = "gaussian", type = "all", cov.type = 2,
n.target = 260, K = 2, Ka = 1, n.source = c(180, 180),
p = 80, s = 5, h = 1
)
target_train <- list(
x = sim$target$x[1:60, , drop = FALSE],
y = sim$target$y[1:60]
)
target_test_x <- sim$target$x[61:260, , drop = FALSE]
target_test_y <- sim$target$y[61:260]
fits <- list(
target_only = glmtrans::glmtrans(target_train, nfolds = 5),
all_sources = glmtrans::glmtrans(
target_train, sim$source, transfer.source.id = "all", nfolds = 5
),
auto_select = glmtrans::glmtrans(
target_train, sim$source, transfer.source.id = "auto",
nfolds = 5, detection.info = FALSE
)
)
rmse <- vapply(fits, function(fit) {
pred <- predict(fit, newx = target_test_x, type = "response")
sqrt(mean((pred - target_test_y)^2))
}, numeric(1))
print(round(rmse, 3))
print(fits$auto_select$transfer.source.id)
3. Read the results
With R 4.5.0 and glmtrans 2.1.0, the results are below. Lower RMSE is better:
| Fit | Target test RMSE |
|---|---|
| Target only | 1.126 |
| Transfer from both sources | 1.312 |
| Automatically select sources | 1.112 |
The automatic fit selects source 1. In this run, borrowing from both sources hurts relative to the target-only baseline. Selecting the more similar source improves RMSE slightly. This is one simulation, not evidence that automatic selection always helps.
For your own data, use the same predictor columns, in the same order and units, across target and sources. Compare against a target-only fit using independent target data. If you use the comparison to choose a workflow, do that on validation data and keep a separate test set for the final estimate.
Cite the method
Tian, Y., and Feng, Y. (2023). Transfer Learning under High-Dimensional Generalized Linear Models. Journal of the American Statistical Association, 118(544), 2684–2697. DOI: 10.1080/01621459.2022.2071278. The paper page has a copyable BibTeX citation.