-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_data_sock_enviro.R
66 lines (47 loc) · 2.62 KB
/
04_data_sock_enviro.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
## Combine sockeye + environmental data
##
## This script assigns annual environmental data to each stock specific brood
## year. Because sockeye populations can enter the ocean over multiple years,
## weighted means of the annual environmental data are calculated to index ocean
## conditions for a brood year, where the weights are equal to the proportion of
## sockeye enter the ocean in a given year.
# Prep data ------------------------------------------------
sock.covar <- brood.table
## Calc stock specific SST averages ------------------------
sst.stock <- sst_averager(sock.info, sst.anom, distance = 400)
names(sst.stock) <- c("year","sst_raw", "sst_anom", "stock_id")
## Add enviro indices --------------------------------------
## SST
sock.covar$sst_anom <- wgt_mean_sst(sock.covar,
sst.stock,
sst.var = "sst_anom")
sock.covar$sst_raw <- wgt_mean_sst(sock.covar,
sst.stock,
sst.var = "sst_raw")
## PDO
sock.covar$pdo_winter <- wgt_mean_pdo(sock.covar, pdo.winter)
sock.covar$pdo_spring <- wgt_mean_pdo(sock.covar, pdo.spring)
sock.covar$pdo_summer <- wgt_mean_pdo(sock.covar, pdo.summer)
## NPGO
sock.covar$npgo_winter <- wgt_mean_pdo(sock.covar, npgo.winter)
sock.covar$npgo_spring <- wgt_mean_pdo(sock.covar, npgo.spring)
sock.covar$npgo_summer <- wgt_mean_pdo(sock.covar, npgo.summer)
## Add derived columns -------------------------------------
sock.covar <- plyr::ddply(sock.covar, .(stock_no), transform,
rps = recruits / spawners,
rps_stnd = scale(recruits / spawners)[ , 1],
ln_rps = log(recruits / spawners),
rk_resid = residuals(lm(log(recruits / spawners) ~ spawners)),
sst_anom_stnd = scale(sst_anom)[ , 1],
sst_raw_stnd = scale(sst_raw)[ , 1],
pdo_winter_stnd = scale(pdo_winter)[ , 1],
pdo_spring_stnd = scale(pdo_spring)[ , 1],
pdo_summer_stnd = scale(pdo_summer)[ , 1],
npgo_winter_stnd = scale(npgo_winter)[ , 1],
npgo_spring_stnd = scale(npgo_spring)[ , 1],
npgo_summer_stnd = scale(npgo_summer)[ , 1])
sock.covar$stock <- factor(sock.covar$stock,
levels = unique(sock.covar$stock))
## Save output ---------------------------------------------
save(sst.stock, file = "./output/sst_stock.RData")
save(sock.covar, file = "./output/sock_covar.RData")