% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/runSA.R
\name{runSA}
\alias{runSA}
\title{Stochastic approximation for parameter fitting}
\usage{
runSA(
  ocmat,
  enmat = NULL,
  rep = 128,
  threads = 1,
  we = 0.001,
  totalit = 1000,
  lambda = 0.01,
  intv = 100,
  runadamW = TRUE,
  maxlr = 0.005,
  sparse = TRUE,
  getall = FALSE,
  reporting = TRUE
)
}
\arguments{
\item{ocmat}{Numeric matrix (0/1). Rows are samples and columns are species.}

\item{enmat}{Optional numeric matrix. Environmental variables for each sample.
Should be normalized/standardized beforehand.}

\item{rep}{Integer. Number of parallel runs (replicates) for optimization.}

\item{threads}{Integer. Number of cores used when running in parallel.}

\item{we}{Numeric. Weight decay parameter for AdamW.}

\item{totalit}{Integer. Total number of optimization iterations.}

\item{lambda}{Numeric. Sparsity penalty parameter (used when \code{sparse = TRUE}).}

\item{intv}{Integer. Interval of iterations at which intermediate results are recorded.}

\item{runadamW}{Logical. If \code{TRUE}, use AdamW-style optimization.}

\item{maxlr}{Numeric. Maximum learning rate.}

\item{sparse}{Logical. If \code{TRUE}, apply sparsity constraint.}

\item{getall}{Logical. If \code{TRUE}, return all replicate trajectories.}

\item{reporting}{Logical. If \code{TRUE}, print progress messages.}
}
\value{
If \code{getall = TRUE}, a list of SA trajectories (length \code{rep}).
Otherwise, a list with two elements, \code{c(params, enmat)} is returned.
\code{params} is the concatenated matrix of the estimated parameter set:
\describe{
  \item{h}{Implicit environmental effects on species occurrence}
  \item{J}{Species-species interaction matrix}
  \item{g}{(If \code{enmat} is not \code{NULL}) Explicit effects of the environmental factors.}
}
}
\description{
Estimate species interaction parameters and (optionally) environmental
preference parameters by stochastic approximation (SA). The function runs
multiple SA optimizations in parallel (when \code{threads > 1}) using
\pkg{foreach}.
}
