% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xreg2_fit.R
\name{xreg2_optim}
\alias{xreg2_optim}
\title{Optimise an xreg2 Model}
\usage{
xreg2_optim(
  xreg2obj,
  method = NULL,
  hessian = TRUE,
  fixed_values = NULL,
  optim_control = NULL,
  ...
)
}
\arguments{
\item{xreg2obj}{An \code{xreg2_obj} list produced by
\code{\link{xreg2_fit}} with \code{return_type = "control"}.}

\item{method}{Character; optimisation method. \code{NULL} defaults to
\code{"ucminf"}.}

\item{hessian}{Logical; compute Hessian. Default \code{TRUE}.}

\item{fixed_values}{Optional named numeric vector of additional fixed values.}

\item{optim_control}{Optional named list of control parameters that override
the method-specific defaults set by \code{\link{xreg2_fit}}.  For example,
\code{optim_control = list(maxit = 500)} tightens the iteration budget.
Passed directly to \code{\link[ucminf]{ucminf}}, \code{\link[stats]{optim}},
or \code{optimx::optimr} depending on \code{method}.}

\item{...}{Additional arguments passed to the optimiser objective function.}
}
\value{
An object of class \code{"xreg2"}.
}
\description{
Low-level optimisation routine called by \code{\link{xreg2_fit}}
  and \code{\link{xreg2_fit_s}}. Can also be called directly on an
  \code{xreg2_obj} to re-optimise with different settings.

  Optimizer routing:
  \itemize{
    \item \code{"ucminf"} (default) — uses \pkg{ucminf} with central-difference
      gradients and high-precision tolerances.
    \item \code{"L-BFGS-B"}, \code{"BFGS"}, \code{"CG"}, \code{"Nelder-Mead"},
      \code{"SANN"}, \code{"Brent"} — uses \code{stats::optim()}.
      Box constraints (\code{lower}/\code{upper} set in
      \code{\link{xreg2_control}}) are enforced for \code{"L-BFGS-B"}.
    \item Any other method string — requires the \pkg{optimx} package (listed
      in \code{Suggests}) and dispatches via \code{optimx::optimr()}.
  }

  Method-specific control defaults (high-precision) are set automatically and
  can be overridden by passing \code{optim_control = list(...)} to
  \code{\link{xreg2_fit}}.
}
\details{
Optimise an xreg2 model object
}
\examples{
\donttest{
set.seed(1)
df   <- data.frame(y = rnorm(200, 2, 0.5), x = rnorm(200))
ctrl <- xreg2_control(
  formulas     = list(y ~ INTERCEPT + x * SLOPE),
  start_values = c(INTERCEPT = 0, SLOPE = 0),
  name         = "main"
)
obj          <- xreg2_fit(controlList = c(ctrl), dataList = list(main = df),
                          return_type = "control")
fit          <- xreg2_optim(obj, method = "ucminf")
fit_lbfgsb   <- xreg2_optim(obj, method = "L-BFGS-B")
}

}
\seealso{
\code{\link{xreg2_fit}}
}
