The Distance package written by Dave Miller was intended to permit analysis of simple datasets in R.

Using the “flatfile” feature of Distance, loading data is simple. We show as examples, the analysis of a simulated survey of duck nests, consisting of 20 transects, each 128.8km in length. These data exist as a comma-delimited text file in the current directory (one can find the current working directory in R using getwd() and change it with setwd()).

library(Distance)
tidy.opts <- list(width.cutoff=40)
ducknests <- read.csv("ducknests.csv")
head(ducknests)
     Region.Label Area Sample.Label Effort distance
1 Monte_Vista_NWR    0            1  128.8     0.06
2 Monte_Vista_NWR    0            1  128.8     0.07
3 Monte_Vista_NWR    0            1  128.8     0.04
4 Monte_Vista_NWR    0            1  128.8     0.01
5 Monte_Vista_NWR    0            1  128.8     0.37
6 Monte_Vista_NWR    0            1  128.8     0.36

Note the following:

Detection function fitting

The below code fits three detection functions: half-normal with cosine adjustments, uniform with cosine adjustments and hazard-rate with simple polynomial adjustments. In each case the number of adjustment terms is selected by AIC. The selection process can be seen in the output.

# Convert.units adjusts for distance measured in metres and effort in km
halfnorm.ducks <- ds(ducknests, key="hn", adjustment="cos", convert.units = 0.001)
## Starting AIC adjustment term selection.
## Fitting half-normal key function
## AIC= 928.134
## Fitting half-normal key function with cosine(2) adjustments
## Warning: Detection function is not weakly monotonic!
## Warning: Detection function is not strictly monotonic!
## Warning: Detection function is greater than 1 at some distances
## AIC= 929.834
## 
## 
## half-normal key function selected!
unifcos.ducks <- ds(ducknests, key="unif", adjustment="cos", mono="strict", convert.units = 0.001)
## Starting AIC adjustment term selection.
## Fitting uniform key function with cosine(1) adjustments
## AIC= 928.48
## Fitting uniform key function with cosine(1,2) adjustments
## AIC= 929.383
## 
## 
## uniform key function with cosine(1) adjustments selected!
hazard.ducks <- ds(ducknests, key="hr",  adjustment="poly", convert.units = 0.001)
## Starting AIC adjustment term selection.
## Fitting hazard-rate key function
## AIC= 929.799
## Fitting hazard-rate key function with simple polynomial(2) adjustments
## Warning: Detection function is not weakly monotonic!
## Warning: Detection function is not strictly monotonic!
## Warning: Detection function is greater than 1 at some distances
## AIC= 931.673
## 
## 
## hazard-rate key function selected!

For both the half-normal and hazard-rate functions, ds complains that the detection function is non-monotone (that detection probability increases with distance at some points in the fitted function), however these are for the models with adjustments which are not selected.

Model selection

AIC scores for the three fitted models:

Model AIC
Half normal 928.1338
Uniform cosine 928.4797
Hazard rate 929.799

Model fit

Having completed the model selection phase, produce depiction of detection function, show its goodness of fit.

par(mfrow=c(1,2))
plot(halfnorm.ducks, main="Duck nests, half-normal detection function")
fit.test <- ddf.gof(halfnorm.ducks$ddf)

Perpendicular distances fitted with half normal detection function

par(mfrow=c(1,1))

My preference is to pluck out the Cramer-von Mises goodness of fit test. For these data, this test has a test statistic value of 0.0354 with an associated p-value of 0.9554

Content with adequate fit of the model to these data, we want to present our density estimate.

Population inference

From the first table below, we see that 534 nests were encountered along 2574km of transects. The encounter rate (ER) is the ratio of these values, implying that roughly 5 nests were encountered per kilometre of transect. We also see the variablity of the encounter rate.

kable(halfnorm.ducks$dht$individuals$summary,format="markdown")
Region Area CoveredArea Effort n k ER se.ER cv.ER
Monte_Vista_NWR 12.36 12.36 2575 534 20 0.2074 0.008 0.0384

This second table produces our estimate of duck nest density within the region covered by the survey transects. The estimate of 49.7 is in units of nests kilometre-2, with associated measures of precision in the form of standard error, coefficient of variation and 95% confidence interval bounds.

kable(halfnorm.ducks$dht$individuals$D,format="markdown")
Label Estimate se cv lcl ucl df
Total 49.7 2.937 0.0591 44.2 55.87 99.56

Finally at the end of the analysis, reset the R environment in preparation for another analysis.

rm(list=ls())