-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.rand.sample.R
213 lines (180 loc) · 9.2 KB
/
test.rand.sample.R
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
library(ggplot2)
library(hrbrthemes)
library(tidyverse)
library(magrittr)
library(gridExtra)
library(ggpattern)
library(dplyr)
#### Test whether sampling is significantly non-random ####
#### adapted by Amy M. Van Cise ####
#### from code by K. Alexandra Curtis ([email protected]) ####
#### shared on March 6, 2020 ####
#################################################################
##function to get legend from ggplot list
get_legend<-function(myggplot){
tmp <- ggplot_gtable(ggplot_build(myggplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
#### Test for non-random sampling within Areas###
# create looping vector and empty data objects
subset <- c("Kauai/Niihau", "Oahu", "Maui Nui", "Hawaii")
stockwilcox.p <- vector()
stockplot.list <- list()
for (j in 1:length(subset)){
set.capdat <- capdat[which(capdat$Area == subset[j]),]
# Define parameter values
n.occasions <- length(unique(set.capdat$year)) # Number of capture occasions, defined by the data
n.original <- set.capdat %>% group_by(year) %>% filter(Sight.type == "Original") %>%
summarise(n = n()) %>% summarise(mean(n)) %>% .$"mean(n)" %>% round(.,digits = 0)
marked <- rep(n.original, ifelse(n.occasions > 1, n.occasions-1, 1)) # Annual number of newly marked individuals, defined by the data
phi <- rep(0.9, ifelse(n.occasions > 1, n.occasions-1, 1)) # survival (from Stolen and Barlow 2003)
p <- rep(0.4, ifelse(n.occasions > 1, n.occasions-1, 1)) # capture probability (from Kery 2012)
# Define matrices with survival and recapture probabilities
PHI <- matrix(phi, ncol = n.occasions-1, nrow = sum(marked))
P <- matrix(p, ncol = n.occasions-1, nrow = sum(marked))
# Define function to simulate a capture-history (CH) matrix
simul.cjs <- function(PHI, P, marked){
n.occasions <- dim(PHI)[2] + 1
CH <- matrix(0, ncol = n.occasions, nrow = sum(marked))
# Define a vector with the occasion of marking
mark.occ <- rep(1:length(marked), marked[1:length(marked)])
# Fill the CH matrix
for (i in 1:sum(marked)){
CH[i, mark.occ[i]] <- 1 # Write an 1 at the release occasion
if (mark.occ[i]==n.occasions) next
for (t in (mark.occ[i]+1):n.occasions){
# Bernoulli trial: does individual survive occasion?
sur <- rbinom(1, 1, PHI[i,t-1])
if (sur==0) break # If dead, move to next individual
# Bernoulli trial: is individual recaptured?
rp <- rbinom(1, 1, P[i,t-1])
if (rp==1) CH[i,t] <- 1
} #t
} #i
return(CH)
}
# Execute function
CH <- simul.cjs(PHI, P, marked)
mean(rowSums(CH))
#hist(rowSums(CH))
randsample <- cbind.data.frame(mean=rowSums(CH), type="random.sample")
# real data for annual resights
annual.resight <- capdat[which(capdat$Area == subset[j]),] %>% group_by(ID..,year) %>% tally() %>% summarize(mean = n()) %>% mutate(type = "annual.sight")
# test for differences from random
yeartest <- wilcox.test(annual.resight$mean,randsample$mean)
stockwilcox.p <- c(stockwilcox.p, yeartest$p.value)
# ggplot
#uniform font sizes
geom.text.size = 7
theme.size = (14/5) * geom.text.size
sight.hist <- rbind(annual.resight[c("mean","type")], randsample)
stockplot.list[[j]] <- ggplot(sight.hist, aes(mean, fill = type)) +
geom_density(color="#e9ecef", alpha = 0.3, position = "identity") +
scale_fill_manual(values=c("#69b3a2", "#404080"), labels = c("Real data", "Simulated data")) +
theme_classic(theme.size) +
labs(fill="")+
xlab("") +
ylab("") +
annotate("text", label = paste(ifelse(subset[j] == "Oahu", "O\u02BBahu",
ifelse(subset[j] == "Kauai/Niihau", "Kaua\u02BBi/Ni\u02BBihau",
ifelse(subset[j] == "Hawaii", "Hawai\u02BBi", subset[j]))),
", ",
ifelse(yeartest$p.value < 0.0001, "p < 0.0001",
paste("p = ", format(yeartest$p.value, digits = 2), sep = "")), sep = ""),
x = Inf, y = Inf, hjust = 1, vjust = 1, size = geom.text.size) +
theme(plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm"), legend.text=element_text(size=20))
# same plot, in black and white
# stockplot.list[[j]] <- ggplot(sight.hist, aes(mean)) +
# geom_density_pattern(aes(pattern=as.factor(type)), alpha = 0.6, position = "identity", pattern_density = 0.6) +
# scale_pattern_discrete(name = "", labels = c("Real dataset", "Simulated dataset")) +
# theme_bw(18) +
# theme(axis.title.x = element_text(hjust = 1)) +
# labs(fill="")+
# annotate("text", label = paste(ifelse(subset[j] == "Oahu", "O\u02BBahu",
# ifelse(subset[j] == "Kauai/Niihau", "Kaua\u02BBi/Ni\u02BBihau",
# ifelse(subset[j] == "Hawaii", "Hawai\u02BBi", subset[j]))),
# ", ",
# ifelse(yeartest$p.value < 0.0001, "p < 0.0001",
# paste("p = ", format(yeartest$p.value, digits = 2), sep = "")), sep = ""), x = Inf, y = Inf, hjust = 1, vjust = 1) +
# ylab("") +
# xlab("") +
# theme(plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm"))
}
#### Test for non-random sampling within subreas###
# create looping vector and empty data objects
subarea <- c("KA","KB","KC","KD","OA","OB","MA","MB","HA","HB")
subwilcox.p <- vector()
subplot.list <- list()
for (j in 1:length(subarea)){
set.capdat <- capdat[which(capdat$subarea == subarea[j]),] %>%
filter(!is.na(subarea))
# Define parameter values
n.occasions <- length(unique(set.capdat$year)) # Number of capture occasions, defined by the data
n.original <- set.capdat %>% group_by(year) %>% filter(Sight.type == "Original") %>%
summarise(n = n()) %>% summarise(mean(n)) %>% .$"mean(n)" %>% round(.,digits = 0)
marked <- rep(n.original, ifelse(n.occasions > 1, n.occasions-1, 1)) # Annual number of newly marked individuals, defined by the data
phi <- rep(0.9, ifelse(n.occasions > 1, n.occasions-1, 1)) # survival (from Stolen and Barlow 2003)
p <- rep(0.4, ifelse(n.occasions > 1, n.occasions-1, 1))# capture probability (from Kery 2012)
# Define matrices with survival and recapture probabilities
PHI <- matrix(phi, ncol = n.occasions-1, nrow = sum(marked))
P <- matrix(p, ncol = n.occasions-1, nrow = sum(marked))
# Define function to simulate a capture-history (CH) matrix
simul.cjs <- function(PHI, P, marked){
n.occasions <- dim(PHI)[2] + 1
CH <- matrix(0, ncol = n.occasions, nrow = sum(marked))
# Define a vector with the occasion of marking
mark.occ <- rep(1:length(marked), marked[1:length(marked)])
# Fill the CH matrix
for (i in 1:sum(marked)){
CH[i, mark.occ[i]] <- 1 # Write an 1 at the release occasion
if (mark.occ[i]==n.occasions) next
for (t in (mark.occ[i]+1):n.occasions){
# Bernoulli trial: does individual survive occasion?
sur <- rbinom(1, 1, PHI[i,t-1])
if (sur==0) break # If dead, move to next individual
# Bernoulli trial: is individual recaptured?
rp <- rbinom(1, 1, P[i,t-1])
if (rp==1) CH[i,t] <- 1
} #t
} #i
return(CH)
}
# Execute function
CH <- simul.cjs(PHI, P, marked)
mean(rowSums(CH))
#hist(rowSums(CH))
randsample <- cbind.data.frame(mean=rowSums(CH), type="random.sample")
#real data for annual resights
annual.resight <- capdat[which(capdat$subarea == subarea[j]),] %>% group_by(ID..,year) %>%
tally() %>% summarize(mean = n()) %>% mutate(type = "annual.sight")
#test for differences from random
yeartest <- wilcox.test(annual.resight$mean,randsample$mean)
subwilcox.p <- c(subwilcox.p, yeartest$p.value)
#ggplot
sight.hist <- rbind(annual.resight[c("mean","type")], randsample)
subplot.list[[j]] <- ggplot(sight.hist, aes(mean, fill = type)) +
geom_density(color="#e9ecef", alpha = 0.4, position = "identity") +
scale_fill_manual(values=c("#69b3a2", "#404080"), labels = c("Real data", "Simulated data")) +
theme_classic(theme.size) +
labs(fill="") +
xlab("") +
ylab("") +
annotate("text", label = paste(subarea[j], ", ",
ifelse(yeartest$p.value < 0.0001, "p < 0.0001",
paste("p = ", format(yeartest$p.value, digits = 2), sep = "")), sep = ""),
x = Inf, y = Inf, hjust = 1, vjust = 1, size = geom.text.size) +
theme(plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm"), legend.text=element_text(size=20))
# same plot, in black and white
# subplot.list[[j]] <- ggplot(sight.hist, aes(mean)) +
# geom_density_pattern(aes(pattern=as.factor(type)), alpha = 0.6, position = "identity", pattern_density = 0.6) +
# theme_bw(18) +
# theme(axis.title.x = element_text(hjust = 1)) +
# labs(fill="")+
# annotate("text", label = paste(subarea[j], ", ",
# ifelse(yeartest$p.value < 0.0001, "p < 0.0001",
# paste("p = ", format(yeartest$p.value, digits = 2), sep = "")), sep = ""), x = Inf, y = Inf, hjust = 1, vjust = 1) +
# scale_pattern_discrete(name = "", labels = c("Real dataset", "Simulated dataset")) +
# theme(plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm"))
}