---
title: 'gnomonicM: An R Package to Estimate Natural Mortality for different life stages'
author: 'Edgar Josymar Torrejon-Magallanes'
date: "`r Sys.Date()`"
output:
  html_document:
    highlight: default
    toc: yes
    fig_caption: true
vignette: >
  %\VignetteIndexEntry{gnomonicM}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo = FALSE, message = FALSE}
library(kableExtra)
library(gnomonicM)
```

This vignette introduces you to `gnomonicM` package and provides a way to estimate **Natural Mortality (M) throughout the life history of species**, mainly fish and invertebrates.


## Install package
You can install from CRAN:
```{r, eval=FALSE}
install.packages("gnomonicM")
```

Or the development version from github:

```{r, eval=FALSE}
# install.packages("devtools")
devtools::install_github("ejosymart/gnomonicM")
```


After that, call the package:
```{r, eval=FALSE}
library("gnomonicM")
```

## 1. Natural mortality (M) via deterministic method
For estimating *M* we will use the data provided by Caddy (1996) based on two species with: (i) seven gnomonic intervals, (ii) egg stage duration of 2 days, (iii) a longevity of one year (365 days), (iv) a mean lifetime fecundity (*MLF*) of 200000 (high fecundity, hf) and 135 eggs (low fecundity, lf), and (v) initial constant proportionality ($\alpha$) value of 2.

```{r echo=TRUE}
model_hf <- gnomonic(nInterval   = 7, 
                     eggDuration = 2, 
                     longevity   = 365, 
                     fecundity   = 200000, 
                     a_init      = 2)
```


```{r echo=TRUE}
model_lf <- gnomonic(nInterval   = 7, 
                     eggDuration = 2, 
                     longevity   = 365, 
                     fecundity   = 135, 
                     a_init      = 2)
```

If you have additional information related to the duration of the other gnomonic intervals, you could provide them via the argument `addInfo`. For example, if we assume that the duration of the second and fifth gnomonic interval is equal to 4 and 40 days respectively, we will have to include this information as follows:

```{r echo=TRUE, results = 'hide'}
modelAddInfo <- gnomonic(nInterval   = 7, 
                         eggDuration = 2, 
                         addInfo     = c(4, NA, NA, 40, NA, NA),
                         longevity   = 365, 
                         fecundity   = 200000, 
                         a_init      = 2)
```

The `NA` values in the argument `addInfo` must be written to complete the length of the vector. In this case the length of the `addInfo` vector is equal to `nInterval` - 1.


### Print and plot for deterministic method
Print the results of the `model_hf` or `model_lf`. The output is an object of class `gnomos`. This object contains a list with the constant proportionality value ($\alpha$), the constant proportion of the overall natural death rate (*G*), a dataframe with the duration (*"interval_duration_day"*), and the natural mortality (*"M_day"* and *"M_year"*) for each gnomonic interval.

For plotting, an object of class `gnomos` is required. The function `plot` generates a scatter plot with the values of *M* for each gnomonic interval. You can modify the labels via `xlab`, `ylab`, the color via `bg` and the sizes of points in the scatter plot, also you can pass arguments to the plot method.


```{r echo = TRUE}
#Species with high fecundity.
print(model_hf)

plot(model_hf)

#Species with low fecundity.
print(model_lf)

plot(model_lf, xlab = "My X label", ylab = "My Y label", cex = 3, bg = "blue")
```

**For comparative purposes, we present the results provided by Caddy (1996).** *Note: We have multiplied the interval duration by 365 in order to have day units.*

```{r, echo=FALSE, }
data_hf <- data.frame(Gnomonic_interval = 1:7, 
                    interval_duration_day = c(2.00, 2.76, 6.58, 15.67, 37.33, 88.91, 211.75),
                    M_year = c(300.16, 217.25, 91.27, 38.30, 16.08, 6.75, 2.84),
                    No_Surv = c(38614, 7455, 1439, 278, 54, 10, 2))
data_hf %>%
  kbl(caption = "Results provided by Caddy (1996) in the estimation of M based on high fecundity species (MLF = 200,000 eggs, egg stage duration = 2 days, and longevity = 365 days)", booktabs = T) %>% 
  kable_styling(full_width = F, position = "center", latex_options = c("striped", "hold_position"))
```


```{r, echo=FALSE, }
data_lf <- data.frame(Gnomonic_interval = 1:7, 
                    interval_duration_day = c(2.00, 2.76, 6.58, 15.67, 37.33, 88.91, 211.75),
                    M_year = c(109.82, 79.48, 33.37, 14.01, 5.88, 2.47, 1.04),
                    No_Surv = c(74, 41, 22, 12, 7, 4, 2))
data_lf %>%
  kbl(caption = "Results provided by Caddy (1996) in the estimation of M based on low fecundity species (MLF = 135, egg stage duration = 2 days, and longevity = 365 days)", booktabs = T) %>% 
  kable_styling(full_width = F, position = "center", latex_options = c("striped", "hold_position"))
```



## 2. Natural mortality (M) via stochastic method: the Pacific chub mackerel (*Scomber japonicus*) case
The function to be used to estimate *M* is `gnomonicStochastic`. The previous estimation of natural mortality, which was based on the `gnomonic` function, did not include any measures of deviation. In this method we calculated these measures assuming that the main source of uncertainty and variability was the mean lifetime fecundity (*MLF*). 

We have included three different distribution function via the argument `distr`. It could be normal: `distr = 'normal'`, uniform: `distr = 'uniform'`, and triangular: `distr = 'triangle'`. Once you have chosen a particular distribution function, you must to include information related with the minimum, maximum, mean and standard deviation (sd) of *MLF*. 

We will use the information reported by the chub mackerel, based on an (i) eight gnomonic intervals, (ii) egg stage duration of 2.33 days (56 hours), (iii) a longevity of eight years (2920 days), (iv) a mean lifetime fecundity (*MLF*) of 78174 [11805 - 144543] and 28978 [7603 - 53921] eggs assuming uniform distribution, and (v) initial constant proportionality ($\alpha$) value of 2. We simulated 1000 estimates of natural mortality for each gnomonic intervals and estimated mean mortality rate ($\bar{M_i}$), the confidence interval, and the standard deviation ($\sigma_{i}$).

```{r echo=TRUE}
model_cm_hf <- gnomonicStochastic(nInterval     = 8, 
                                  eggDuration   = 2.33,
                                  longevity     = 2920,
                                  distr         = "uniform", 
                                  min_fecundity = 11805, 
                                  max_fecundity = 144543, 
                                  niter         = 1000, 
                                  a_init        = 2)


model_cm_lf <- gnomonicStochastic(nInterval     = 8, 
                                  eggDuration   = 2.33,
                                  longevity     = 2920,
                                  distr         = "uniform", 
                                  min_fecundity = 7603, 
                                  max_fecundity = 53921, 
                                  niter         = 1000, 
                                  a_init        = 2)
```


### Print and plot for stochastic method
Printing and plotting the result of the stochastic method follow the same idea that deterministic method. In this case when using print it includes the *niter* values of *G* and *fecundity*, and the M values for each gnomonic intervals for each iteration. The plot is based on a boxplot and the arguments could be modified as in the deterministic method.


```{r echo = TRUE, results = 'hide'}
#The results are not shown here. Please run it in your console.
print(model_cm_hf)

print(model_cm_lf)
```

In the previous plots, by default, the M values have $day^{-1}$ units (`dayUnits = TRUE`). You can modify it via the argument `dayUnits = FALSE` which will show a plot with the M values in $year^{-1}$ units.

```{r echo = TRUE}
par(mar = c(6,6,6,6))
plot(model_cm_hf, main = "M for chub mackerel, MLF = [11 805 - 144 543]", dayUnits = FALSE)

plot(model_cm_lf, main = "M for chub mackerel, MLF = [7 603 - 53 921]", dayUnits = FALSE)
```


## 3. Different distributions in *MLF*
In this section, we test different distribution functions in the *MLF* via the argument `distr`. There are three options: `distr = 'normal'`, `distr = 'uniform'` and `distr = 'triangle'`. You must include particular information related to the minimum, maximum, mean, and standard deviation (sd) of *MLF* based on the particular distribution function.

```{r echo=TRUE}
modelUniformAddInfo <- gnomonicStochastic(nInterval     = 7, 
                                          eggDuration   = 2,
                                          addInfo       = c(4, NA, NA, 40, NA, NA),
                                          longevity     = 365,
                                          distr         = "uniform", 
                                          min_fecundity = 100000, 
                                          max_fecundity = 300000, 
                                          niter         = 1000, 
                                          a_init        = 2)


modelNormal <- gnomonicStochastic(nInterval     = 7, 
                                  eggDuration   = 2,
                                  longevity     = 365,
                                  distr         = "normal", 
                                  fecundity     = 200000, 
                                  sd_fecundity  = 50000, 
                                  niter         = 1000, 
                                  a_init        = 2)


modelTriangle <- gnomonicStochastic(nInterval     = 7, 
                                    eggDuration   = 2,
                                    longevity     = 365,
                                    distr         = "triangle", 
                                    fecundity     = 200000,
                                    min_fecundity = 100000,
                                    max_fecundity = 300000,
                                    niter         = 1000, 
                                    a_init        = 2)
```


```{r echo = TRUE}
plot(modelUniformAddInfo, main = "Uniform distribution in MLF, with additional information")
plot(modelNormal, main = "Normal distribution in MLF")
plot(modelTriangle, main = "Triangular distribution in MLF")
```


## 4. Testing in other species 
This section shows the applications to published data that used the gnomonic model (see, Ramírez-Rodríguez & Arreguín-Sánchez (2003); Martínez-Aguilar et al (2005); Giménez-Hurtado et al. (2009); Martínez-Aguilar et al. (2010); Aranceta-Garza et al. (2016); Romero-Gallardo et al. (2018)). It gave the chance to assess the approach in different taxa (fish, invertebrates) and life-history (demersal, pelagic, benthic, short, and large life span).


```{r echo=TRUE}
Farfantopenaeus <-  gnomonic(nInterval   = 7,
                             eggDuration = 1.5, 
                             longevity   = 480,
                             fecundity   = 500000,
                             a_init      = 1)


Vannamei <- gnomonic(nInterval   = 7,
                     eggDuration = 0.54, 
                     longevity   = 365,
                     fecundity   = 265000,   
                     a_init      = 3)


Sardinops <- gnomonicStochastic(nInterval     = 10,
                                eggDuration   = 2.5, 
                                longevity     = 2555,
                                min_fecundity = 646763,
                                max_fecundity = 1090678,
                                niter         = 1000, 
                                a_init        = 2)


Epinephelus <- gnomonicStochastic(nInterval     = 11,
                                  eggDuration   = 2, 
                                  longevity     = 7300,
                                  min_fecundity = 102000,
                                  max_fecundity = 573500,
                                  niter         = 1000, 
                                  a_init        = 2)


Dosidicus <- gnomonicStochastic(nInterval     = 5,
                                eggDuration   = 6, 
                                longevity     = 438,
                                min_fecundity = 813000,
                                max_fecundity = 25887000,
                                niter         = 1000, 
                                a_init        = 2)



Isostichopus <- gnomonicStochastic(nInterval     = 6,
                                   eggDuration   = 2,
                                   longevity     = 3650,
                                   min_fecundity = 13500,
                                   max_fecundity = 5062490, 
                                   niter         = 1000,
                                   a_init        = 2)

```


```{r echo = TRUE}
par(mar=c(5.1, 4.1, 6, 2.1))
plot(Farfantopenaeus, main = "M for Farfantopenaeus duorarum", dayUnits = FALSE)

plot(Vannamei, main = "M for Penaeus vannamei", col = "darkred", dayUnits = FALSE)

plot(Sardinops, main = "M for Sardinops caeruleus", col = "blue")

plot(Epinephelus, main = "M for Epinephelus morio", col = "darkgreen", dayUnits = FALSE)

plot(Dosidicus, main = "M for Dodisicus gigas", col = "purple", dayUnits = FALSE)

plot(Isostichopus, main = "M for Isostichopus badionotus", col = "skyblue", dayUnits = FALSE)
```


## 5. References
[Aranceta-Garza F, Arreguín-Sánchez F, Ponce-Díaz G, Seijo JC. 2016. Natural mortality of three commercial penaeid shrimps (*Litopenaeus vannamei*, *L. stylirostris* and *Farfantepenaeus californiensis*) of the Gulf of California using gnomonic time divisions. Scientia Marina 80:199–206.](https://doi.org/10.3989/scimar.04326.29A)

[Caddy JF. 1991. Death rates and time intervals: is there an alternative to the constant natural mortality axiom? Reviews in Fish Biology and Fisheries 1:109–138. doi:10.1007/BF00157581.](https://doi.org/10.1007/BF00157581)

[Caddy JF. 1996. Modelling natural mortality with age in short-lived invertebrate populations: definition of a strategy of gnomonic time division. Aquatic Living Resources 9:197–207.](https://doi.org/10.1051/alr:1996023)

[Giménez-Hurtado E, Arreguín-Sánchez F, Lluch-Cota SE. 2009. Natural Mortality Rates during Life History Stages of the Red Grouper on Campeche Bank, Mexico. North American Journal of Fisheries Management 29:216–222.](https://doi.org/10.1577/M06-041.1)

[Martínez-Aguilar S, Arreguín-Sánchez F, Morales-Bojórquez E. 2005. Natural mortality and life history stage duration of Pacific sardine (*Sardinops caeruleus*) based on gnomonic time divisions. Fisheries Research 71:103–114.](https://doi.org/10.1016/j.fishres.2004.04.008)

Martínez-Aguilar S, Díaz Uribe JG, De Anda-Montañez JA, Cisneros-Mata MA. 2010. Natural mortality and life history stage duration for the jumbo squid (*Dosidicus gigas*) in the Gulf of California, Mexico, using the gnomonic time division. Ciencia Pesquera 18:31–42.

[Ramírez-Rodríguez M, Arreguín-Sánchez F. 2003. Life history stage duration and natural mortality for the pink shrimp *Farfantepenaeus duorarum* (Burkenroad, 1939) in the southern Gulf of Mexico, using the gnomonic model for time division. Fisheries Research 60:45–51.](https://doi.org/10.1016/S0165-7836(02)00064-4)

[Romero-Gallardo S, Velazquez-Abunader I, Alberto Lopez-Rocha J, Garza-Gisholt E. 2018. Natural mortality estimates throughout the life history of the sea cucumber *Isostichopus badionotus* (Holothuroidea: Aspidochirotida). PeerJ 6:e5235.](https://peerj.com/articles/5235/)
