% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/mi_network.R
\name{find_optimal_k}
\alias{find_optimal_k}
\title{Find optimal k for the kNN MI estimator}
\usage{
find_optimal_k(
  data,
  k_values = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 15L, 18L, 20L),
  cpus = 4L,
  auto_select = TRUE
)
}
\arguments{
\item{data}{A numeric matrix or data.frame with observations (samples)
as rows and variables (MAGs) as columns.}

\item{k_values}{Integer vector of k values to evaluate.}

\item{cpus}{Integer; number of parallel workers.}

\item{auto_select}{Logical; if \code{TRUE} (default), returns the elbow
point as \verb{$best_k}.}
}
\value{
A list with:
\describe{
\item{\code{results}}{A \code{data.frame(k, avg_mi)} for plotting.}
\item{\code{best_k}}{(when \code{auto_select = TRUE}) the k at the elbow
point, determined by the most negative second derivative.}
}
}
\description{
Tests multiple k values by computing the full MI matrix for each k and
recording the average MI across all variable pairs. The resulting
elbow curve helps choose k: the point where MI stops improving steeply
(largest negative second derivative) is selected automatically when
\code{auto_select = TRUE}.
}
\details{
Computation is parallelised across k values using \pkg{snowfall}.
}
\examples{
small_mat <- matrix(rnorm(50), nrow = 10, ncol = 5)
res <- find_optimal_k(small_mat, k_values = c(2L, 3L), cpus = 1)
res$best_k

}
