-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc_trajsim.R
57 lines (42 loc) · 2.17 KB
/
func_trajsim.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
# Calculates quantiles from all sampled trajectories
trajsim_quantiles <- function(trajsim, var) {
quantiles <- plyr::ddply(.data=trajsim, .variables="time",
function(x) quantile(x[, var],
prob = c(0.025, 0.5, 0.975), na.rm=T))
colnames(quantiles)[2:4] <- c("low95CI", "median", "up95CI")
quantiles <- merge(unique(trajsim[,c("time", "date", "week", "month", "doy", "year", "npos")]),
quantiles, by="time", all=T)
return(quantiles)
}
# This function loads the trajecories simulated from the posteriors and produces
# all the datasets derived from it. It is done in a function to save memory
func_trajsim <- function(file, data, chainno=NULL) {
trajsim <- read_csv(paste0("output/", file, ".csv"))
#subset to specified chains
if (!is.null(chainno)) {
trajsim <- trajsim[trajsim$chain %in% chainno,]
}
trajsim <- merge(data[,c("time", "date", "week", "month", "year", "doy", "npos")],
trajsim, by="time", all=T)
colnames(trajsim) <- str_replace_all(colnames(trajsim), "¹", "\\_1")
colnames(trajsim) <- str_replace_all(colnames(trajsim), "²", "\\_2")
fit <- trajsim_quantiles(trajsim, "simobserror")
reporting <- trajsim_quantiles(trajsim, "rhopct")
beta <- trajsim %>%
filter(year==2011 & !is.na(date)) %>%
trajsim_quantiles(., "betaseason") %>%
dplyr::mutate(pct_bl_median = median*100/min(median)-100,
pct_bl_low95 = low95CI*100/min(low95CI)-100,
pct_bl_up95 = up95CI*100/min(up95CI)-100)
traj_year <- trajsim %>%
dplyr::filter(year==2011) %>%
select(c("replicate", "doy", matches("^[SEIR][0-3].*?"), matches("inc[0-9]"))) %>%
pivot_longer(cols=c(matches("^[SEIR][0-3].*?"), matches("inc[0-9]")), names_to = "state", values_to = "n") %>%
group_by(doy, replicate) %>%
dplyr::mutate(order=row_number())
out <- list("fit" = fit,
"reporting" = reporting,
"beta" = beta,
"traj_year"= traj_year)
return(out)
}