```{r echo=FALSE, eval=FALSE} ############################################################## # == BEGIN -- NOT SHOWN IN SUPPLEMENT, FOR PRINTING ONLY ===== # First line renders an appropriate HTML file for the webpage # Second line makes the script file # RUN BOTH MANUALLY (following using Knit HTML button) setwd("C:/aaaWork/Web/GitHub/IFAR/supplements/growth") source("../../rhelpers/IFARhelpers.R") modHTML("OtherNLAlgorithms",need2render=FALSE) if (require(FSA)) purl2("OtherNLAlgorithms.Rmd",topnotes="User must set working directory appropriately.") # == END -- NOT SHOWN IN SUPPLEMENT, FOR PRINTING ONLY ===== ############################################################## ``` The von Bertalanffy Growth Function (VBGF) was introduced and methods for fitting the function were illustrated in Chapter 12 of the [*Introductory Fisheries Analyses with R*](http://derekogle.com/IFAR/) (IFAR) book. The `nls()` function demonstrated in the **IFAR** book 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. Other supplements demonstrate how to [fit other parameterizations of the VBGF](OtherVBGFParams.html) and [other growth functions](OtherGrowthFuns.html). \ ### Required Packages for this Supplement Functions used in this supplement require the packages shown below. ```{r echo=FALSE, results='hide', message=FALSE, warnings=FALSE} ############################################################## # == BEGIN -- NOT SHOWN IN SUPPLEMENT, FOR PRINTING ONLY ===== # # Setup of knitr source("../../rhelpers/knitr_setup.R") # declare packages used rqrd <- c("FSA","dplyr","nlstools","minpack.lm","captioner","knitr") # setup figure, table, and equation captioning library(captioner) figcaps <- captioner(prefix="Figure") figcaps("","") # == END -- NOT SHOWN IN SUPPLEMENT, FOR PRINTING ONLY ===== ############################################################## ``` ```{r cache=FALSE, results='hide', message=FALSE} library(FSA) library(dplyr) library(nlstools) library(minpack.lm) ``` ### Data Used in this Supplement The male Black Drum data ([view](https://github.com/droglenc/FSAdata/blob/master/data-raw/BlackDrum2001.csv), [download](https://raw.githubusercontent.com/droglenc/FSAdata/master/data-raw/BlackDrum2001.csv), [meta-data](http://derekogle.com/fishR/data/data-html/BlackDrum2001.html)) used in the **IFAR** book will also be used in this supplement. ```{r} bdmf <- read.csv("BlackDrum2001.csv") %>% select(-c(spname,day,weight)) %>% filterD(sex %in% c("male","female"),otoage<50) bdm <- filterD(bdmf,sex=="male") headtail(bdm) ``` \ ## 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 [@MotulskyRansnas1987]. 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` [@Elzhovetalminpack.lm] and uses the same main arguments as `nls()`. For example, the "Typical" VBGF is fit (and parameter estimates, likelihood profile confidence intervals, and predictions are extracted) to the male Black Drum data below using the L-M algorithm. ```{r warning=FALSE} vbTyp <- vbFuns() svTyp <- list(Linf=1193,K=0.13,t0=-2.0) fitLM <- nlsLM(tl~vbTyp(otoage,Linf,K,t0),data=bdm,start=svTyp) bootLM <- nlsBoot(fitLM) cbind(Ests=coef(fitLM),confint(bootLM)) predict(bootLM,vbTyp,t=3) ``` \ ## Algorithms that Allow 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 may substantively effect the results of the model fitting. Thus, they should be used rarely and, when used, set liberally. 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. ```{r} clwr <- c(Linf=1, K=0.0001,t0=-Inf) cupr <- c(Linf=Inf,K=Inf, t0=Inf) ``` 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. ```{r warning=FALSE} fitLM1 <- nlsLM(tl~vbTyp(otoage,Linf,K,t0),data=bdm,start=svTyp,lower=clwr,upper=cupr) bootLM1 <- nlsBoot(fitLM1) cbind(Ests=coef(fitLM1),confint(bootLM1)) ``` Parameter constraints may be used similarly with `nls()`, but the optimization algorithm must be changed to "Port" with `algorithm="port"`. ```{r warning=FALSE} fitP <- nls(tl~vbTyp(otoage,Linf,K,t0),data=bdm,start=svTyp, algorithm="port",lower=clwr,upper=cupr) bootP <- nlsBoot(fitP) cbind(Ests=coef(fitP),confint(bootP)) ``` \ ## Other Algorithms Still other algorithms are found in `nlxb()` from `nlmrt` [@Nashnlmrt] and `nls2()` from `nls2` [@Grothendiecknls2]. \ -------------------------------------------------------------- ```{r echo=FALSE, results='asis'} reproInfo(rqrdPkgs=rqrd,out="markdown",links=c(Script="OtherNLAlgorithms.R",RMarkdown="OtherNLAlgorithms.Rmd")) ``` -------------------------------------------------------------- \ ## References