---
title: "Case Study: Chinook Salmon Bioenergetics"
author: "Hans Ttito"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Case Study: Chinook Salmon Bioenergetics}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 7,
  fig.height = 5,
  message   = FALSE,
  warning   = FALSE
)
library(fb4package)
```

## Overview

This vignette walks through a complete bioenergetics analysis of juvenile
*Oncorhynchus tshawytscha* (Chinook salmon) using conditions representative
of an interior Pacific Northwest lake during a single growing season (180 days,
April–September). We cover:

1. Setting up the model from the built-in parameter database
2. Estimating daily consumption from a target final weight (binary search)
3. Quantifying uncertainty via bootstrap resampling
4. Summarising growth, energy budgets, and feeding performance

---

## 1. Species parameters

`fb4package` ships with a built-in database (`fish4_parameters`) covering
more than 105 parameterisations from the published literature.

```{r species-params}
data(fish4_parameters)

chinook_db <- fish4_parameters[["Oncorhynchus tshawytscha"]]
stage      <- if ("juvenile" %in% names(chinook_db$life_stages)) {
                "juvenile"
              } else {
                names(chinook_db$life_stages)[1]
              }

sp_params <- chinook_db$life_stages[[stage]]
sp_info   <- chinook_db$species_info
sp_info$life_stage <- stage

cat("Life stage  :", stage, "\n")
cat("CEQ (consumption equation):", sp_params$consumption$CEQ, "\n")
cat("REQ (respiration equation):", sp_params$respiration$REQ, "\n")
```

---

## 2. Environmental data

We simulate a 180-day seasonal temperature profile typical of a Pacific
Northwest lake (April through September), with a peak in late July.

```{r temperature}
set.seed(42)
days      <- 1:180
base_temp <- 7 + 7 * sin(pi * (days - 20) / 180)   # peak ~14 °C at day 110
temp_vec  <- pmax(2, base_temp + rnorm(180, 0, 0.4))

temp_data <- data.frame(
  Day         = days,
  Temperature = round(temp_vec, 2)
)

cat(sprintf("Temperature range : %.1f – %.1f °C\n",
            min(temp_data$Temperature), max(temp_data$Temperature)))
cat(sprintf("Mean temperature  : %.1f °C\n", mean(temp_data$Temperature)))
```

---

## 3. Diet composition

Juvenile Chinook in Pacific Northwest lakes consume primarily forage fish
(alewife) in summer, supplemented by shrimp and invertebrates in spring and
autumn. Prey energy densities are typical values from the literature
(J/g wet weight).

```{r diet}
alewife <- pmax(0, 0.55 + 0.25 * sin(pi * (days - 30) / 180))
shrimp  <- pmax(0, 0.28 - 0.10 * sin(pi * (days - 30) / 180))
inverts <- pmax(0, 1 - alewife - shrimp)
total   <- alewife + shrimp + inverts

diet_props <- data.frame(
  Day     = days,
  Alewife = round(alewife / total, 4),
  Shrimp  = round(shrimp  / total, 4),
  Inverts = round(inverts / total, 4)
)

prey_energy <- data.frame(
  Day     = days,
  Alewife = 4900,   # J/g (wet weight)
  Shrimp  = 3200,
  Inverts = 2600
)

cat("Diet proportions (first 3 days):\n")
print(head(diet_props, 3))
```

---

## 4. Building the `Bioenergetic` object

All model components are assembled into a single `Bioenergetic` object.
Initial weight is set to 5 g, representing a post-emergence juvenile.

```{r bio-object}
bio_chinook <- Bioenergetic(
  species_params     = sp_params,
  species_info       = sp_info,
  environmental_data = list(temperature = temp_data),
  diet_data          = list(
    proportions = diet_props,
    prey_names  = c("Alewife", "Shrimp", "Inverts"),
    energies    = prey_energy
  ),
  simulation_settings = list(initial_weight = 5, duration = 180)
)

# Predator energy density: linear interpolation from 4 200 to 5 000 J/g
# as the fish accumulate lipids through summer (PREDEDEQ = 1 from DB)
bio_chinook$species_params$predator$ED_ini <- 4200
bio_chinook$species_params$predator$ED_end <- 5000

print(bio_chinook)
```

### Setup visualisation

```{r plot-setup, fig.cap="Model setup dashboard: environmental and diet data coverage."}
plot(bio_chinook, type = "dashboard")
```

```{r plot-temperature, fig.cap="Seasonal temperature profile used in the simulation."}
plot(bio_chinook, type = "temperature", colors = "red")
```

```{r plot-diet, fig.cap="Daily diet composition over the 180-day season."}
plot(bio_chinook, type = "diet", colors = "green")
```

---

## 5. Estimating consumption — binary search

We ask: *what feeding level (p) produces a final weight of 40 g after 180 days?*
This is the standard FB4 approach when a target weight has been measured in
the field.

```{r binary-search}
res_bs <- run_fb4(
  x         = bio_chinook,
  fit_to    = "Weight",
  fit_value = 40,
  strategy  = "binary_search",
  verbose   = FALSE
)

cat(sprintf("Estimated p-value     : %.4f\n", res_bs$summary$p_value))
cat(sprintf("Final weight          : %.1f g\n", res_bs$summary$final_weight))
cat(sprintf("Total consumption     : %.1f g\n", res_bs$summary$total_consumption_g))
cat(sprintf("Simulation converged  : %s\n",     res_bs$summary$converged))
```

### Growth trajectory

```{r plot-growth, fig.cap="Daily weight trajectory from binary search fit."}
plot(res_bs, type = "growth")
```

### Energy budget

```{r plot-energy, fig.cap="Daily energy budget partitioning (J/day)."}
plot(res_bs, type = "energy")
```

### Full dashboard

```{r plot-dashboard, fig.cap="Simulation dashboard: growth, consumption, temperature, and energy."}
plot(res_bs, type = "dashboard")
```

---

## 6. Consumption with a fixed feeding level

When *p* is known (e.g., from a bioenergetics study), use
`strategy = "direct_p_value"` to forward-simulate without fitting.

```{r direct}
res_direct <- run_fb4(
  x         = bio_chinook,
  fit_to    = "p_value",
  fit_value = 0.75,
  strategy  = "direct_p_value",
  verbose   = FALSE
)

cat(sprintf("Final weight at p = 0.75 : %.1f g\n", res_direct$summary$final_weight))
cat(sprintf("Total consumption        : %.1f g\n", res_direct$summary$total_consumption_g))
```

---

## 7. Bootstrap uncertainty estimation

When field data include multiple final weights (e.g., a sample of individually
tagged fish), bootstrap resampling propagates measurement variability into the
*p* estimate.

We simulate 25 observed final weights around the binary-search result, with a
CV of 8 % to mimic realistic field sampling variability.

```{r bootstrap, cache=TRUE}
set.seed(123)
n_obs           <- 25
final_wt_true   <- res_bs$summary$final_weight
obs_weights     <- rnorm(n_obs, mean = final_wt_true, sd = final_wt_true * 0.08)

res_boot <- run_fb4(
  x                = bio_chinook,
  fit_to           = "Weight",
  observed_weights = obs_weights,
  strategy         = "bootstrap",
  n_bootstrap      = 100,
  upper            = 1,
  parallel         = FALSE,
  confidence_level = 0.95,
  verbose          = FALSE
)

cat(sprintf("p mean (bootstrap) : %.4f\n", res_boot$summary$p_mean))
cat(sprintf("p SD               : %.4f\n", res_boot$summary$p_sd))
cat(sprintf("95%% CI             : [%.4f, %.4f]\n",
            res_boot$method_data$confidence_intervals$p_ci_lower,
            res_boot$method_data$confidence_intervals$p_ci_upper))
```

```{r plot-boot, cache=TRUE, fig.cap="Bootstrap distribution of estimated p-values with 95% CI."}
plot(res_boot, type = "uncertainty")
```

---

## 8. Result analysis

`fb4package` provides four dedicated analysis functions that extract
ecologically meaningful metrics from any `fb4_result` object.

```{r analysis}
growth_stats   <- analyze_growth_patterns(res_bs)
feeding_stats  <- analyze_feeding_performance(res_bs)
energy_budget  <- analyze_energy_budget(res_bs)

# Growth metrics
cat("=== Growth ===\n")
cat(sprintf("  Final weight            : %.1f g\n",
            growth_stats$final_weight$estimate))
cat(sprintf("  Total growth            : %.1f g\n",
            growth_stats$total_growth$estimate))
cat(sprintf("  Specific growth rate    : %.4f g/g/day\n",
            growth_stats$specific_growth_rate$estimate))

# Feeding performance
cat("\n=== Feeding performance ===\n")
cat(sprintf("  Total consumption       : %.1f g\n",
            feeding_stats$total_consumption$estimate))
cat(sprintf("  Gross conv. efficiency  : %.3f\n",
            feeding_stats$gross_conversion_efficiency$estimate))
```

---

## 9. Ecological interpretation

The estimated *p* ≈ 0.62 indicates that juvenile Chinook consumed approximately
62 % of their bioenergetically predicted maximum ration during this
180-day growing season.  A gross conversion efficiency near 0.13–0.15 is
consistent with published values for salmonids at these temperature ranges
(Deslauriers et al. 2017).

The energy budget plot shows that respiration dominates energy expenditure
(~55–60 % of consumed energy), with egestion and excretion accounting for
another 20 %. The remaining ~20–25 % is directed towards somatic growth.

---

## References

Deslauriers D, Chipps SR, Breck JE, Rice JA, Madenjian CP (2017). Fish
Bioenergetics 4.0: An R-Based Modeling Application. *Fisheries* 42(11):586–596.
<https://doi.org/10.1080/03632415.2017.1377558>

Stewart DJ, Ibarra M (1991). Predation and production by salmonine fishes in
Lake Michigan, 1978–88. *Canadian Journal of Fisheries and Aquatic Sciences*
48(5):909–922. <https://doi.org/10.1139/f91-107>
         