-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotPosteriorMeanRate.R
278 lines (250 loc) · 11.4 KB
/
PlotPosteriorMeanRate.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
#' Plot Posterior Mean Rate of Sample Occurrence for Poisson Process Model
#'
#' @description
#' Given output from the Poisson process fitting function [carbondate::PPcalibrate] calculate
#' and plot the posterior mean rate of sample occurrence (i.e., the underlying Poisson process
#' rate \eqn{\lambda(t)}) together with specified probability intervals, on a given calendar age grid
#' (provided in cal yr BP).
#'
#' Will show the original set of radiocarbon determinations (those you are modelling/summarising),
#' the chosen calibration curve, and the estimated posterior rate of occurrence \eqn{\lambda(t)} on the same plot.
#' Can also optionally show the posterior mean of each individual sample's calendar age estimate.
#'
#' \strong{Note:} If all you are interested in is the value of the posterior mean rate
#' on a grid, without an accompanying plot, you can use
#' [carbondate::FindPosteriorMeanRate] instead.
#'
#' For more information read the vignette: \cr
#' \code{vignette("Poisson-process-modelling", package = "carbondate")}
#'
#' @param output_data The return value from the updating function
#' [carbondate::PPcalibrate]. Optionally, the output data can have an extra list item
#' named `label` which is used to set the label on the plot legend.
#' @param n_posterior_samples Number of samples it will draw, after having removed `n_burn`,
#' from the (thinned) MCMC realisations stored in `output_data` to estimate the
#' rate \eqn{\lambda(t)}. These samples may be repeats if the number of, post burn-in,
#' realisations is less than `n_posterior_samples`. If not given, 5000 is used.
#' @param calibration_curve This is usually not required since the name of the
#' calibration curve variable is saved in the output data. However, if the
#' variable with this name is no longer in your environment then you should pass
#' the calibration curve here. If provided, this should be a dataframe which
#' should contain at least 3 columns entitled `calendar_age`, `c14_age` and `c14_sig`.
#' This format matches [carbondate::intcal20].
#' @param plot_14C_age Whether to use the radiocarbon age (\eqn{{}^{14}}C yr BP) as
#' the units of the y-axis in the plot. Defaults to `TRUE`. If `FALSE` uses
#' F\eqn{{}^{14}}C concentration instead.
#' @param plot_cal_age_scale (Optional) The calendar scale to use for the x-axis. Allowed values are
#' "BP", "AD" and "BC". The default is "BP" corresponding to plotting in cal yr BP.
#' @param show_individual_means (Optional) Whether to calculate and show the mean posterior
#' calendar age estimated for each individual \eqn{{}^{14}}C sample on the plot as a rug on
#' the x-axis. Default is `TRUE`.
#' @param show_confidence_intervals Whether to show the pointwise confidence intervals
#' (at chosen probability level) on the plot. Default is `TRUE`.
#' @param interval_width The confidence intervals to show for both the
#' calibration curve and the predictive density. Choose from one of `"1sigma"` (68.3%),
#' `"2sigma"` (95.4%) and `"bespoke"`. Default is `"2sigma"`.
#' @param bespoke_probability The probability to use for the confidence interval
#' if `"bespoke"` is chosen above. E.g., if 0.95 is chosen, then the 95% confidence
#' interval is calculated. Ignored if `"bespoke"` is not chosen.
#' @param denscale (Optional) Whether to scale the vertical range of the Poisson process mean rate plot
#' relative to the calibration curve plot. Default is 3 which means
#' that the maximum of the mean rate will be at 1/3 of the height of the plot.
#' @param resolution The distance between calendar ages at which to calculate the value of the rate
#' \eqn{\lambda(t)}. These ages will be created on a regular grid that automatically covers
#' the calendar period specified in `output_data`. Default is 1.
#' @param n_burn The number of MCMC iterations that should be discarded as burn-in (i.e.,
#' considered to be occurring before the MCMC has converged). This relates to the number
#' of iterations (`n_iter`) when running the original update functions (not the thinned `output_data`).
#' Any MCMC iterations before this are not used in the calculations. If not given, the first half of the
#' MCMC chain is discarded. Note: The maximum value that the function
#' will allow is `n_iter - 100 * n_thin` (where `n_iter` and `n_thin` are the arguments that were given to
#' [carbondate::PPcalibrate]) which would leave only 100 of the (thinned) values in `output_data`.
#' @param n_end The last iteration in the original MCMC chain to use in the calculations. Assumed to be the
#' total number of iterations performed, i.e. `n_iter`, if not given.
#' @param plot_pretty logical, defaulting to `TRUE`. If set `TRUE` then will select pretty plotting
#' margins (that create sufficient space for axis titles and rotates y-axis labels). If `FALSE` will
#' implement current user values.
#'
#'
#' @return A list, each item containing a data frame of the `calendar_age_BP`, the `rate_mean`
#' and the confidence intervals for the rate - `rate_ci_lower` and `rate_ci_upper`.
#'
#' @export
#'
#' @examples
#' # NOTE: All these examples are shown with a small n_iter and n_posterior_samples
#' # to speed up execution.
#' # Try n_iter and n_posterior_samples as the function defaults.
#'
#' pp_output <- PPcalibrate(
#' pp_uniform_phase$c14_age,
#' pp_uniform_phase$c14_sig,
#' intcal20,
#' n_iter = 1000,
#' show_progress = FALSE)
#'
#' # Default plot with 2 sigma interval
#' PlotPosteriorMeanRate(pp_output, n_posterior_samples = 100)
#'
#' # Specify an 80% confidence interval
#' PlotPosteriorMeanRate(
#' pp_output,
#' interval_width = "bespoke",
#' bespoke_probability = 0.8,
#' n_posterior_samples = 100)
PlotPosteriorMeanRate <- function(
output_data,
n_posterior_samples = 5000,
calibration_curve = NULL,
plot_14C_age = TRUE,
plot_cal_age_scale = "BP",
show_individual_means = TRUE,
show_confidence_intervals = TRUE,
interval_width = "2sigma",
bespoke_probability = NA,
denscale = 3,
resolution = 1,
n_burn = NA,
n_end = NA,
plot_pretty = TRUE) {
arg_check <- .InitializeErrorList()
.CheckOutputData(arg_check, output_data, "RJPP")
.CheckInteger(arg_check, n_posterior_samples, lower = 10)
.CheckCalibrationCurveFromOutput(arg_check, output_data, calibration_curve)
.CheckFlag(arg_check, plot_14C_age)
.CheckChoice(arg_check, plot_cal_age_scale, c("BP", "AD", "BC"))
.CheckFlag(arg_check, show_individual_means)
.CheckFlag(arg_check, show_confidence_intervals)
.CheckIntervalWidth(arg_check, interval_width, bespoke_probability)
.CheckNumber(arg_check, denscale, lower = 0)
.CheckNumber(arg_check, resolution, lower = 0.01)
.ReportErrors(arg_check)
# Ensure revert to main environment par on exit of function
oldpar <- graphics::par(no.readonly = TRUE)
on.exit(graphics::par(oldpar))
# Set nice plotting parameters
if(plot_pretty) {
graphics::par(
mgp = c(3, 0.7, 0),
xaxs = "i",
yaxs = "i",
mar = c(5, 4.5, 4, 2) + 0.1,
las = 1)
}
if (is.null(calibration_curve)) {
calibration_curve <- get(output_data$input_data$calibration_curve_name)
}
rc_determinations <- output_data$input_data$rc_determinations
rc_sigmas <- output_data$input_data$rc_sigmas
F14C_inputs <-output_data$input_data$F14C_inputs
if (plot_14C_age == TRUE) {
calibration_curve <- .AddC14ageColumns(calibration_curve)
if (F14C_inputs == TRUE) {
converted <- .ConvertF14cTo14Cage(rc_determinations, rc_sigmas)
rc_determinations <- converted$c14_age
}
} else {
calibration_curve <- .AddF14cColumns(calibration_curve)
if (F14C_inputs == FALSE) {
converted <- .Convert14CageToF14c(rc_determinations, rc_sigmas)
rc_determinations <- converted$f14c
}
}
##############################################################################
# Initialise plotting parameters
calibration_curve_colour <- "blue"
calibration_curve_bg <- grDevices::rgb(0, 0, 1, .3)
output_colour <- "purple"
start_age <- ceiling(min(output_data$rate_s[[1]]) / resolution) * resolution
end_age <- floor(max(output_data$rate_s[[1]]) / resolution) * resolution
if (end_age == max(output_data$rate_s[[1]])) {
# Removes issue of sequence coinciding with end changepoint
end_age <- end_age - resolution
}
calendar_age_sequence <- seq(from = start_age, to = end_age, by = resolution)
xlim <- rev(range(calendar_age_sequence))
##############################################################################
# Calculate means and rate
posterior_rate <- FindPosteriorMeanRate(
output_data,
calendar_age_sequence,
n_posterior_samples,
interval_width,
bespoke_probability,
n_burn,
n_end)
if (show_individual_means){
n_iter <- output_data$input_parameters$n_iter
n_thin <- output_data$input_parameters$n_thin
n_burn <- .SetNBurn(n_burn, n_iter, n_thin)
n_end <- .SetNEnd(n_end, n_iter, n_thin)
calendar_age_means <- apply(output_data$calendar_ages[(n_burn + 1):n_end, ], 2, mean)
}
ylim_rate <- c(0, denscale * max(posterior_rate$rate_mean))
##############################################################################
# Plot curves
.PlotCalibrationCurveAndInputData(
plot_cal_age_scale,
xlim,
calibration_curve,
rc_determinations,
plot_14C_age,
calibration_curve_colour,
calibration_curve_bg,
interval_width,
bespoke_probability,
title = expression(paste("Estimate of Poisson process rate ", lambda, "(t)")))
.SetUpDensityPlot(plot_cal_age_scale, xlim, ylim_rate)
if (show_individual_means) {
calendar_age_means <- .ConvertCalendarAge(plot_cal_age_scale, calendar_age_means)
graphics::rug(calendar_age_means, side = 1, quiet = TRUE)
}
.PlotRateEstimateOnCurrentPlot(plot_cal_age_scale, posterior_rate, output_colour, show_confidence_intervals)
.AddLegendToRatePlot(
output_data,
show_confidence_intervals,
interval_width,
bespoke_probability,
calibration_curve_colour,
output_colour)
invisible(posterior_rate)
}
.PlotRateEstimateOnCurrentPlot <- function(
plot_cal_age_scale, posterior_rate, output_colour, show_confidence_intervals) {
cal_age <- .ConvertCalendarAge(plot_cal_age_scale, posterior_rate$calendar_age_BP)
graphics::lines(cal_age, posterior_rate$rate_mean, col = output_colour)
if (show_confidence_intervals) {
graphics::lines(cal_age, posterior_rate$rate_ci_lower, col = output_colour, lty = 2)
graphics::lines(cal_age, posterior_rate$rate_ci_upper, col = output_colour, lty = 2)
}
}
.AddLegendToRatePlot <- function(
output_data,
show_confidence_intervals,
interval_width,
bespoke_probability,
calibration_curve_colour,
output_colour) {
ci_label <- switch(
interval_width,
"1sigma" = expression(paste("1", sigma, " interval")),
"2sigma" = expression(paste("2", sigma, " interval")),
"bespoke" = paste0(round(100 * bespoke_probability), "% interval"))
legend_labels <- c(
gsub("intcal", "IntCal",
gsub("shcal", "SHCal",
output_data$input_data$calibration_curve_name)), # Both IntCal and SHCal
ci_label,
"Posterior mean rate")
lty <- c(1, 2, 1)
pch <- c(NA, NA, NA)
col <- c(calibration_curve_colour, calibration_curve_colour, output_colour)
if (show_confidence_intervals) {
legend_labels <- c(legend_labels, ci_label)
lty <- c(lty, 2)
pch <- c(pch, NA)
col <- c(col, output_colour)
}
graphics::legend(
"topright", legend = legend_labels, lty = lty, pch = pch, col = col)
}