---
title: "FW 536 — Day 4 morning lab (Maximum Likelihood) — Your Name"
author: "Your Name Here"
date: "`r Sys.Date()`"
output:
  html_document:
    self_contained: true
    toc: true
    toc_depth: 3
    toc_float: true
    code_folding: show
    theme: cosmo
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```

# Day 4 morning lab — Maximum likelihood problem set

> **Topic scope.** These are the *morning* problems and use **maximum likelihood only** — likelihood functions, grid search, `optim()`/`stats4::mle()`, likelihood-ratio tests, and profile-likelihood confidence intervals. Bayesian methods (priors, posteriors, MCMC) are taught this afternoon; do **not** use them here. The afternoon problems live in `afternoon_lab_template.Rmd`.

This template matches Problems 1–4 of [`problem_set.html`](problem_set.html). Write your work below, **Knit** to HTML, and upload the `.html` to the matching Canvas assignment.

For setup help see the [R Markdown tutorial](../rmarkdown_tutorial.html).

---

# Setup

```{r}
# Packages for the morning (maximum likelihood) session.
library(ggplot2)
library(dplyr)
library(stats4)   # mle() and confint() for profile-likelihood CIs

# The morning problems load three real course datasets from the data/ folder:
#   data/vonbert.csv   (Problem 2, von Bertalanffy growth)
#   data/whale.csv     (Problem 3, two-component Poisson mixture)
#   data/Logistic.csv  (Problem 4, logistic r/K growth by MLE)
```

---

# Problem 1 — Post-release survival from a tagging study

> You released 80 fish after barotrauma treatment; 47 were detected alive after 7 days. Treat each outcome as independent Bernoulli($\phi$). Derive the MLE $\hat\phi$ and Fisher-information Wald CI by hand; get the profile CI from `mle()` + `confint()`; and explain why MLE alone cannot answer "$\Pr(\phi \ge 0.5 \mid \text{data})$".

**My work:**

```{r}
# Your R code here
```

**Answer / biological interpretation:**


---

# Problem 2 — Von Bertalanffy growth from length-at-age data (vonbert.csv)

> Load `data/vonbert.csv` (sex, age, length). Run a grid search for $\kappa$, fit the full 3-parameter model with `mle()`, and test for a sex effect with a likelihood-ratio test.

```{r}
# Load exactly as in the lab:
# TheData <- read.csv("data/vonbert.csv")
# Sex <- TheData$sex; Age <- TheData$age; Len <- TheData$length
```

**My work:**

```{r}
# Your R code here
```

**Answer / biological interpretation:**


---

# Problem 3 — Two-component Poisson mixture for whale blow counts (whale.csv)

> Load `data/whale.csv` (sex, blows). Fit the mixture $f(y) = \pi\,\text{Pois}(y;\lambda_1) + (1-\pi)\text{Pois}(y;\lambda_2)$ with `mle()` from several starts; discuss multimodality and label switching (the two components correspond to males and females).

```{r}
# Load exactly as in the lab:
# TheData <- read.csv("data/whale.csv")
# blows <- TheData$blows
```

**My work:**

```{r}
# Your R code here
```

**Answer / biological interpretation:**


---

# Problem 4 — Logistic (r/K) growth by maximum likelihood (Logistic.csv)

> Load `data/Logistic.csv` (PopulationSize, GrowthRate). Fit the per-capita linear model, recover $\hat r$ and $\hat K$, reproduce them with `mle()`, and compute the plug-in MLE of MSY $= rK/4$.

```{r}
# Load exactly as in the lab:
# Logistic <- read.csv("data/Logistic.csv", header = TRUE)
# Logistic <- Logistic[order(Logistic$PopulationSize), ]
```

**My work:**

```{r}
# Your R code here
```

**Answer / biological interpretation:**


---

# (Optional) Embed a photo of hand-written work

```{r, echo=FALSE, eval=FALSE, out.width="60%", fig.cap="Hand-derivation for Problem N"}
knitr::include_graphics("images/problemN_derivation.jpg")
```

---

# Submission checklist

- [ ] Answered every morning problem (1–4) from `problem_set.html`.
- [ ] Used **maximum likelihood only** — no priors, posteriors, or MCMC.
- [ ] Loaded `vonbert.csv`, `whale.csv`, and `Logistic.csv` from the `data/` folder.
- [ ] Knitted to HTML without errors.
- [ ] Photos (if any) embedded with `self_contained: true` in the YAML.
- [ ] Uploaded the `.html` to the matching Canvas assignment.
