% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/topic_modeling.R
\name{find_optimal_k}
\alias{find_optimal_k}
\title{Find Optimal Number of Topics}
\usage{
find_optimal_k(
  dfm_object,
  topic_range,
  max.em.its = 75,
  emtol = 1e-04,
  cores = 1,
  categorical_var = NULL,
  continuous_var = NULL,
  height = 600,
  width = 800,
  verbose = TRUE,
  ...
)
}
\arguments{
\item{dfm_object}{A quanteda dfm object to be used for topic modeling.}

\item{topic_range}{A vector of K values to test (e.g., 2:10).}

\item{max.em.its}{Maximum number of EM iterations (default: 75).}

\item{emtol}{Convergence tolerance for EM algorithm (default: 1e-04).
Higher values (e.g., 1e-03) speed up fitting but may reduce precision.}

\item{cores}{Number of CPU cores to use for parallel processing (default: 1).
Set to higher values for faster searchK on multi-core systems.}

\item{categorical_var}{Optional categorical variable(s) for prevalence.}

\item{continuous_var}{Optional continuous variable(s) for prevalence.}

\item{height}{Plot height in pixels (default: 600).}

\item{width}{Plot width in pixels (default: 800).}

\item{verbose}{Logical indicating whether to print progress (default: TRUE).}

\item{...}{Additional arguments passed to stm::searchK.}
}
\value{
A list containing search results and diagnostic plots.
}
\description{
Searches for the optimal number of topics (K) using stm::searchK.
Produces diagnostic plots to help select the best K value. The default
spectral initialization is deterministic; with LDA or random
initialization, fit several seeds per K before selecting a model.
}
\seealso{
\code{\link[=plot_quality_metrics]{plot_quality_metrics()}} to visualize topic-count diagnostics; \code{stm::stm()} to fit the chosen model; \code{\link[=fit_embedding_model]{fit_embedding_model()}} for an embedding-based alternative to STM
}
\concept{topic-modeling}
