-
Notifications
You must be signed in to change notification settings - Fork 0
/
flexdashboard_occupancy.Rmd
158 lines (128 loc) · 4.33 KB
/
flexdashboard_occupancy.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: "Bias in single-season occupancy models"
author: "Olivier Gimenez"
output:
flexdashboard::flex_dashboard:
orientation: columns
social: menu
source_code: embed
runtime: shiny
---
```{r global, include=FALSE}
# Load packages
library(unmarked)
library(tidyverse)
# Define function to carry out simulations:
occu_par <- function(
nb_sites = 50, # number of sites
nb_surveys = 5, # number of surveys
occpr = 0.3, # occupancy prob
detpr = 0.5, # detection prob
n_sim = 500){ # number of simulations
# preallocate memory for storing occupancy estimates
res <- rep(NA, n_sim)
# simulate data from a static occupancy model n_sim times
for (j in 1:n_sim){
# define state process
z <- rbinom(nb_sites, 1, occpr) # occupancy state
# pre-allocate memory for matrix of detection/non-detections
y <- matrix(NA, nrow = nb_sites, ncol = nb_surveys) # detection histories
# define observation process
for(i in 1:nb_sites){
prob <- z[i] * detpr
y[i,1:nb_surveys] <- rbinom(nb_surveys, 1, prob)
}
# format data
dat <- unmarkedFrameOccu(y)
# fit static occupancy model w/ constant parameters
fm <- occu(~ 1 ~ 1, dat)
# get estimate of occupancy prob
res[j] <- backTransform(fm, type = 'state')@estimate
}
# return relative bias in %
bias <- round(mean((res - occpr)/occpr) * 100,1)
return(bias)
}
# grids on detection/occupancy probabilities
det <- seq(0.05, 0.95, by = 0.1)
occ <- seq(0.05, 0.95, by = 0.1)
```
Column {.sidebar}
-----------------------------------------------------------------------
Compute the relative bias (in %) in the maximum-likelihood estimator of the occupancy probability $\psi$ in a single-season (aka static) occupancy model with constant parameters fitted with the package `unmarked`. Relative bias is $(E(\hat \psi) - \psi)/\psi$. Warning: the number of simulations is set to 10 by default, you might want to use 100 or 500 instead.
```{r}
sliderInput("nsites", label = "Number of sites:",
min = 10, max = 100, value = 10, step = 1)
sliderInput("nsurveys", label = "Number of surveys:",
min = 5, max = 50, value = 5, step = 1)
selectInput("nsim", label = "Number of simulations:",
choices = c(10, 100, 500), selected = 10)
selectInput("pal", label = "Color palette:",
choices = c("BrBG", "PiYG", "PRGn", "PuOr", "RdBu", "RdGy", "RdYlBu", "RdYlGn", "Spectral"), selected = "BrBG")
```
Column {data-width=600}
-----------------------------------------------------------------------
### What is the amount of bias in the probability of occupancy ?
```{r}
selectedData <- reactive({
# Initialize a table for storing results:
sim <- tibble(det = double(),
occ = double(),
nsites = double(),
nsurveys = double(),
bias = double())
# Compute bias:
for (i in det){
for (j in occ){
res <- occu_par(nb_sites = as.numeric(input$nsites),
nb_surveys = as.numeric(input$nsurveys),
occpr = j,
detpr = i,
n_sim = as.numeric(input$nsim))
sim <- sim %>% add_row(det = i,
occ = j,
nsites = as.numeric(input$nsites),
nsurveys = as.numeric(input$nsurveys),
bias = res)
}
}
sim
})
```
```{r}
# Visualize bias:
renderPlot({
limit <- selectedData() %>%
pull(bias) %>%
max(abs(.)) * c(-1, 1) # to set midpoint at 0
selectedData() %>%
ggplot() +
aes(x = det,
y = occ,
fill = bias) +
geom_tile() +
scale_fill_distiller(palette = input$pal,
type = 'div',
direction = 1,
name = 'relative bias (%)',
limit = limit) +
labs(x = 'detection probability',
y = 'occupancy probability') +
theme_bw(base_size = 14) +
theme(legend.position="right",
legend.title = element_blank())
})
```
Column {data-width=400}
-----------------------------------------------------------------------
### Simulation details {data-width=400}
```{r}
renderTable({
selectedData() %>%
rename('Pr(det)' = det,
'Pr(occ)' = occ,
'Nb sites' = nsites,
'Nb surveys' = nsurveys,
'Bias' = bias)
})
```