% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/Tuning_KNNRLD.R
\name{KNNRLD.tune}
\alias{KNNRLD.tune}
\title{Tune k in KNNRLD using Cross-Validation}
\usage{
KNNRLD.tune(
  y,
  x,
  nfolds = 10,
  folds = NULL,
  seed = NULL,
  A = 10,
  graph = FALSE
)
}
\arguments{
\item{y}{Matrix of longitudinal outcomes (subjects x timepoints).}

\item{x}{Matrix of predictor variables (subjects x features).}

\item{nfolds}{Number of cross-validation folds (default = 10).}

\item{folds}{Optional list of pre-specified fold indices. If provided, nfolds is ignored.}

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

\item{A}{Maximum number of neighbors to evaluate (searches from 2 to A, default = 10).}

\item{graph}{Logical; if TRUE, plots MSPE vs. k.}
}
\value{
A list containing:
\item{crit}{Mean squared prediction error (MSPE) for each k value}
\item{best_k}{Optimal number of neighbors (minimizes MSPE)}
\item{performance}{Minimum MSPE value}
\item{runtime}{Elapsed computation time}
}
\description{
Finds the optimal number of neighbors for KNN regression in longitudinal data
using k-fold cross-validation. Evaluates k values from 2 to A.
}
\examples{
\donttest{
set.seed(123)
n <- 30
T <- 3
d <- 2
x <- matrix(runif(n * d), nrow = n)
y <- matrix(rnorm(n * T), nrow = n)
tune_result <- KNNRLD.tune(
  y = y,
  x = x,
  nfolds = 3,
  A = 4
)
str(tune_result)
}

}
