```
library(FSA) # for headtail(), vbFuns(), vbStarts()
library(dplyr) # for select(), filter()
library(nlstools) # for nlsBoot()
library(minpack.lm) # for nlsLM()
```

# Other Nonlinear Model Fitting Algorithms

The von Bertalanffy Growth Function (VBGF) was introduced and methods for fitting the function were illustrated in Chapter 12 of Ogle (2016). The `nls()`

function demonstrated in Ogle (2016) uses the Gauss-Newton algorithm by default. However, there are many other algorithms for fitting nonlinear functions. Two of the several algorithms coded in R are demonstrated in this supplement.

# Setup

## Packages

Functions used in this supplement require the following packages.

## Data

The male Black Drum data.^{1} used in Ogle (2016) are also used in this supplement. As in Ogle (2016) a few unneeded variables are removed for simplicity, only males are examined, and a single old fish is removed by retaining only fish with an otolith age less than 50.

^{1} Download data with CSV link on the linked page.

```
<- read.csv("https://raw.githubusercontent.com/fishR-Core-Team/FSAdata/main/data-raw/BlackDrum2001.csv") |>
bdm select(-c(spname,day,weight)) |>
filter(sex=="male",otoage<50)
headtail(bdm)
```

```
#R| year agid month tl sex otoage
#R| 1 2001 1 4 787.5 male 6
#R| 2 2001 2 5 700.0 male 5
#R| 3 2001 8 5 1140.0 male 23
#R| 72 2001 122 5 1175.0 male 39
#R| 73 2001 125 6 590.0 male 4
#R| 74 2001 127 6 530.0 male 3
```

# Levenberg-Marquardt Algorithm

The Levenberg-Marquardt (L-M) algorithm is a powerful and common method that switches between two other algorithms depending on when those algorithms perform most optimally (Motulsky and Ransnas 1987). Most practically, the L-M algorithm appears to be quite robust to “poor” starting values.

The L-M algorithm is implemented in `nlsLM()`

from `minpack.lm`

(Elzhov et al. 2013) and uses the same main arguments as `nls()`

. For example, the “Typical” VBGF is fit (and parameter estimates, bootstrapped confidence intervals, and predictions are extracted) to the male Black Drum data below using the L-M algorithm.^{2}

^{2} This code is the same as used in Ogle (2016), except that `nlsLM()`

replaced `nls()`

.

```
<- vbFuns()
vbTyp <- list(Linf=1193,K=0.13,t0=-2.0)
svTyp <- nlsLM(tl~vbTyp(otoage,Linf,K,t0),data=bdm,start=svTyp)
fitLM <- nlsBoot(fitLM)
bootLM cbind(Ests=coef(fitLM),confint(bootLM))
```

```
#R| Ests 95% LCI 95% UCI
#R| Linf 1196.7193453 1180.7583403 1215.3277659
#R| K 0.1418266 0.1245746 0.1614129
#R| t0 -1.5943403 -2.4861941 -0.8246865
```

`predict(bootLM,vbTyp,t=3)`

```
#R| t Median 95% LCI 95% UCI
#R| [1,] 3 573.6826 534.7676 608.3075
```

# Using Parameter Constraints

In some instances, the user may want to constrain the model fitting algorithms to only consider parameter values within a certain range. For example, the user may want to constrain the \(L_{\infty}\) and \(K\) parameters of the “Typical” VBGF to be positive.

Parameter constraints can be used with at least two algorithms in R. In either case, the lower and upper bounds for each parameter are entered into separate named **vectors** in the same order as the list used for starting values. Infinite bounds are the default ,but may be specifically defined for some parameters with `Inf`

and `-Inf`

(where `Inf`

represents infinity). For example, vectors that define constraints for the parameters in the “Typical” VBGF are defined below, with \(L_\infty\) and \(K\) constrained to be positive values and \(t_{0}\) left unconstrained.

```
<- c(Linf=1, K=0.0001,t0=-Inf)
clwr <- c(Linf=Inf,K=Inf, t0=Inf) cupr
```

These constraints may be provided to `lower=`

and `upper=`

, respectively, of `nlsLM()`

. In this instance, these bounds had no noticeable effect until bootstapping, where there were fewer instances of lack of convergence.

```
<- nlsLM(tl~vbTyp(otoage,Linf,K,t0),data=bdm,start=svTyp,
fitLM1 lower=clwr,upper=cupr)
<- nlsBoot(fitLM1)
bootLM1 cbind(Ests=coef(fitLM1),confint(bootLM1))
```

```
#R| Ests 95% LCI 95% UCI
#R| Linf 1196.7193453 1180.4433085 1214.9203900
#R| K 0.1418266 0.1228016 0.1619365
#R| t0 -1.5943403 -2.5765079 -0.8162231
```

Parameter constraints may be used similarly with `nls()`

, but the “Port” optimization algorithm must be used with `algorithm="port"`

.

```
<- nls(tl~vbTyp(otoage,Linf,K,t0),data=bdm,start=svTyp,
fitP algorithm="port",lower=clwr,upper=cupr)
<- nlsBoot(fitP)
bootP cbind(Ests=coef(fitP),confint(bootP))
```

```
#R| Ests 95% LCI 95% UCI
#R| Linf 1196.7188171 1178.3021069 1216.2244935
#R| K 0.1418273 0.1230324 0.1629955
#R| t0 -1.5943092 -2.5654540 -0.7327735
```

# Other Algorithms

Still other algorithms are found in `nlxb()`

from `nlmrt`

(Nash 2014) and `nls2()`

from `nls2`

(Grothendieck 2013).