-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoChron.Rmd
284 lines (232 loc) · 12.6 KB
/
GeoChron.Rmd
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
---
title: "GeoChron"
output: html_notebook
---
Assess variability of age or CPE estimates at multiple levels.
At the highest level is a region which is composed of $nL$ localities.
The mean age across a region is $e$ which generates means $c_l$ for each locality.
$$ c_l = e + \zeta_c, l = 1,\ldots, nL, \zeta_c \sim N(0, \sigma_c^2)$$
Each locality has a number $nR_l$ of constituent reefs and a mean $c_l$ which generates means for each reef.
$$r_{kl} = c_l + \zeta_r, k = 1,\ldots, nR_l, l = 1\ldots nL, \zeta_r \sim N(0,\sigma_r^2)$$
Each reef has a number $nH_{kl}$ of constituent holes (typically 3 for our data) and the reef mean generates observations for each hole.
$$ x_{jkl} = r_{kl} + \zeta_h, j = 1,\ldots, nH_{kl}, k = 1,\ldots, nR_l, l = 1,\ldots, nL, \zeta_h \sim N(0,\sigma_h^2)$$
Finally, for each hole there are actuall two depths and one would expect the deeper sample to be older than the sample above it. We model the offset $d$ and report its posterior distribution.
We are interested in comparing its variance $\sigma_d^2$ with the other sigmas.
$$ y_{jkl} = d + x_{jkl} $$
$$ y_{jkl}-x_{jkl} \sim N(\mu_d, \sigma_d^2) $$
```{r}
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(magrittr))
suppressPackageStartupMessages(library(lme4))
suppressPackageStartupMessages(library(cmdstanr))
suppressPackageStartupMessages(library(bayesplot))
```
Create data list from Geochronology "sampsum" data table created from "HOBS-geochronology-ms2.rmd". For a simulation illustrating how this model works, see "GeoChron_sim.Rmd."
```{r}
sampsum[, Loc := str_sub(sample, 1, 2), by = sample]
sampsum[, Locality := as.integer(factor(Loc, levels = unique(sampsum$Loc)))]
# sampsum[, reef2 := fcase(Locality %in% c(1, 2, 5:7, 9:11), reef,
# Locality == 3, ifelse(reef == 5, 4L, reef),
# Locality == 4, ifelse(reef == 4, 3L, reef),
# Locality == 8, as.integer(reef - 1)), by = c("Locality", "reef")]
sampsum[, reef3 := as.integer(factor(reefID, levels = unique(sampsum$reefID)))]
sampsum[, reef2Locality := Locality, by = c("Locality", "reef")]
sampsum[, holeID2 := holeID]
sampsum[, sampsperhole := length(holeID), by = holeID2]
sampsum_r2l <- distinct(sampsum[, .(reef,
locality,
reefID,
Locality,
reef3,
reef2Locality)])
sampsum_15to25cm <- distinct(sampsum[depth == "15-25", .(sample,
Sample_median_age,
median_age_bp,
Sample_corrected_posterior_age_estimate,
reef,
locality,
reefID,
holeID,
holeID2,
Locality,
reef3,
reef2Locality)])
sampsum_25to35cm <- distinct(sampsum[depth == "25-35", .(sample,
Sample_median_age,
median_age_bp,
Sample_corrected_posterior_age_estimate,
reef,
locality,
reefID,
holeID,
holeID2,
Locality,
reef3,
reef2Locality)])
depthdiffs <- sampsum[sampsperhole == 2, .(sample,
Sample_median_age,
median_age_bp,
Sample_corrected_posterior_age_estimate,
depth,
reef,
locality,
reefID,
holeID,
holeID2,
Locality,
reef3,
reef2Locality)]
depthdiffs[, `:=` (depthdiff_mabp = median_age_bp[depth == "25-35"] - median_age_bp[depth == "15-25"],
depthdiff_cpe = Sample_corrected_posterior_age_estimate[depth == "25-35"] - Sample_corrected_posterior_age_estimate[depth == "15-25"]), by = holeID2]
depthdiffs2 <- distinct(depthdiffs[, .(reef, locality, reefID, holeID, Locality, reef3, reef2Locality, depthdiff_mabp, depthdiff_cpe)])
locs <- distinct(sampsum_15to25cm[, .(locality, Locality)])
locs[, loclab := 12 - Locality]
setorder(locs, "Locality")
reefs <- distinct(sampsum_15to25cm[, .(reefID, reef3)])
reefs[, reeflab := 32 - reef3]
setorder(reefs, "reef3")
#median estimated sample ages and corresponding time-averaging estimates from Dominguez et al. (2016)
Detal <- data.table(median_age_bp = c(123, 283, 146, 148, 160, 2043),
Sample_corrected_posterior_age_estimate = c(68, 2670, 16, 27, 16, 1937),
Locality = c(1, 1, 1, 1, 1, 1),
Reef = c(1, 2, 3, 4, 5, 6),
Reef2Locality = c(1, 1, 1, 1, 1, 1),
nL = 1,
nR = 6)
#for median age over space (15-25cm burial depth)
HOBS_medagexspace_15to25cm <- list(N = nrow(sampsum_15to25cm),
Locality = sampsum_15to25cm$Locality,
Reef = sampsum_15to25cm$reef3,
Reef2Locality = sampsum_r2l$reef2Locality,
x = sampsum_15to25cm$median_age_bp,
nL = length(unique(sampsum_15to25cm$Locality)),
nR = length(unique(sampsum_15to25cm$reef3)))
#for CPE over space (15-25cm burial depth)
HOBS_cpexspace_15to25cm <- list(N = nrow(sampsum_15to25cm),
Locality = sampsum_15to25cm$Locality,
Reef = sampsum_15to25cm$reef3,
Reef2Locality = sampsum_r2l$reef2Locality,
x = sampsum_15to25cm$Sample_corrected_posterior_age_estimate,
nL = length(unique(sampsum_15to25cm$Locality)),
nR = length(unique(sampsum_15to25cm$reef3)))
#for median age over space (25-35cm burial depth)
HOBS_medagexspace_25to35cm <- list(N = nrow(sampsum_25to35cm),
Locality = sampsum_25to35cm$Locality,
Reef = sampsum_25to35cm$reef3,
Reef2Locality = sampsum_r2l$reef2Locality,
x = sampsum_25to35cm$median_age_bp,
nL = length(unique(sampsum_25to35cm$Locality)),
nR = length(unique(sampsum_25to35cm$reef3)))
#for CPE over space (25-35cm burial depth)
HOBS_cpexspace_25to35cm <- list(N = nrow(sampsum_25to35cm),
Locality = sampsum_25to35cm$Locality,
Reef = sampsum_25to35cm$reef3,
Reef2Locality = sampsum_r2l$reef2Locality,
x = sampsum_25to35cm$Sample_corrected_posterior_age_estimate,
nL = length(unique(sampsum_25to35cm$Locality)),
nR = length(unique(sampsum_25to35cm$reef3)))
#for median age depth differences over space
HOBS_medageddxspace <- list(N = nrow(depthdiffs2),
Locality = depthdiffs2$Locality,
Reef = depthdiffs2$reef3,
Reef2Locality = sampsum_r2l$reef2Locality,
x = depthdiffs2$depthdiff_mabp,
nL = length(unique(depthdiffs2$Locality)),
nR = length(unique(depthdiffs2$reef3)))
#for CPE depth differences over space
HOBS_cpeddxspace <- list(N = nrow(depthdiffs2),
Locality = depthdiffs2$Locality,
Reef = depthdiffs2$reef3,
Reef2Locality = sampsum_r2l$reef2Locality,
x = depthdiffs2$depthdiff_cpe,
nL = length(unique(depthdiffs2$Locality)),
nR = length(unique(depthdiffs2$reef3)))
#for median age depth differences over space - Dominguez et al. (2016) data
Detal_medagexspace <- list(N = 6,
Locality = Detal$Locality,
Reef = Detal$Reef,
Reef2Locality = Detal$Reef2Locality,
x = Detal$median_age_bp,
nL = Detal$nL,
nR = Detal$nR)
#for CPE depth differences over space - Dominguez et al. (2016) data
Detal_cpexspace <- list(N = 6,
Locality = Detal$Locality,
Reef = Detal$Reef,
Reef2Locality = Detal$Reef2Locality,
x = Detal$Sample_corrected_posterior_age_estimate,
nL = Detal$nL,
nR = Detal$nR)
```
```{r}
#For HOBS data
model_HOBS <- cmdstan_model("GeoChron.stan")
#For Dominguez et al. (2016) data
model_Detal <- cmdstan_model("GeoChron_Detal.stan")
```
```{r}
datalists <- c(as.name("HOBS_medagexspace_15to25cm"),
as.name("HOBS_cpexspace_15to25cm"),
as.name("HOBS_medagexspace_25to35cm"),
as.name("HOBS_cpexspace_25to35cm"),
as.name("HOBS_medageddxspace"),
as.name("HOBS_cpeddxspace"),
as.name("Detal_medagexspace"),
as.name("Detal_cpexspace"))
for(Data in datalists){
if(str_detect(Data, "HOBS")){
model <- cmdstan_model("GeoChron.stan")
} else{
model <- cmdstan_model("GeoChron_Detal.stan")
}
fit = model$sample(data = eval(Data),
seed = 1234,
chains = 8,
parallel_chains = 8,
refresh = 1000,
iter_warmup = 3000, #3000,
iter_sampling = 15000,
thin = 3,
adapt_delta = 0.99,
max_treedepth = 15)
fit$save_output_files("StanOutput")
fit$cmdstan_diagnose()
dim(fit$draws()) # accessing draws seems to fill the structure with draws
saveRDS(fit, file = here::here(paste0("StanOutput/", Data, ".rds")))
plot1 <- bayesplot::mcmc_intervals(fit$draws("mu_locality"))
plot1 <- plot1 +
geom_text(data = locs, aes(x = min(plot1$data$ll) - 3, y = loclab, label = locality), inherit.aes = FALSE) +
xlim(min(plot1$data$ll) - 5, max(plot1$data$hh))
saveRDS(plot1, here::here(paste0("StanOutput/", Data, "locintplot.rds")))
ggsave(filename = here::here(paste0("StanOutput/", Data, "locintplot.pdf")),
plot = plot1,
width = 6,
height = 6,
units = "in",
dpi = 400)
plot2 <- bayesplot::mcmc_intervals(fit$draws("mu_reef"))
plot2 <- plot2 +
geom_text(data = reefs, aes(x = min(plot2$data$ll) - 10, y = reeflab, label = reefID), inherit.aes = FALSE) +
xlim(min(plot2$data$ll) - 12, max(plot2$data$hh))
saveRDS(plot2, here::here(paste0("StanOutput/", Data, "reefintplot.rds")))
ggsave(filename = here::here(paste0("StanOutput/", Data, "reefintplot.pdf")),
plot = plot2,
width = 6,
height = 6,
units = "in",
dpi = 400)
}
```
```{r}
bayesplot::mcmc_dens(fit$draws("mu_locality"), facet_args = list(scales = "fixed", ncol = 3))
plot1 <- bayesplot::mcmc_intervals(fit$draws("mu_locality"))
plot1 +
geom_text(data = locs, aes(x = min(plot1$data$ll) - 3, y = loclab, label = locality), inherit.aes = FALSE) +
xlim(min(plot1$data$ll) - 5, max(plot1$data$hh))
```
```{r}
plot2 <- bayesplot::mcmc_intervals(fit$draws("mu_reef"))
plot2 +
geom_text(data = reefs, aes(x = min(plot2$data$ll) - 8, y = reeflab, label = reefID), inherit.aes = FALSE) +
xlim(min(plot2$data$ll) - 10, max(plot2$data$hh))
```