-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPosteriors.R
81 lines (64 loc) · 2.35 KB
/
getPosteriors.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
### Script collates mcmc chains for simulation study with burnin
# function to apply burnin
burnin <- function(df) {
df <- df[-c(1:500), ]
}
path <- getwd()
logPath <- paste0(path, "/log/")
files <- dir(path = logPath, pattern = ".+.log")
log <- lapply(paste0(logPath, files), function(x) read.table(head = TRUE, x))
backup <- log
names(log) <- gsub(files, pattern = "[.]log", replacement = "")
# apply burnin
for (i in 1:seq_along(log)){
log[[i]] <- burnin(log[[i]])
print(i)
}
# sanity check final ESS values
ess <- vector()
library(coda)
for (i in 1:length(log)){
colNames <- colnames(as.mcmc(log[[i]]))
ess <- c(ess, effectiveSize(as.mcmc(log[[i]])[, which(grepl("reproductiveNumber", colNames))]))
}
names(ess) <- names(log)
# all(ess > 200) == TRUE # nolint
# constructing a list with logs grouped in 4 for each condition
chains <- list()
tree <- vector()
rate <- vector()
sampProp <- vector()
# looping through reps
for (t in 1:100){
# loopng through occurrence proportions
for (p in c(0.05, 0.5, 1)){
# looping through rates
for (r in c("1e-05", "1e-03")){
# seq data p= (equiv. full data or amount reduced)
name <- paste0("fullData", "t", t, "p", p, "r", r)
fullData <- log[[name]][, which(grepl("reproductiveNumber", names(log[[name]])))]
# seq data p=1 (equiv. occurrences)
name <- paste0("dateData", "t", t,"p", p, "r", r)
dateData <- log[[name]][, which(grepl("reproductiveNumber", names(log[[name]])))]
# date date p=0 (equiv. date estiamtion or amount reduced)
name <- paste0("seqData", "t", t,"p", p, "r", r)
seqData <- log[[name]][, which(grepl("reproductiveNumber", names(log[[name]])))]
# date date p=1 (equiv. true prior)
name <- paste0("noData", "t", t, "p", p, "r", r)
noData <- log[[name]][, which(grepl("reproductiveNumber", names(log[[name]])))]
# equalise lengths and group posteriors
maxLen <- max(length(fullData), length(dateData), length(seqData), length(noData))
length(fullData) <- maxLen
length(dateData) <- maxLen
length(seqData) <- maxLen
length(noData) <- maxLen
elementName <- paste0("t", t, "p", p, "r", r)
chains[[elementName]] <- as.data.frame(cbind(fullData, dateData, seqData, noData))
# recording simulation conditions
sampProp <- c(sampProp, p)
tree <- c(tree, t)
rate <- c(rate, r)
}
}
}
save(chains, sampProp, rate, tree, ess, file = "postR0.RData")