% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/Tuning_CKNNRLD.R
\name{CKNNRLD.tune}
\alias{CKNNRLD.tune}
\title{Tune CKNNRLD Model with Automatic Cluster Selection}
\usage{
CKNNRLD.tune(
  y,
  x,
  nfolds = 10,
  folds = NULL,
  seed = NULL,
  A = 10,
  C_range = 2:6,
  cluster_method = "kml"
)
}
\arguments{
\item{y}{Matrix of longitudinal outcomes (subjects x timepoints).}

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

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

\item{folds}{Optional list of pre-specified fold indices.}

\item{seed}{Random seed for reproducibility.}

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

\item{C_range}{Range of cluster numbers to evaluate (default = 2:6).}

\item{cluster_method}{Clustering method to use. Currently supports "kml" (default).}
}
\value{
A list containing:
\item{best_c}{Optimal number of clusters}
\item{cluster_results}{Tuning results for each cluster (from KNNRLD.tune)}
\item{cluster_sizes}{Size of each cluster}
\item{cluster_assignments}{Cluster labels for each subject}
\item{criteria}{Data frame of quality criteria used for selecting best C}
}
\description{
Automatically selects the best number of clusters (C) using internal validation
criteria, then tunes the CKNNRLD model within each cluster via cross-validation.
}
\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 <- CKNNRLD.tune(
  y = y,
  x = x,
  nfolds = 3,
  A = 4,
  C_range = 2:3
)
print(tune_result$best_c)
}

}
