% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/krnn.R
\name{krnn_cv}
\alias{krnn_cv}
\alias{print.krnn_cv}
\title{Cross-Validation for k-RNN Neighbourhood Size}
\usage{
krnn_cv(
  X,
  y,
  k_max = NULL,
  R = 10L,
  rule = c("min", "1se"),
  grid = NULL,
  type = c("tukey", "simplicial"),
  seed = NULL
)

\method{print}{krnn_cv}(x, ...)
}
\arguments{
\item{X}{Numeric matrix (\eqn{N \times M}).}

\item{y}{Integer vector of class labels (0/1).}

\item{k_max}{Maximum value of k to evaluate. Default
\code{floor(nrow(X) / 4)}.}

\item{R}{Number of CV folds. Default \code{10}.}

\item{rule}{Character: \code{"min"} (default) or \code{"1se"}.}

\item{grid}{Numeric vector of length \eqn{M}.}

\item{type}{Depth variant.}

\item{seed}{Optional integer seed for reproducibility.}

\item{x}{A \code{"krnn_cv"} object.}

\item{...}{Ignored.}
}
\value{
An object of class \code{"krnn_cv"} with components:
\describe{
\item{k_opt}{Selected optimal k.}
\item{cv_error}{Mean CV misclassification error for each k.}
\item{cv_se}{Standard error of CV error per k.}
\item{k_max}{Maximum k evaluated.}
\item{R}{Number of folds used.}
\item{rule}{The selection rule used.}
}

Invisibly returns \code{x}.
}
\description{
Selects the optimal half-neighbourhood size \eqn{k} by \eqn{R}-fold
cross-validation, using either the minimum-error rule or the
one-standard-error (1-SE) rule (Section 3.3.1 of Perez Ruiz, 2020).
}
\examples{
\donttest{
set.seed(3)
M <- 80
t  <- seq(0, 1, length.out = M)
X0 <- t(replicate(50, sin(2 * pi * t) + rnorm(M, sd = 0.4)))
X1 <- t(replicate(50, cos(2 * pi * t) + rnorm(M, sd = 0.4)))
X  <- rbind(X0, X1)
y  <- rep(0:1, each = 50)
cv <- krnn_cv(X, y, k_max = 20, R = 5, seed = 1)
print(cv)
plot_krnn_cv(cv)
}

}
