Package {trackopt}


Type: Package
Title: Track Numerical Optimization
Version: 0.1.1
Description: Tracks parameter values, gradients, and Hessians at each iteration of numerical optimizers. Useful for analyzing optimization progress, diagnosing issues, and studying convergence behavior.
License: GPL (≥ 3)
Encoding: UTF-8
RoxygenNote: 7.3.3
Imports: checkmate, cli, ggplot2, oeli (≥ 0.7.2), optimizeR (≥ 1.2.0), stats, tibble, utils
Suggests: testthat (≥ 3.0.0)
Depends: R (≥ 4.1.0)
Config/testthat/edition: 3
URL: https://github.com/loelschlaeger/trackopt
BugReports: https://github.com/loelschlaeger/trackopt/issues
NeedsCompilation: no
Packaged: 2026-05-12 15:52:33 UTC; Lennart Oelschläger
Author: Lennart Oelschläger [aut, cre]
Maintainer: Lennart Oelschläger <oelschlaeger.lennart@gmail.com>
Repository: CRAN
Date/Publication: 2026-05-12 18:40:09 UTC

trackopt: Track Numerical Optimization

Description

logo

Tracks parameter values, gradients, and Hessians at each iteration of numerical optimizers. Useful for analyzing optimization progress, diagnosing issues, and studying convergence behavior.

Author(s)

Maintainer: Lennart Oelschläger oelschlaeger.lennart@gmail.com

See Also

Useful links:

Examples

himmelblau <- function(x) {
  (x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2
}
track <- nlm_track(f = himmelblau, p = c(0, 0))
summary(track)
ggplot2::autoplot(track)

Track numerical optimization

Description

Usage

nlm_track(
  f,
  p,
  target = NULL,
  npar = NULL,
  gradient = NULL,
  hessian = NULL,
  ...,
  iterations_max = 100,
  tolerance = 1e-06,
  typsize = rep(1, length(p)),
  fscale = 1,
  ndigit = 12,
  stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000),
  steptol = 1e-06,
  minimize = TRUE,
  verbose = FALSE
)

optim_track(
  f,
  p,
  target = NULL,
  npar = NULL,
  gradient = NULL,
  ...,
  iterations_max = 100,
  tolerance = 1e-06,
  lower = NULL,
  upper = NULL,
  method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"),
  control = list(),
  minimize = TRUE,
  verbose = FALSE
)

## S3 method for class 'trackopt'
summary(object, ...)

## S3 method for class 'trackopt'
autoplot(object, iteration = NULL, xlim = NULL, xlim2 = NULL, ...)

Arguments

f

[function]
A function to optimize. It must return a single numeric value.

By default, the first argument of f is optimized. That argument should be a numeric vector of the same length as p. Additional arguments can be supplied through ....

Use target when f should be optimized over another argument or over multiple arguments.

p

[numeric()]
The starting parameter values for the target argument(s).

target

[character() | NULL]
The names of the numeric argument(s) to optimize.

If NULL (default), the first argument of f is used.

npar

[integer()]
The length of each target argument.

Specify npar when optimizing over multiple target arguments so p can be split correctly.

If target contains a single argument and npar is NULL, length(p) is used.

gradient

[function | NULL]
Optionally a function that returns the gradient of f.

Its arguments must match the arguments of f.

hessian

[function | NULL]
Optionally a function that returns the Hessian of f.

Its arguments must match the arguments of f.

...

Additional arguments passed to f, and to gradient and hessian when they are specified.

iterations_max

[integer(1)]
The maximum number of tracked optimization steps before termination.

tolerance

[numeric(1)]
Tracking stops when the absolute change in function value between two consecutive iterations is less than this value.

typsize, fscale, ndigit, stepmax, steptol

Arguments passed on to nlm.

minimize

[logical(1)]
If TRUE, minimize f; otherwise maximize it.

verbose

[logical(1)]
If TRUE, print progress after each iteration.

lower, upper

[numeric() | NULL]
Optional lower and upper parameter bounds. Scalars are recycled to the number of parameters.

method, control

Arguments passed on to optim.

Elements trace and maxit are ignored in control because optim_track() controls tracing and iteration limits directly.

object

[trackopt]
A trackopt object.

iteration

[integer(1)]
The iteration to plot.

If NULL, the last iteration is plotted.

This option is useful for creating animations with the R Markdown animation chunk option.

xlim, xlim2

[numeric(2)]
Ranges for the first and second parameter to plot.

If NULL, they are derived from the parameter ranges in object.

Value

A tibble with one row per stored iteration, including the starting point.

Examples

himmelblau <- function(x) {
  (x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2
}
track <- nlm_track(f = himmelblau, p = c(0, 0))
summary(track)
ggplot2::autoplot(track)