-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_jagsNECfit.R
374 lines (350 loc) · 11.5 KB
/
plot_jagsNECfit.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Copyright 2020 Australian Institute of Marine Science
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#' plot.jagsNEC
#'
#' Generates a plot of a fitted jagsNEC model, as returned by fit.jagsNEC.
#'
#' @param X the jagsNEC model fit as returned by fit.jagsNEC.
#'
#' @param CI a logical value indicating if confidence intervals on the model fit should be plotted,
#' calculated as the upper and lower bounds of the individual predicted values from all posterior samples
#'
#' @param posterior.median a logical value indicating if the posterior median of the model fit should be plotted,
#' calculated as the median of the individual predicted values from all posterior samples
#'
#' @param median.model a logical value indicating if the fitted model calculated from the median estimates of the NEC,
#' top and beta parameters should be plotted. This is the fitted model as shown in Fox 2010.
#'
#' @param add.NEC a logical value indicating if the estimated NEC value and 95\% credible intervals should be added to the plot.
#'
#' @param add.EC10 a logical value indicating if an estimated EC10 value and 95\% credible intervals should be added to the plot.
#'
#' @param legend.loc a vector indicating the location of the NEC or EC10 legend, as per a call to legend.
#'
#' @param xform a function to be applied as a transformation of the x data.
#'
#' @param lxform a function to be applied as a transformation only to axis labels and the annoted NEC/EC10 values.
#'
#' @param jitter.x a logical value indicating if the x data points on the plot should be jittered.
#'
#' @param jitter.y a logical value indicating if the y data points on the plot should be jittered.
#'
#' @param xlab a character vector to use for the x-axis label
#'
#' @param ylab a character vector to use for the y-axis label
#'
#' @param xlim a numeric vector of length two to use for the lower and uper limits of the x-axis range
#'
#' @param xticks a numeric vector indicating where to place the tick marks of the x-axis
#'
#' @export
#' @return a plot of the fitted model
plot.jagsNECfit <- function(X,
CI = TRUE,
posterior.median = TRUE,
median.model = FALSE,
add.NEC = TRUE,
add.EC10 = FALSE,
legend.loc = "topright",
xform = NA,
lxform = NA,
jitter.x = FALSE,
jitter.y = FALSE,
ylab = "response",
xlab = "concentration",
xlim = NA,
xticks = NA, ...) {
# check if y.type is binomial
y.type <- X$y.type
if (y.type == "binomial") {
y.dat <- X$mod.dat$y / X$mod.dat$trials
} else {
y.dat <- X$mod.dat$y
}
EC10 <- c(NA, NA, NA)
if (add.EC10 == TRUE & X$y.type != "gaussian") {
EC10 <- extract_ECx.jagsNECfit(X)
}
if (add.EC10 == TRUE & X$y.type == "gaussian") {
EC10 <- extract_ECx.jagsNECfit(X, type = "relative")
}
# check if a transformation is required for x
if (class(xform) == "function") {
x.dat <- xform(X$mod.dat$x)
NEC <- xform(X$NEC)
x.vec <- xform(X$pred.vals$x)
EC10 <- xform(EC10)
} else {
x.dat <- X$mod.dat$x
NEC <- X$NEC
x.vec <- X$pred.vals$x
}
if (jitter.x == TRUE) {
x.dat <- jitter(x.dat)
}
if (jitter.y == TRUE) {
y.dat <- jitter(y.dat)
}
# check if a range for the x axis has been specified
if (max(is.na(xlim)) == 1) {
x.lim <- range(x.dat)
} else {
x.lim <- xlim
}
# Check if x axis tick marks have been specified
if (max(is.na(xticks)) == 1) {
x.ticks <- seq(min(x.dat), max(x.dat), length = 7)
} else {
x.ticks <- xticks
}
plot(x.dat, y.dat,
ylab = ylab,
xlab = xlab,
pch = 16, xaxt = "n", xlim = x.lim,
col = adjustcolor(1, alpha = 0.25),
cex = 1.5
)
if (class(lxform) != "function") {
if (max(is.na(xticks)) == 1) {
axis(side = 1)
} else {
axis(side = 1, at = x.ticks)
}
NEC.legend <- paste("NEC: ", signif(NEC[2], 2),
" (", signif(NEC[1], 2), "-", signif(NEC[3], 2), ")",
sep = ""
)
EC10.legend <- paste("EC10: ", signif(EC10[1], 2),
" (", signif(EC10[2], 2), "-", signif(EC10[3], 2), ")",
sep = ""
)
} else {
x.labs <- signif(lxform(x.ticks), 2)
axis(side = 1, at = x.ticks, labels = x.labs)
NEC.legend <- paste("NEC: ", signif(lxform(NEC[2]), 2),
" (", signif(lxform(NEC[1]), 2), "-",
signif(lxform(NEC[3]), 2), ")",
sep = ""
)
EC10.legend <- paste("EC10: ", signif(lxform(EC10[1]), 2),
" (", signif(lxform(EC10[2]), 2), "-",
signif(lxform(EC10[3]), 2), ")",
sep = ""
)
}
if (CI == TRUE) {
lines(x.vec, X$pred.vals$up, lty = 2)
lines(x.vec, X$pred.vals$lw, lty = 2)
}
if (posterior.median == TRUE) {
lines(x.vec, X$pred.vals$y)
}
if (median.model == TRUE) {
lines(x.vec, X$pred.vals$y.m, col = "red")
}
if (add.NEC == TRUE & add.EC10 == FALSE) {
abline(v = NEC, col = "red", lty = c(3, 1, 3))
legend(legend.loc,
bty = "n",
legend = NEC.legend, lty = 1, col = "red"
)
}
if (add.EC10 == TRUE & add.NEC == FALSE) {
abline(v = EC10, col = "red", lty = c(1, 3, 3))
legend(legend.loc,
bty = "n",
legend = EC10.legend, lty = 1, col = "red"
)
}
if (add.EC10 == TRUE & add.NEC == TRUE) {
abline(v = NEC, col = "red", lty = c(3, 1, 3))
abline(v = EC10, col = "orange", lty = c(1, 3, 3))
legend(legend.loc,
bty = "n",
legend = c(NEC.legend, EC10.legend),
lty = 1, col = c("red", "orange")
)
}
}
#' plot.jagsMANEC
#'
#' Generates a plot of a fitted jagsMANEC model, as returned by fit.jagsMANEC.
#'
#' @inheritParams plot.jagsNECfit
#'
#' @param xtick.rounding the rounding to be applied for manually specifying xticks, when data are back transformed via lxform.
#'
#' @param all_models a logical, indicating if all models should be plotted individually, or if the model averaged plot should be returned.
#'
#' @export
#' @return a plot of the fitted model
plot.jagsMANECfit <- function(X,
CI = TRUE,
posterior.median = TRUE,
median.model = FALSE,
add.NEC = TRUE,
add.EC10 = FALSE,
legend.loc = "topright",
xform = NA, lxform = NA,
jitter.x = FALSE, jitter.y = FALSE,
ylab = "response",
xlab = "concentration",
xlim = NA,
ylim = NA,
xticks = NA,
xtick.rounding = 5,
all_models = FALSE, ...) {
if (all_models) {
mod_fits <- X$mod.fits
par(mfrow = c(ceiling(length(mod_fits) / 2), 2), mar = c(1.5, 1.5, 1.5, 1.5), oma = c(3, 3, 0, 0))
for (m in 1:length(mod_fits)) {
plot(
X = mod_fits[[m]],
CI = CI, add.NEC = add.NEC,
legend.loc = legend.loc,
add.EC10 = add.EC10,
xform = xform, lxform = lxform,
jitter.x = jitter.x, jitter.y = jitter.y,
ylab = "", xlab = "",
xlim = xlim,
xticks = xticks, ...
)
mtext(xlab, side = 1, outer = T, line = 2)
mtext(ylab, side = 2, outer = T, line = 2)
mtext(names(mod_fits)[m], side = 3, outer = FALSE, cex = 0.8)
}
} else {
# check if y.type is binomial
y.type <- X$y.type
if (y.type == "binomial") {
y.dat <- X$mod.dat$y / X$mod.dat$trials
} else {
y.dat <- X$mod.dat$y
}
## extract EC10
EC10 <- c(NA, NA, NA)
if (add.EC10 == TRUE & X$y.type != "gaussian") {
EC10 <- extract_ECx.jagsMANECfit(X)
}
if (add.EC10 == TRUE & X$y.type == "gaussian") {
EC10 <- extract_ECx.jagsMANECfit(X, type = "relative")
}
# check if a transformation is required for x
if (class(xform) == "function") {
x.dat <- xform(X$mod.dat$x)
NEC <- xform(X$NEC)
x.vec <- xform(X$pred.vals$x)
EC10 <- xform(EC10)
} else {
x.dat <- X$mod.dat$x
NEC <- X$NEC
x.vec <- X$pred.vals$x
}
if (jitter.x == TRUE) {
x.dat <- jitter(x.dat)
}
if (jitter.y == TRUE) {
y.dat <- jitter(y.dat)
}
# check if a range for the x axis has been specified
if (max(is.na(xlim)) == 1) {
x.lim <- range(x.dat)
} else {
x.lim <- xlim
}
# Check if x axis tick marks have been specified
if (max(is.na(xticks)) == 1) {
x.ticks <- seq(min(x.dat), max(x.dat), length = 7)
} else {
x.ticks <- xticks
}
if (is.na(xlim[1]) == TRUE) {
xlim <- range(x.dat)
}
if (is.na(ylim[1]) == TRUE) {
ylim <- range(y.dat)
}
plot(x.dat, y.dat,
ylab = ylab,
xlab = xlab,
pch = 16, xaxt = "n",
xlim = xlim, ylim = ylim,
col = adjustcolor(1, alpha = 0.25),
cex = 1.5
)
if (class(lxform) != "function") {
if (max(is.na(xticks)) == 1) {
axis(side = 1)
} else {
axis(side = 1, at = x.ticks)
}
NEC.legend <- paste("NEC: ", signif(NEC[2], 2),
" (", signif(NEC[1], 2), "-",
signif(NEC[3], 2), ")",
sep = ""
)
EC10.legend <- paste("EC10: ", signif(EC10[1], 2),
" (", signif(EC10[2], 2), "-",
signif(EC10[3], 2), ")",
sep = ""
)
} else {
x.labs <- signif(round(lxform(x.ticks), xtick.rounding), 2)
axis(side = 1, at = x.ticks, labels = x.labs)
NEC.legend <- paste("NEC: ", signif(lxform(NEC[2]), 2),
" (", signif(lxform(NEC[1]), 2), "-",
signif(lxform(NEC[3]), 2), ")",
sep = ""
)
EC10.legend <- paste("EC10: ", signif(lxform(EC10[1]), 2),
" (", signif(lxform(EC10[2]), 2), "-",
signif(lxform(EC10[3]), 2), ")",
sep = ""
)
}
if (CI == TRUE) {
lines(x.vec, X$pred.vals$up, lty = 2)
lines(x.vec, X$pred.vals$lw, lty = 2)
}
if (posterior.median == TRUE) {
lines(x.vec, X$pred.vals$y)
}
if (median.model == TRUE) {
lines(x.vec, X$pred.vals$y.m, col = "red")
}
if (add.NEC == TRUE & add.EC10 == FALSE) {
abline(v = NEC, col = "red", lty = c(3, 1, 3))
legend(legend.loc,
bty = "n",
legend = NEC.legend, lty = 1, col = "red"
)
}
if (add.EC10 == TRUE & add.NEC == FALSE) {
abline(v = EC10, col = "red", lty = c(1, 3, 3))
legend(legend.loc,
bty = "n",
legend = EC10.legend, lty = 1, col = "red"
)
}
if (add.EC10 == TRUE & add.NEC == TRUE) {
abline(v = NEC, col = "red", lty = c(3, 1, 3))
abline(v = EC10, col = "orange", lty = c(1, 3, 3))
legend(legend.loc,
bty = "n",
legend = c(NEC.legend, EC10.legend),
lty = 1, col = c("red", "orange")
)
}
}
}