```{r setup, echo=FALSE}
tidy.opts <- list(width.cutoff=50)
```

\pagenumbering{gobble}

This document is designed to give you some pointers so that you can perform the Mark-Recapture Distance Sampling practical directly using the mrds package in R, rather than via the Distance visual interface.  I assume you have some knowledge of R, the mrds package, and Distance.

Crabeater seal survey
-------------------

```{r, echo=FALSE}
library(knitr)
library(knitcitations)
cleanbib()
options("citation_format" = "pandoc")
```

This analysis is described in `r citet("10.1111/j.1541-0420.2005.00493.x")` of aerial survey data looking for seals in the Antarctic pack ice.  There were four observers in the plane, two on each side (front and back).

The data from the survey has been saved in a `.csv` file. This file can be easily read into R, and with the `checkdata()` function, the information to construct the region, sample, and observation table can be extracted.  Note that these tables are only needed when estimating abundance by scaling up from the covered region to the study area.

```{r, comment=NA}
library(Distance)
crabseal <- read.csv("crabbieMRDS.csv")
#  Half normal detection function, 700m truncation distance,
#      logit function for mark-recapture component
crab.ddf.io <- ddf(method="io", dsmodel=~cds(key="hn"),
                 mrmodel=~glm(link="logit", formula=~distance),
                 data=crabseal, meta.data=list(width=700))
summary(crab.ddf.io)
```

Goodness of fit could be examined in the same manner as the golf tees by the use of `ddf.gof(crab.ddf.io)` but I have not shown this step.

Following model criticism and selection, estimation of abundance ensues.  the estimates of abundance for the study area are arbitrary because inference of the study was restricted to the covered region.  Hence the estimates of abundance here are artificial, but if we wished to produce them, we would need to produce the region, sample, and observation tables and apply Horvitz-Thompson like estimators to produce estimates of $\hat{N}$.  The use of `covert.units` adjusts the units of perpendicular distance measurement (m) to units of transect effort (km).  Be sure to perform the conversion correctly or your abundance estimates will be off by orders of magnitude.

```{r, comment=NA}
tables <- Distance:::checkdata(crabseal)
crab.ddf.io.abund <- dht(region=tables$region.table, sample=tables$sample.table, obs=tables$obs.table,
                         model=crab.ddf.io, se=TRUE, options=list(convert.units=0.001))
print(crab.ddf.io.abund)
```

##References

```{r, echo=FALSE, results='hide', message=FALSE}
write.bibtex(file="references.bib")
```
