Provides a deterministic method for splitting data into training and testing sets that maximizes the distance between training and testing points while preserving their distributions. See Joseph and Cho (in press) for details. This work is supported by U.S. National Science Foundation grant DMS-2310637.
Robust deterministic data splitting for model validation.
maxtwin splits data into training and testing sets by balancing two objectives:
The method is robust to paired and clustered data, where methods like random splitting or DUPLEX can fail.
# After CRAN release
install.packages("maxtwin")
library(maxtwin)
set.seed(42)
X <- matrix(runif(500), ncol = 5)
Y <- sin(X[,1]) + cos(X[,2]) + X[,3]^2
# Split with 20% test set
test_idx <- maxtwin(X, Y, splitratio = 0.2)
X_train <- X[-test_idx, ]
X_test <- X[test_idx, ]
Y_train <- Y[-test_idx]
Y_test <- Y[test_idx]
Factor responses and factor predictor columns are encoded using Helmert contrasts. Character columns must first be converted to factors.
# Classification with a factor response
test_idx <- maxtwin(
X = iris[, 1:4],
Y = iris$Species,
splitratio = 0.2
)
When there is no separate response, splitting can be based on all columns in a data frame:
test_idx <- maxtwin(iris, splitratio = 0.2)
test_idx <- initialmaxtwin(X, Y, splitratio = 0.2)
| Parameter | Default | Description |
|---|---|---|
splitratio |
-- | Fraction of data for test set. Must be 1/r for integer r >= 2 (0.5, 0.25, 0.2, 0.1, ...) |
initialization |
TRUE |
Start twinning from the point closest to the centroid |
standardization |
TRUE |
Standardize the augmented data before twinning |
MaxTwin (Algorithm 1 in the paper) proceeds in two steps:
Step 1: Apply twinning to select candidate test points, then within each Voronoi cell of the twinning set, pick the point with the largest nearest-neighbor distance.
Step 2: If the average separation distance falls below the threshold from Proposition 1, iteratively replace test points to improve separation while staying within a ball constraint that preserves distribution quality.
devtools::test()
Tests cover:
Joseph, V.R. and Cho, Y. (in press). A Robust Deterministic Data Splitting Method for Model Validation. Journal of Computational and Graphical Statistics.
GPL (>= 3)