Title: | Tools for Data Splitting |
---|---|
Description: | Fast, lightweight toolkit for data splitting. Data sets can be partitioned into disjoint groups (e.g. into training, validation, and test) or into (repeated) k-folds for subsequent cross-validation. Besides basic splits, the package supports stratified, grouped as well as blocked splitting. Furthermore, cross-validation folds for time series data can be created. See e.g. Hastie et al. (2001) <doi:10.1007/978-0-387-84858-7> for the basic background on data partitioning and cross-validation. |
Authors: | Michael Mayer [aut, cre] |
Maintainer: | Michael Mayer <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0.1 |
Built: | 2024-11-07 05:12:50 UTC |
Source: | https://github.com/mayer79/splittools |
This function provides a list of row indices used for k-fold cross-validation (basic, stratified, grouped, or blocked). Repeated fold creation is supported as well. By default, in-sample indices are returned.
create_folds( y, k = 5L, type = c("stratified", "basic", "grouped", "blocked"), n_bins = 10L, m_rep = 1L, use_names = TRUE, invert = FALSE, shuffle = FALSE, seed = NULL )
create_folds( y, k = 5L, type = c("stratified", "basic", "grouped", "blocked"), n_bins = 10L, m_rep = 1L, use_names = TRUE, invert = FALSE, shuffle = FALSE, seed = NULL )
y |
Either the variable used for "stratification" or "grouped" splits. For other types of splits, any vector of the same length as the data intended to split. |
k |
Number of folds. |
type |
Split type. One of "stratified" (default), "basic", "grouped", "blocked". |
n_bins |
Approximate numbers of bins for numeric |
m_rep |
How many times should the data be split into k folds? Default is 1, i.e., no repetitions. |
use_names |
Should folds be named? Default is |
invert |
Set to |
shuffle |
Should row indices be randomly shuffled within folds?
Default is |
seed |
Integer random seed. |
By default, the function uses stratified splitting. This will balance the folds
regarding the distribution of the input vector y
.
(Numeric input is first binned into n_bins
quantile groups.)
If type = "grouped"
, groups specified by y
are kept together
when splitting. This is relevant for clustered or panel data.
In contrast to basic splitting, type = "blocked"
does not sample
indices at random, but rather keeps them in sequential groups.
If invert = FALSE
(the default), a list with in-sample row indices.
If invert = TRUE
, a list with out-of-sample indices.
partition()
, create_timefolds()
y <- rep(c(letters[1:4]), each = 5) create_folds(y) create_folds(y, k = 2) create_folds(y, k = 2, m_rep = 2) create_folds(y, k = 3, type = "blocked")
y <- rep(c(letters[1:4]), each = 5) create_folds(y) create_folds(y, k = 2) create_folds(y, k = 2, m_rep = 2) create_folds(y, k = 3, type = "blocked")
This function provides a list with in- and out-of-sample indices per fold used for time series k-fold cross-validation, see Details.
create_timefolds(y, k = 5L, use_names = TRUE, type = c("extending", "moving"))
create_timefolds(y, k = 5L, use_names = TRUE, type = c("extending", "moving"))
y |
Any vector of the same length as the data intended to split. |
k |
Number of folds. |
use_names |
Should folds be named? Default is |
type |
Should in-sample data be "extending" over the folds (default) or consist of one single fold ("moving")? |
The data is first partitioned into sequential blocks
to
. Each fold consists of two index vectors: one with in-sample row numbers,
the other with out-of-sample row numbers. The first fold uses
as in-sample
and
as out-of-sample data. The second one uses either
(if
type = "moving"
) or (if
type = "extending"
)
as in-sample, and as out-of-sample data etc. Finally, the kth fold uses
("extending") or
("moving") as in-sample data,
and
as out-of-sample data. This makes sure that out-of-sample data
always follows in-sample data.
A nested list with in-sample and out-of-sample indices per fold.
y <- runif(100) create_timefolds(y) create_timefolds(y, use_names = FALSE) create_timefolds(y, use_names = FALSE, type = "moving")
y <- runif(100) create_timefolds(y) create_timefolds(y, use_names = FALSE) create_timefolds(y, use_names = FALSE, type = "moving")
Creates a stratification vector based on multiple columns of
a data.frame
that can then be passed to the splitting functions.
Currently, the function offers two strategies to create the strata:
"kmeans": k-means cluster analysis on scaled input. (Ordered factors are integer encoded first, unordered factors and character columns are one-hot-encoded.)
"interaction": All combinations (after binning numeric columns into
approximately k
bins).
multi_strata(df, strategy = c("kmeans", "interaction"), k = 3L)
multi_strata(df, strategy = c("kmeans", "interaction"), k = 3L)
df |
A |
strategy |
A string (either "kmeans" or "interaction") to compute the strata, see description. |
k |
An integer. For |
Factor with strata as levels.
y_multi <- data.frame( A = rep(c(letters[1:4]), each = 20), B = factor(sample(c(0, 1), 80, replace = TRUE)), c = rnorm(80) ) y <- multi_strata(y_multi, k = 3) folds <- create_folds(y, k = 5)
y_multi <- data.frame( A = rep(c(letters[1:4]), each = 20), B = factor(sample(c(0, 1), 80, replace = TRUE)), c = rnorm(80) ) y <- multi_strata(y_multi, k = 3) folds <- create_folds(y, k = 5)
This function provides row indices for data splitting, e.g., to split data into training, validation, and test. Different types of split strategies are supported, see Details. The partition indices are either returned as list with one element per partition (the default) or as vector of partition IDs.
partition( y, p, type = c("stratified", "basic", "grouped", "blocked"), n_bins = 10L, split_into_list = TRUE, use_names = TRUE, shuffle = FALSE, seed = NULL )
partition( y, p, type = c("stratified", "basic", "grouped", "blocked"), n_bins = 10L, split_into_list = TRUE, use_names = TRUE, shuffle = FALSE, seed = NULL )
y |
Either the variable used for "stratification" or "grouped" splits. For other types of splits, any vector of the same length as the data intended to split. |
p |
A vector with split probabilities per partition, e.g.,
|
type |
Split type. One of "stratified" (default), "basic", "grouped", "blocked". |
n_bins |
Approximate numbers of bins for numeric |
split_into_list |
Should the resulting partition vector be split into a list?
Default is |
use_names |
Should names of |
shuffle |
Should row indices be randomly shuffled within partition?
Default is |
seed |
Integer random seed. |
By default, the function uses stratified splitting. This will balance the partitions
as good as possible regarding the distribution of the input vector y
.
(Numeric input is first binned into n_bins
quantile groups.)
If type = "grouped"
, groups specified by y
are kept together when
splitting. This is relevant for clustered or panel data.
In contrast to basic splitting, type = "blocked"
does not sample indices
at random, but rather keeps them in groups: e.g., the first 80% of observations form
a training set and the remaining 20% are used for testing.
A list with row indices per partition (if split_into_list = TRUE
)
or a vector of partition IDs.
y <- rep(c(letters[1:4]), each = 5) partition(y, p = c(0.7, 0.3), seed = 1) partition(y, p = c(0.7, 0.3), split_into_list = FALSE, seed = 1) p <- c(train = 0.8, valid = 0.1, test = 0.1) partition(y, p, seed = 1) partition(y, p, split_into_list = FALSE, seed = 1) partition(y, p, split_into_list = FALSE, use_names = FALSE, seed = 1) partition(y, p = c(0.7, 0.3), type = "grouped") partition(y, p = c(0.7, 0.3), type = "blocked")
y <- rep(c(letters[1:4]), each = 5) partition(y, p = c(0.7, 0.3), seed = 1) partition(y, p = c(0.7, 0.3), split_into_list = FALSE, seed = 1) p <- c(train = 0.8, valid = 0.1, test = 0.1) partition(y, p, seed = 1) partition(y, p, split_into_list = FALSE, seed = 1) partition(y, p, split_into_list = FALSE, use_names = FALSE, seed = 1) partition(y, p = c(0.7, 0.3), type = "grouped") partition(y, p = c(0.7, 0.3), type = "blocked")