---
title: "Choosing Lag Fit Algorithms in Growth Modeling"
author: "Maks Necki"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Choosing lags}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

This vignette introduces two functions, *choose_lag_fit_algorithm_baranyi* and *choose_lag_fit_algorithm_logistic*, that help you select the best fitting algorithm for estimating lag parameters in growth modeling using Baranyi and Logistic models. We will explore the usage and examples of these functions.

## The choose_lag_fit_algorithm_baranyi Function
### Introduction
The choose_lag_fit_algorithm_baranyi function is designed to fit the best Baranyi model parameters to a given growth curve. It runs nonlinear least squares (nls) and nonlinear least squares with bounds (nlsLM) algorithms with different parameter setups to choose the best model. The selected model minimizes the residual sum of squares, provided that all coefficients are nonnegative.

### Usage
The function takes the following parameters:

- **gr_curve**: A data frame containing growth curve data with columns "LOG10N" and "t."
- **LOG10N0**: Initial value for the LOG10N0 parameter.
- **init_lag**: Initial value for the lag parameter.
- **init_mumax**: Initial value for the mumax parameter.
- **init_LOG10Nmax**: Initial value for the LOG10Nmax parameter.
- **max_iter**: Maximum number of iterations.
- **lower_bound**: Lower bound for the bounded nls optimization.
The function returns the best nls fitting object with parameters fitted to the Baranyi model.

### Examples
``` {r eval = FALSE}
# Load required libraries
library(dplyr)

# Generate example growth curve data
set.seed(123)
time <- 1:10
LOG10N <- c(2.0, 2.8, 3.6, 5.0, 7.0, 9.0, 12.0, 15.8, 20.0, 25.5)
gr_curve <- data.frame(t = time, LOG10N = LOG10N)

# Fit the Baranyi model using the best algorithm
best_fit <- choose_lag_fit_algorithm_baranyi(gr_curve, LOG10N0 = 2.0, init_lag = 0.5, init_mumax = 0.3, init_LOG10Nmax = 30, max_iter = 100, lower_bound = 0)

# Print the results
best_fit
```

## The choose_lag_fit_algorithm_logistic Function
### Introduction
The choose_lag_fit_algorithm_logistic function is similar to the previous function but tailored for fitting the Logistic model to a growth curve. It selects the best model by comparing nls and nlsLM algorithms with different parameter setups.

### Usage
The function takes the following parameters:

- **gr_curve**: A data frame containing growth curve data with columns "biomass" and "time."
- **n0**: The initial biomass.
- **init_gr_rate**: Initial value for the growth rate.
- **init_K**: Initial value for the saturation parameter K.
- **init_lag**: Initial value for the lag parameter.
- **max_iter**: Maximum number of iterations (default is 100).
- **lower_bound**: Lower bound for the bounded nls optimization (default is 0).
The function returns the best nls fitting object with parameters fitted to the Logistic model.

### Examples
``` {r eval = FALSE}
# Load required libraries
library(dplyr)

# Generate example growth curve data
set.seed(123)
time <- 1:10
biomass <- c(0.1, 0.3, 0.7, 1.5, 3.0, 5.0, 8.0, 12.0, 18.0, 25.0)
gr_curve <- data.frame(time = time, biomass = biomass)

# Fit the Logistic model using the best algorithm
best_fit <- choose_lag_fit_algorithm_logistic(gr_curve, n0 = 0.1, init_gr_rate = 0.5, init_K = 30, init_lag = 0.5, max_iter = 100, lower_bound = c(0, 0, 0))

# Print the results
best_fit
```

## Conclusion
These functions provide a convenient way to select the best fitting algorithm for estimating lag parameters in growth modeling. They help you choose the most suitable model for your data, whether it follows a Baranyi or Logistic growth pattern. Use these functions to enhance your growth curve analysis with the most accurate and reliable parameter estimation.
