-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-m.R
178 lines (119 loc) · 5.95 KB
/
03-m.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
## ----showClass_a4aM-----------------------------------------------------------
showClass("a4aM")
## ----m_02---------------------------------------------------------------------
mod02 <- FLModelSim(model=~a, params=FLPar(a=0.2))
m1 <- a4aM(level=mod02)
m1
## ----jensen_second_m----------------------------------------------------------
shape2 <- FLModelSim(model=~exp(-age-0.5))
level2 <- FLModelSim(model=~1.5*k, params=FLPar(k=0.4))
m2 <- a4aM(shape=shape2, level=level2)
m2
## ----gis_shape----------------------------------------------------------------
shape_len <- FLModelSim(model=~K*(linf/len)^1.5, params=FLPar(linf=60, K=0.4))
m_len <- a4aM(shape=shape_len)
## ----nao_m--------------------------------------------------------------------
# Get NAO
nao <- read.table("https://www.cpc.ncep.noaa.gov/products/precip/CWlink/pna/norm.nao.monthly.b5001.current.ascii.table", skip=1, fill=TRUE, na.strings="-99.90")
dnms <- list(quant="nao", year=1950:2024, unit="unique", season=1:12, area="unique")
# Build an FLQuant from the NAO data
nao.flq <- FLQuant(unlist(nao[,-1]), dimnames=dnms, units="nao")
# Build covar by calculating mean over the first 3 months
nao <- seasonMeans(trim(nao.flq, year=dimnames(stock.n(ple4))$year))
# Turn into Boolean
nao <- (nao>0)
# Constructor
trend3 <- FLModelSim(model=~1+b*nao, params=FLPar(b=0.5))
shape3 <- FLModelSim(model=~exp(-age-0.5))
level3 <- FLModelSim(model=~1.5*k, params=FLPar(k=0.4))
m3 <- a4aM(shape=shape3, level=level3, trend=trend3)
m3
## ----mvrnorm_m----------------------------------------------------------------
shape4 <- FLModelSim(model=~exp(-age-0.5))
level4 <- FLModelSim(model=~k^0.66*t^0.57, params=FLPar(k=0.4, t=10), vcov=array(c(0.002, 0.01,0.01, 1), dim=c(2,2)))
trend4 <- FLModelSim(model=~1+b*nao, params=FLPar(b=0.5), vcov=matrix(0.02))
m4 <- a4aM(shape=shape4, level=level4, trend=trend4)
# Call mvrnorm()
m4 <- mvrnorm(100, m4)
m4
## ----mvrnorm_m1---------------------------------------------------------------
m4@level
## ----mvrnorm_m2---------------------------------------------------------------
params(trend(m4))
## ----mvrnorm_m3---------------------------------------------------------------
params(shape(m4))
## ----univariate_m-------------------------------------------------------------
m4 <- a4aM(shape=shape4, level=mvrnorm(100, level4), trend=mvrnorm(100, trend4))
## ----gis_copula---------------------------------------------------------------
linf <- 60
k <- 0.4
# vcov matrix (make up some values)
mm <- matrix(NA, ncol=2, nrow=2)
# 10% cv
diag(mm) <- c((linf*0.1)^2, (k*0.1)^2)
# 0.2 correlation
mm[upper.tri(mm)] <- mm[lower.tri(mm)] <- c(0.05)
# a good way to check is using cov2cor
cov2cor(mm)
# create object
mgis2 <- FLModelSim(model=~k*(linf/len)^1.5, params=FLPar(linf=linf, k=k), vcov=mm)
# set the lower, upper and (optionally) centre of the parameters (without the centre, the triangle is symmetrical)
pars <- list(list(a=55,b=65), list(a=0.3, b=0.6, c=0.35))
mgis2 <- mvrtriangle(1000, mgis2, paramMargins=pars)
mgis2
## ----plot_tri_gis_m, echo=FALSE, fig.cap="Parameter estimates for Gislason's second natural mortality model from using a triangle distribution."----
splom(t(params(mgis2)@.Data), par.settings=list(plot.symbol=list(pch=19, cex=0.1, col=1)))
## ----plot_tri_gis_m_hist, echo=FALSE, fig.cap="Marginal distributions of the parameters for Gislason's second natural mortality model using a triangle distribution."----
par(mfrow=c(2,1))
hist(c(params(mgis2)["linf",]), main="Linf", xlab="")
hist(c(params(mgis2)["k",]), main="K", xlab="")
## ----making_complicated_m-----------------------------------------------------
m5 <- a4aM(shape=mgis2, level=level4, trend=trend4)
# or
m5 <- m4
shape(m5) <- mgis2
## ----simple_m-----------------------------------------------------------------
m1
## ----simple_m1----------------------------------------------------------------
range(m1)
## ----simple_m2----------------------------------------------------------------
m(m1)
## ----simple_m3----------------------------------------------------------------
range(m1, c("min","max")) <- c(0,7) # set the quant range
range(m1, c("minyear","maxyear")) <- c(2000, 2010) # set the year range
range(m1)
## ----simple_m4----------------------------------------------------------------
m(m1)
## ----m2-----------------------------------------------------------------------
m2
range(m2, c("min","max")) <- c(0,7) # set the quant range
range(m2, c("minyear","maxyear")) <- c(2000, 2003) # set the year range
range(m2)
m(m2)
## ----m2_1---------------------------------------------------------------------
predict(level(m2))
## ----m2_2---------------------------------------------------------------------
m(m2)["0"]
## ----m2_3---------------------------------------------------------------------
range(m2, c("minmbar","maxmbar")) <- c(0,5)
range(m2)
## ----m2_4---------------------------------------------------------------------
m(m2)
## ----m2_5---------------------------------------------------------------------
quantMeans(m(m2)[as.character(0:5)])
## ----m3_trend-----------------------------------------------------------------
m(m3, nao=1)
## ----m3_trend1----------------------------------------------------------------
range(m3, c("min","max")) <- c(0,7)
m(m3, nao=0)
## ----m3_trend2----------------------------------------------------------------
range(m3, c("minyear","maxyear")) <- c(2000, 2003)
m(m3, nao=as.numeric(nao[,as.character(2000:2003)]))
## ----m4_uncertainty_m---------------------------------------------------------
range(m4, c("min","max")) <- c(0,7)
range(m4, c("minyear","maxyear")) <- c(2000, 2003)
flq <- m(m4, nao=as.numeric(nao[,as.character(2000:2003)]))
flq
dim(flq)
## ----uncertain_m, echo=FALSE, fig.cap="Natural mortality with age and year trend."----
bwplot(data~factor(age)|year, data=flq, par.settings=list(plot.symbol=list(cex=0.2, col="gray50"), box.umbrella=list(col="gray40"), box.rectangle=list(col="gray30")), ylab="M", xlab="age (years)", scales=list(x=list(rot=90)))