\name{Cross-validation for the Jensen-Shannon based k--NN algorithm}
\alias{cv.knnimp}
\title{
Cross-validation for the Jensen-Shannon based k--NN algorithm
}

\description{
Cross-validation for the Jensen-Shannon based k--NN algorithm.
}

\usage{
cv.knnimp(x, k = 2:10, type = "Ait", R = 200)
}

\arguments{
\item{x}{
A matrix with the compositional data, zeros are allowed.
}
\item{k}{
A vector with the number of nearest neighbours to explore.
}
\item{type}{
The type of distance to use in order to see the difference between the true and the imputed data.
If there are no zeros, Aitchison's distance ("Ait", default value) may be used, otherwise the (squared) Jensen-Shannon
divergence ("js") may be used.
}
\item{R}{
The number of repetitions to perform.
}
}

\details{
The algorithm performs a repeated cross-validation for the Jensen-Shanon divergence based k--NN algorithm.
It selects the optimal value of k, the number of nearest neighbours.
}

\value{
A list including:
\item{perf}{
A matrix with the performance (Aitchison or Jensen-Shannon divergence) for each value of k, at each repetition.
}
\item{performance}{
The averagge performance (Aitchison or Jensen-Shannon divergence) for each value of k.
}
\item{best_k}{
The optimal value of k.
}
\item{runtime}{
The runtime of the cross-validation procedure.
}
}

\references{
Tsagris M., Alenazi A. and Stewart C. (2026). A Jensen--Shannon divergence based k--NN algorithm
for missing value imputation in compositional data.
Journal of Applied Statistics (Accepted for publication).
https://arxiv.org/pdf/2605.29702
}

\author{
Michail Tsagris.

R implementation and documentation: Michail Tsagris \email{mtsagris@uoc.gr}.
}

\seealso{
\code{\link{knnimp}
}
}

\examples{
x <- matrix( rgamma(100 * 7, runif(7, 4,6), 1), ncol = 7, byrow = TRUE )
x <- w <- x / Rfast::rowsums(x)  ## Dirichlet simulated values
xam <- matrix(nrow = 10, ncol =  3)
nu <- sample(100, 10)
for ( vim in 1:10)  xam[vim, ] <- sample(1:7, 3)
for ( vim in 1:10)  x[ nu[vim], xam[vim, ] ] <- NA
mod <- cv.knnimp(x, k = 2:5, R = 30)
}

