![cc](pix/cc-attrib-nc.png)
Licensed under the 
[Creative Commons attribution-noncommercial license](http://creativecommons.org/licenses/by-nc/3.0/).
Please share \& remix noncommercially, mentioning its origin.

# Basics

## outline

- explore data (plot)
- define objective function (neg log likelihood)
- decide on starting values
- choose optimization function (`optim()` etc.)

## Set up objective function

*either* 

- objective function as explicit function of parameter vector `p`
     - explicit
	 - arbitrarily complex
- *or* use `bbmle::mle2` formula interface
     - simpler, readable
	 - allows use of `predict()`, `simulate()`, `residuals()` ...
     - don't have standalone prediction/log-likelihood function (except `@minuslogl`)

# tadpoles (functional response)

From @VoneshBolker2005.

Suggested models:

```{r}
frogs <- emdbook::ReedfrogFuncresp
## predicted number killed with two different 
rogers_pred <- function(N0, a, h, P, time) {
  N0 - emdbook::lambertW(a * h * N0 * exp(-a * (P * time - h * N0)))/(a * h)
}
holling_pred <- function(N0, a, h, P, time) {
  a*N0*P*time/(1+a*h*N0)
}
time <- 14 ## time period
P <- 3  ## number of predators
```

- initial slope=$aPT$ ($a$ = predation rate per predator per unit time)
- asymptote = $PT/h$

# caterpillar virus

From @reilly_density-dependent_2008, a tiny data set:

```{r}
dd <- read.table(header=TRUE,
           text="
density larvae surviving
1   90  60
5   90  60
10  89  56
15  87  41
20  93  31
")
```

Suggested model: $S \sim \textrm{Binom}(p=\beta_0 + \beta_1 D^\beta_2$, N)$

**exercise**: fit a binomial GLM (and possibly a quadratic GLM). Then fit the same model with `bbmle` (hint: `plogis()` is the logistic function). Check that you got the same answers. Then fit the weird power-law model. Find predicted values.  Simulate some values.

## references
