% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stats_cv_sets_methods.R
\name{stratified_kfold}
\alias{stratified_kfold}
\title{Y-stratified k-fold cross-validation strategy}
\usage{
stratified_kfold(k, type = c("DA", "R"), probs = NULL)
}
\arguments{
\item{k}{Integer. Number of folds.}

\item{type}{Character. Either \code{"DA"} (classification)
or \code{"R"} (regression).}

\item{probs}{Numeric vector of quantile probabilities used
to stratify continuous \code{Y} when \code{type = "R"}.}
}
\value{
A named \code{list} with elements:
\describe{
\item{train}{List of integer vectors containing training set indices
for each resampling iteration.}
\item{strategy}{Character string indicating the resampling strategy.}
\item{n}{Integer. Number of samples in the dataset.}
\item{seed}{Integer. Random seed used to generate the resampling splits,
ensuring reproducibility.}
}
}
\description{
Y-stratified k-fold cross-validation strategy
}
\details{
For classification (\code{type = "DA"}), folds are generated such that
class proportions are approximately preserved in each fold.

For regression (\code{type = "R"}), \code{Y} is discretised into bins
defined by \code{probs}, and folds are generated to approximately
preserve the bin distribution.
}
\examples{
set.seed(1)
n <- 100
thr <- 1.5
Y <- c(rnorm(80, thr - 3, 0.3), rnorm(20, thr + 3, 0.3))  # unbalanced outcome
mean(Y > thr)

q80 <- quantile(Y, 0.8)  # defines the rare "high" stratum (top 20\%)

cv_k  <- kfold(k = 10)
cv_sk  <- stratified_kfold(k = 10, type = "R", probs = c(0, 0.8, 1))

k_inst <-  metabom8:::.arg_check_cv(cv_pars=cv_k, model_type='R', n=n, Y_prepped=cbind(Y))
sk_inst <-  metabom8:::.arg_check_cv(cv_pars=cv_sk, model_type='R', n=n, Y_prepped=cbind(Y))

round(sapply(k_inst$train,  function(i) mean(Y[i] > q80)), 2)  # reflects imbalance
round(sapply(sk_inst$train, function(i) mean(Y[i] > q80)), 2)  # balanced across strata
}
\seealso{
Other resampling strategies: 
\code{\link{balanced_boot}()},
\code{\link{balanced_mc}()},
\code{\link{kfold}()},
\code{\link{mc}()}
}
\concept{resampling strategies}
