-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrac2_solution.Rmd
122 lines (94 loc) · 4.28 KB
/
Prac2_solution.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
title: Exercise 2<br> Line transect estimation using R
author: Centre for Research into Ecological and Environmental Modelling <br> **University of St Andrews**
date: Introduction to distance sampling<br> August/September 2022
output:
rmdformats::readthedown:
highlight: tango
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```
<div class="alert alert-success">
<strong>Solutions</strong> Line transect estimation
</div>
# Inspect duck nest data
Import and check the data.
```{r, echo=TRUE, fig.width=4, fig.height=4}
library(Distance)
data(ducknest)
# Check data OK
head(ducknest, n=3)
# How many observations (note: detections made on every transect)
nrow(ducknest)
# Histogram with 8 bins
brks <- seq(from=0, to=2.4, by=0.3)
hist(ducknest$distance, breaks=brks, xlab="Distance (m)",
main="Perpendicular distances duck nests")
```
# Fit detection functions
Fit the three models using proper units of distance measure.
The answer is another function `convert_units`. Arguments to this function are
- distance_units
- units of measure for perpendicular/radial distances
- effort_units
- units of measure for effort (NULL for point transects)
- area_units
- units of measure for the study area.
```{r, echo=T, message=FALSE}
conversion.factor <- convert_units("Meter", "Kilometer", "Square Kilometer")
# Half-normal with no adjustments
nest.hn <- ds(ducknest, key="hn", adjustment=NULL,
convert_units=conversion.factor)
summary(nest.hn)
```
In addition to the half normal key function, fit uniform and hazard rate models with possible adjustment terms.
```{r, message=FALSE}
nest.uf.cos <- ds(ducknest, key="unif", adjustment="cos",
convert_units=conversion.factor)
nest.hr.herm <- ds(ducknest, key="hr", adjustment="herm",
convert_units=conversion.factor)
```
# Assess absolute model fit
The goodness of fit for the basic model is shown below.
```{r, echo=T}
gof_ds(nest.hn, plot=FALSE)
```
# Contrast competing models
A function useful for contrasting models is `summarize_ds_models`. A summary table of goodness of fit statistics for all models is created below.
```{r, echo=T}
# Summarise gof statistics
knitr::kable(summarize_ds_models(nest.hn, nest.uf.cos, nest.hr.herm, output="plain"),
caption="Model results for ducknest data set.", digits=3)
```
# Density estimates from the competing models
The density results from all models are summarized below.
```{r, echo=F}
# This chunk of code is fiddly and needed only to harvest results
# and make them look pretty in the output. Not important for your
# understanding of performing distance sampling estimation.
nest.tab <- data.frame(Model=1:3,
DetectionFunction=c("Half-normal, no adjustments",
"Uniform, cosine adjustments",
"Hazard rate, no adjustments "),
Density=rep(NA,3), LowerCI=rep(NA,3), UpperCI=rep(NA,3))
get.results.f <- function(fit.model) {
return(c(D=fit.model$dht$individuals$D$Estimate,
lCL=fit.model$dht$individuals$D$lcl,
uCL=fit.model$dht$individuals$D$ucl))
}
nest.tab[1,3:5] <- get.results.f(nest.hn)
nest.tab[2,3:5] <- get.results.f(nest.uf.cos)
nest.tab[3,3:5] <- get.results.f(nest.hr.herm)
knitr::kable(nest.tab,
caption="Density estimates and confidence intervals for three fitted models.",
digits = 2)
```
# Visualise shape of key functions with duck nest data
The detection function plots are shown below.
```{r, echo=T, out.width="33%", fig.show="hold", fig.width=6, fig.height=6}
plot(nest.hn, nc=8, main="Half normal, no adjustments")
plot(nest.uf.cos, nc=8, main="Uniform, cosine adjustments")
plot(nest.hr.herm, nc=8, main="Hazard rate, no adjustments")
```
The half-normal detection function with no adjustments has the smallest AIC which provides support for this model. The $\Delta$AIC values for all three models is small. In general, you should get similar density estimates using different detection function models, provided those models fit the data well, as in this example.