Repository navigation
Expand file tree
/
Copy pathpredict.R
More file actions
101 lines (90 loc) · 3.38 KB
/
Copy pathpredict.R
File metadata and controls
101 lines (90 loc) · 3.38 KB
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
# Cases = number of cases | E = population | week/month period component
# ID_year = year | ID_spat = location
library(yaml)
library(jsonlite)
library(INLA)
library(dplyr)
source("lib.R")
predict_chap <- function(model_fn, hist_fn, future_fn, preds_fn, config_fn = "") {
if (config_fn != "") {
config <- parse_model_configuration(config_fn)
covariate_names <- config$additional_continuous_covariates
user_options <- config$user_option_values
} else {
covariate_names <- c("rainfall", "mean_temperature")
user_options <- list()
}
precision <- user_options$precision %||% 0.01
region_seasonal <- user_options$region_seasonal %||% FALSE
location_specific_effects <- user_options$location_specific_effects %||% FALSE
nonlinearity <- get_nonlinearity_backend(
user_options$nonlinearity %||% "rw1_inla_group"
)
historic_df <- read.csv(hist_fn)
future_df <- read.csv(future_fn)
future_df$Cases <- rep(NA, nrow(future_df))
future_df$disease_cases <- rep(NA, nrow(future_df))
df <- rbind(historic_df, future_df)
df$Cases <- round(df$Cases)
if ("week" %in% colnames(df)) {
df <- mutate(df, ID_time_cyclic = week)
df <- offset_years_and_weeks(df)
} else {
df <- mutate(df, ID_time_cyclic = month)
df <- offset_years_and_months(df)
}
df$ID_time_cyclic2 <- df$ID_time_cyclic
df$ID_year <- df$ID_year - min(df$ID_year) + 1
if (length(covariate_names) == 0) {
generated <- generate_basic_model(df, region_seasonal)
} else {
lag_map <- resolve_lags(historic_df, covariate_names, user_options,
lags_path = lags_companion_path(model_fn))
generated <- generate_lagged_model(
df, covariate_names, lag_map, region_seasonal,
location_specific_effects = location_specific_effects,
nonlinearity = nonlinearity
)
}
formula_used <- generated$formula
df <- generated$data
df$ID_spat <- as.integer(as.factor(df$ID_spat))
model <- inla(
formula = formula_used, data = df, family = "nbinomial", offset = log(E),
control.inla = list(strategy = "adaptive"),
control.compute = list(dic = TRUE, config = TRUE, cpo = TRUE, return.marginals = FALSE),
control.fixed = list(correlation.matrix = TRUE, prec.intercept = 1e-4, prec = precision),
control.predictor = list(link = 1, compute = TRUE),
verbose = FALSE, safe = FALSE
)
casestopred <- df$Cases
idx.pred <- which(is.na(casestopred))
mpred <- length(idx.pred)
s <- 1000
y.pred <- matrix(NA, mpred, s)
xx <- inla.posterior.sample(s, model)
xx.s <- inla.posterior.sample.eval(
function(idx.pred) c(theta[1], Predictor[idx.pred]), xx, idx.pred = idx.pred
)
for (s.idx in seq_len(s)) {
xx.sample <- xx.s[, s.idx]
y.pred[, s.idx] <- rnbinom(mpred, mu = exp(xx.sample[-1]), size = xx.sample[1])
}
new.df <- data.frame(
time_period = df$time_period[idx.pred],
location = df$location[idx.pred],
y.pred
)
colnames(new.df) <- c("time_period", "location", paste0("sample_", 0:(s - 1)))
write.csv(new.df, preds_fn, row.names = FALSE)
saveRDS(model, file = model_fn)
}
args <- commandArgs(trailingOnly = TRUE)
if (length(args) >= 1) {
model_fn <- args[1]
hist_fn <- args[2]
future_fn <- args[3]
preds_fn <- args[4]
config_fn <- if (length(args) == 5) args[5] else ""
predict_chap(model_fn, hist_fn, future_fn, preds_fn, config_fn)
}