-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsampling_routine.R
309 lines (213 loc) · 9.31 KB
/
subsampling_routine.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
rm(list=ls())
setwd("C:/Users/mandy.karnauskas/Desktop/RS_FATEproject/FINALRUNS_DEC2020/final_for_subsample")
# set directory ---------------
foldersel <- dir()
write.table(c(), file = "C:/Users/mandy.karnauskas/Desktop/RS_FATEproject/FINALRUNS_DEC2020/outputs2.txt",
quote = FALSE, append = F, col.names = F)
# loop through folders to calculate GoM-Atl connectivity ---------------------
for (i in 1:length(foldersel)) {
filelist <- list.files(path = foldersel[i], pattern="con_file") # find files
for (j in 1:length(filelist)) {
dat <- read.table(paste0(foldersel[i], "/", filelist[j]))
colnames(dat) <- c("rel_poly","ret_poly","ret_yr","ret_mo","ret_d","age","ret_dep","rel_yr","rel_mo","rel_d")
##################### NEW #######################
# sf <- 77:79
# remov <- which(dat$ret_poly %in% sf)
# dat <- dat[-remov, ]
####################################################
dat$ret_reg <- "GOM"
dat$ret_reg[which(dat$ret_poly >= 76)] <- "ATL"
dat$rel_reg <- "GOM"
dat$rel_reg[which(dat$rel_poly >= 76)] <- "ATL"
tab <- table(dat$rel_reg, dat$ret_reg)
num <- tab[2, 1] / (tab[1, 1] + tab[2, 1]) # of all recruits in Atlantic, what % came from Gulf
res <- c(foldersel[i], j, num, as.vector(tab))
write.table(matrix(res, 1, 7), file = "C:/Users/mandy.karnauskas/Desktop/RS_FATEproject/FINALRUNS_DEC2020/outputs2.txt",
quote = FALSE, append = T, col.names = F)
}
}
# read in summary output calculated above and plot -------------------
rm(list = ls())
d <- read.table("C:/Users/mandy.karnauskas/Desktop/RS_FATEproject/FINALRUNS_DEC2020/outputs2.txt", sep = " ")
d <- d[,2:4]
names(d) <- c("fold", "year", "ga")
d$model <- unlist(strsplit(as.character(d$fold), "_"))[seq(1, nrow(d)*2, 2)]
d$spp <- unlist(strsplit(as.character(d$fold), "_"))[seq(2, nrow(d)*2, 2)]
d$spp[which(d$spp == "lane")] <- "OVM 1"
d$spp[which(d$spp == "mutton")] <- "OVM 2"
d$spp[which(d$spp == "grey")] <- "OVM 3"
head(d)
table(as.numeric(as.factor(d$model))+1, d$model)
table(as.numeric(as.factor(d$spp)), d$spp)
plot(jitter(d$year), d$ga, col = as.numeric(as.factor(d$model))+1, pch = as.numeric(as.factor(d$spp)), cex = 2,
xlab = "", ylab = "percentage of Atlantic recruits spawned from Gulf", axes = F)
axis(2, las = 2); box()
legend("topright", c(unique(d$model), unique(d$spp)), pch = c(19, 19, 19, 1:3), col = c(2:4, 1, 1, 1))
d$model[which(d$model == "SABGOM")] <- " SABGOM"
# barplot of summary results -----------------
library(ggplot2)
png(filename="C:/Users/mandy.karnauskas/Desktop/RS_FATEproject/plots/summary_boxplot2.png", units="in", width=4.5, height=3.5, pointsize=12, res=72*10)
ggplot(d, aes(y = ga*100, fill = year), axes = F) + ylim(0, 35) +
geom_boxplot(width = 3) + facet_wrap(~model) +
#scale_y_continuous(breaks = pretty(c(0, 35), n = 5)) +
scale_x_discrete(name="", breaks = 1, labels = "") +
ylab("% of Atlantic recruits spawned in Gulf") +
scale_fill_discrete(name = " vertical\nmigration\n behavior") +
theme(legend.position = 'right')
dev.off()
# other plots ----------
tapply(d$ga, d$model, mean)
tapply(d$ga, d$model, sd)
tapply(d$ga, d$model, sd) / tapply(d$ga, d$model, mean)
#SABGOM HYCOM Mercator
#0.2734216 0.4033875 0.5891569
#0.2636748 0.4746301 0.6493526
tab <- tapply(d$ga, list(d$spp, d$model), mean)
apply(tab, 2, mean)
apply(tab, 2, sd)
apply(tab, 2, sd) / apply(tab, 2, mean)
#SABGOM HYCOM Mercator
#0.05441065 0.14830738 0.07045451
#0.06142489 0.12241253 0.13884702
d$model <- as.factor(d$model)
d$spp <- as.factor(d$spp)
d$year <- as.factor(d$year)
out <- lm(d$ga ~ d$model + d$spp + d$year)
summary(out)
anova(out)
rm(list = ls())
d <- read.table("C:/Users/mandy.karnauskas/Desktop/RS_FATEproject/FINALRUNS_DEC2020/outputs2.txt", sep = " ")
d <- d[,2:8]
names(d) <- c("fold", "year", "gaper", "aa", "ga", "ag", "gg")
d$model <- unlist(strsplit(as.character(d$fold), "_"))[seq(1, nrow(d)*2, 2)]
d$spp <- unlist(strsplit(as.character(d$fold), "_"))[seq(2, nrow(d)*2, 2)]
d$GoM2Atl <- d$ga / (d$aa + d$ga)
round(d$gaper, 6) - round(d$GoM2Atl, 6)
d2 <- cbind(d$aa, d$ga, d$ag, d$gg)
barplot(t(d2), beside = F)
rowSums(d2)
d$model <- as.factor(d$model)
tapply(rowSums(d2), d$model, mean)
boxplot(rowSums(d2) ~ d$model)
anova(lm(rowSums(d2) ~ d$model)) # SABGOM and Mercator similar total survival rates; HYCOM slightly lower
d3 <- d2 / rowSums(d2)
barplot(t(d3), beside = F)
boxplot(d$aa ~ d$model) # SABGOM much stronger self-recruitment in Atl
anova(lm(d$aa ~ d$model))
boxplot(d$ga ~ d$model) # SABGOM slightly lower transport Gulf to Atl
anova(lm(d$ga ~ d$model))
boxplot(d$ag ~ d$model)
anova(lm(d$ag ~ d$model))
boxplot(d$gg ~ d$model)
anova(lm(d$gg ~ d$model))
# subsampling routine for calculating distributions based on biomass assumptions ----------------------
rm(list=ls())
setwd("C:/Users/mandy.karnauskas/Desktop/RS_FATEproject/FINALRUNS_DEC2020/final_for_subsample")
foldersel <- dir() # [grep("SABGOM", dir())]
numsel <- 10000
finres <- c(NA, NA, NA, NA)
par(mfrow = c(5, 2), mar = c(3, 3, 0, 0))
for (b in 1:10) {
mod <- rep(NA, numsel)
num <- rep(NA, numsel)
for (i in 1:numsel) {
x1 <- sample(1:length(foldersel), 1)
mod[i] <- foldersel[x1]
filelist <- list.files(path = foldersel[x1], pattern="con_file") # find files
x2 <- sample(1:length(filelist), 1)
dat <- read.table(paste0(foldersel[x1], "/", filelist[x2]))
colnames(dat) <- c("rel_poly","ret_poly","ret_yr","ret_mo","ret_d","age","ret_dep","rel_yr","rel_mo","rel_d")
##################### NEW #######################
sf <- 76:79
remov <- which(dat$ret_poly %in% sf)
dat <- dat[-remov, ]
####################################################
dat$ret_reg <- "GOM"
dat$ret_reg[which(dat$ret_poly >= 76)] <- "ATL"
dat$rel_reg <- "GOM"
dat$rel_reg[which(dat$rel_poly >= 76)] <- "ATL"
gomdat <- dat[which(dat$rel_reg == "GOM"),]
atldat <- dat[which(dat$rel_reg == "ATL"),]
biomfact <- b
samp <- sample(1:nrow(gomdat), nrow(gomdat) * biomfact, replace = TRUE)
# cat(length(samp)/nrow(gomdat)) # check
# cat("\n")
gomdat2 <- gomdat[samp,]
dat2 <- rbind(atldat, gomdat2)
tab <- table(dat2$rel_reg, dat2$ret_reg)
num[i] <- tab[2, 1] / (tab[1, 1] + tab[2, 1]) # of all recruits in Atlantic, what % came from Gulf
}
boxplot(num ~ mod, xlab = "model", ylab = "percent of recruits in Atl\nspawned from Gulf")
res <- c(biomfact, quantile(num, mean(num), probs = c(0.025, 0.50, 0.975)))
finres <- rbind(finres, res)
}
finres
#mean(num)
#max(num)
#min(num)
#hist(num)
#tapply(num, mod, mean)
finres
# old code
########## plot new grid cells #########################
pols <- unique(d$pol)
are <- rep(NA, length(unique(d$pol)))
plot(0, ylim=c(24, 36), xlim=c(-100, -75)) # check
for (i in 1:117) {
f <- d[which(d$pol==i),]
polygon(f, border=1) }
map('state', add=T)
text(tapply(d$lon, d$pol, mean), tapply(d$lat, d$pol, mean), unique(d$pol), cex=0.5)
are[1:41] <- "W GoM"
are[42:75] <- "E GoM"
are[76:89] <- "E FL"
are[90:95] <- "GA"
are[96:106] <- "SC"
are[107:117] <- "NC"
are <- as.factor(are)
tab <- table(pols, are)
map('state', fill = 1, interior=F, col = gray(0.95), ylim=c(23,36), xlim=c(-99,-75))
for (j in unique(d[,3])) {
m <- d[which(d[,3]==j),]; polygon(m, lwd=3, col=as.numeric(are)[j]+1, border = 1, lwd=0.5) }
axis(1); axis(2); box()
for (j in unique(are)) { text( mean(d$lon[d$pol %in% which(are==j)]), mean(d$lat[d$pol %in% which(are==j)]), j, col=1) }
abline(v=-89.1, lty=2); text(-87.5, 24, "longitude = -89.1")
mids <- tapply(pols, are, mean)
maxs <- tapply(pols, are, max)
mins <- tapply(pols, are, min)
tklocs <- maxs + 0.5
lablocs <- sort((tklocs-mins-0.5)/2 + mins) # used in plotting later
tklocs <- c(89.5, 76.5, 95.5, 117.5, 106.5, 41.5)
lablocs <- c(21.0, 59.0, 83.0, 92.5, 101.0, 112.0)
efl <- which(tab[,1]==1)
egom <- which(tab[,2]==1)
ga <- which(tab[,3]==1)
nc <- which(tab[,4]==1)
sc <- which(tab[,5]==1)
wgom <- which(tab[,6]==1)
dat$state <- NA
dat$state[which(dat$ret_poly<=max(egom))] <- "E GOM"
dat$state[which(dat$ret_poly>=min(wgom) & dat$ret_poly<=max(wgom))] <- "W GOM"
dat$state[which(dat$ret_poly>=min(efl) & dat$ret_poly<=max(efl))] <- "E FL"
dat$state[which(dat$ret_poly>=min(ga) & dat$ret_poly<=max(ga))] <- "GA"
dat$state[which(dat$ret_poly>=min(sc) & dat$ret_poly<=max(sc))] <- "SC"
dat$state[which(dat$ret_poly>=min(nc))] <- "NC"
##################################################################################
# Gulf SSB vs Atlantic NAA plot
d <- read.table("RS_S73_NAA.csv", header = T, sep = ",")
d <- d[which(d$year >=1978), ]
dim(d)
cols <- rainbow(20, end = 0.8)
cols[1] <- "darkgray"
dim(t(d[, 2:21]))
par(mar = c(4, 5, 1, 6))
b <- barplot(as.matrix(t(d[, 2:21])), space = 0, col = cols, names.arg = d$year, las = 2, axes = F,
legend = c(1:20), args.legend = list(x = 20, y = 2500000, ncol = 2, bty = "n"), border = NA)
axis(4, las = 2, at = seq(0, 2500000, 500000), lab = seq(0, 2500000, 500000)*10^5*1000/10^6)
axis(2, las = 2, at = seq(0, 2500000, 500000), lab = seq(0, 2500000, 500000)/10^6)
lines(b, d$Gulf_SSB/10^5, lwd = 3)
abline(h=0)
points(b, d$Gulf_SSB/10^5, pch = 20, cex = 1.5)
mtext(side = 2, line = 2.5, "South Atlantic numbers at age (millions of fish)")
mtext(side = 4, line = 4.5, "Eastern Gulf spawning output (millions of eggs)")
ccf(d$X1[1:39], d$Gulf_SSB[1:39])