# calcMin: Calculate the Minimum of a User-Defined Function In PBSmodelling: GUI Tools Made Easy: Interact with Models and Explore Data

## Description

Minimization based on the R-stat functions `nlm`, `nlminb`, and `optim`. Model parameters are scaled and can be active or not in the minimization.

## Usage

 ```1 2``` ```calcMin(pvec, func, method="nlm", trace=0, maxit=1000, reltol=1e-8, steptol=1e-6, temp=10, repN=0, ...) ```

## Arguments

 `pvec` Initial values of the model parameters to be optimized. `pvec` is a data frame comprising four columns ( `"val","min","max","active"`) and as many rows as there are model parameters. The `"active"` field (logical) determines whether the parameters are estimated (`T`) or remain fixed (`F`). `func` The user-defined function to be minimized (or maximized). The function should return a scalar result. `method` The minimization method to use: one of `nlm`, `nlminb`, `Nelder-Mead`, `BFGS`, `CG`, `L-BFGS-B`, or `SANN`. Default is `nlm`. `trace` Non-negative integer. If positive, tracing information on the progress of the minimization is produced. Higher values may produce more tracing information: for method `"L-BFGS-B"` there are six levels of tracing. Default is `0`. `maxit` The maximum number of iterations. Default is `1000`. `reltol` Relative convergence tolerance. The algorithm stops if it is unable to reduce the value by a factor of `reltol*(abs(val)+reltol)` at a step. Default is `1e-8`. `steptol` A positive scalar providing the minimum allowable relative step length. Default is `1e-6`. `temp` Temperature controlling the `"SANN"` method. It is the starting temperature for the cooling schedule. Default is `10`. `repN` Reports the parameter and objective function values on the R-console every `repN` evaluations. Default is `0` for no reporting. `...` Further arguments to be passed to the optimizing function chosen: `nlm`, `nlminb`, or `optim`. Beware of partial matching to earlier arguments.

## Details

See `optim` for details on the following methods: `Nelder-Mead`, `BFGS`, `CG`, `L-BFGS-B`, and `SANN`.

## Value

A list with components:

 `Fout` The output list from the optimizer function chosen through `method`. `iters` Number of iterations. `evals` Number of evaluations. `cpuTime` The user CPU time to execute the minimization. `elapTime` The total elapsed time to execute the minimization. `fminS` The objective function value calculated at the start of the minimization. `fminE` The objective function value calculated at the end of the minimization. `Pstart` Starting values for the model parameters. `Pend` Final values estimated for the model parameters from the minimization. `AIC` Akaike's Information Criterion `message` Convergence message from the minimization routine.

## Note

Some arguments to `calcMin` have no effect depending on the `method` chosen.

## Author(s)

Jon T. Schnute, Pacific Biological Station, Fisheries and Oceans Canada, Nanaimo BC

`scalePar`, `restorePar`, `calcMin`, `GT0`
In the `stats` package: `nlm`, `nlminb`, and `optim`.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24``` ```local(envir=.PBSmodEnv,expr={ Ufun <- function(P) { Linf <- P[1]; K <- P[2]; t0 <- P[3]; obs <- afile\$len; pred <- Linf * (1 - exp(-K*(afile\$age-t0))); n <- length(obs); ssq <- sum((obs-pred)^2 ); return(n*log(ssq)); }; oldpar = par(no.readonly = TRUE) afile <- data.frame(age=1:16,len=c(7.36,14.3,21.8,27.6,31.5,35.3,39, 41.1,43.8,45.1,47.4,48.9,50.1,51.7,51.7,54.1)); pvec <- data.frame(val=c(70,0.5,0),min=c(40,0.01,-2),max=c(100,2,2), active=c(TRUE,TRUE,TRUE),row.names=c("Linf","K","t0"), stringsAsFactors=FALSE); alist <- calcMin(pvec=pvec,func=Ufun,method="nlm",steptol=1e-4,repN=10); print(alist[-1]); P <- alist\$Pend; #resetGraph(); expandGraph(); xnew <- seq(afile\$age[1],afile\$age[nrow(afile)],len=100); ynew <- P[1] * (1 - exp(-P[2]*(xnew-P[3])) ); plot(afile); lines(xnew,ynew,col="red",lwd=2); addLabel(.05,.88,paste(paste(c("Linf","K","t0"),round(P,c(2,4,4)), sep=" = "),collapse="\n"),adj=0,cex=0.9); par(oldpar) }) ```