forked from cdcepi/Flusight-ensemble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create_FluSight_Ensemble.R
163 lines (133 loc) · 5.94 KB
/
Create_FluSight_Ensemble.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
#### Developed from:
#### https://github.com/reichlab/hubEnsembles/blob/main/vignettes/basic-ensemble.Rmd
#### Developed by: P. Prasad
# Packages to Install
# devtools::install_github("reichlab/zoltr")
# devtools::install_github("reichlab/covidData")
# devtools::install_github("reichlab/simplets")
# devtools::install_github("reichlab/covidHubUtils")
#devtools::install_github("reichlab/hubEnsembles")
library(covidHubUtils)
library(hubEnsembles)
library(dplyr)
# Set the environment - dates should change each week & check to see if the file paths are correct
userid = "rpe5"
forecast_date = "2022-10-24" # Monday
sixweeks_before_forecast_date = "2022-09-12" # 6 weeks ago Monday
ensemble_code_path = paste0("C:/Users/",userid,"/Desktop/GitHub/Flusight-ensemble")
flusight_path = paste0("C:/Users/",userid,"/Desktop/GitHub/Flusight-forecast-data") #using my forked repo for this now
setwd(flusight_path)
output_dir <- paste0(ensemble_code_path, "/", forecast_date, "/")
if(!dir.exists(output_dir)){
dir.create(path = output_dir)
}
# Get the models to be included in the ensemble
if(!file.exists(paste0(output_dir, "models-to-include-in-ensemble-", forecast_date, ".csv"))){
file_names = list.files(path = paste0(flusight_path, "/data-forecasts"))
all_models = file_names[!(file_names %in% c("Flusight-baseline", "Flusight-ensemble")) &
!grepl(paste0(".md", collapse = "|"), file_names)]
all_metadata = paste0(flusight_path, "/data-forecasts/", all_models,
"/metadata-", all_models, ".txt") %>%
lapply(., read.delim)
include <- c()
for(i in 1:length(all_models)){
metadata = all_metadata[[i]]
# this checks to see that this week's file is in the model directory and
# that it is a designated primary, secondary, or proposed model in the metadata
if(file.exists(paste0(flusight_path, "/data-forecasts/", all_models[i],
"/", forecast_date, "-", all_models[i], ".csv")) &
(colSums("team_model_designation: primary" == metadata) +
colSums("team_model_designation: proposed" == metadata) +
colSums("team_model_designation: secondary" == metadata) > 0)){
include = c(include, all_models[i])
}
}
write.csv(data.frame(model = include),paste0(output_dir, "models-to-include-in-ensemble-", forecast_date, ".csv"))
}
# We start by loading the forecasts of weekly incident hospitalizations from selected models
eligible_models = read.csv(paste0(output_dir, "models-to-include-in-ensemble-", forecast_date, ".csv"),
header = TRUE)
models =as.character(eligible_models$model)
#Read in forecast data
forecast_data <- load_forecasts_repo(
file_path = paste0(flusight_path, "/data-forecasts/"),
models = models,
targets = c(paste(1:4, "wk ahead inc flu hosp")),
forecast_dates = forecast_date,
hub = "FluSight",
types = "quantile")%>%
rename(full_location_name = location_name) %>%
mutate(full_location_name = case_when(location == "US" ~ "United States",
location != "US" ~ full_location_name))
#Read in truth data
truth_data <- load_truth(
truth_source = "HealthData",
target_variable = "inc flu hosp",
locations = unique(forecast_data$location),
hub = "FluSight"
) %>%
rename(full_location_name = location_name)
#Plot individual team forecasts
all_locations = sort(unique(forecast_data$location))
starting_location = seq(1, length(all_locations), 3)
pdf(paste0(output_dir, "all-models-", forecast_date, ".pdf"))
for(i in starting_location){
plot_forecasts(
forecast_data = forecast_data %>% filter(location %in% all_locations[i:(i+2)]),
facet = .~location,
facet_scales = "free_y",
facet_ncol = 1,
facet_nrow = 3,
truth_data = truth_data %>% filter(target_end_date > sixweeks_before_forecast_date),
truth_source = "HealthData",
use_median_as_point = TRUE,
fill_by_model = TRUE,
title = "Weekly Influenza Incident Hospitalizations: observed and forecasted",
show_caption = FALSE,
fill_transparency = .3
)
}
dev.off()
pdf(paste0(output_dir, "all-models-full-page-", forecast_date, ".pdf"))
for(i in starting_location){
plot_forecasts(
forecast_data = forecast_data %>% filter(location %in% all_locations[i]),
facet = .~location,
facet_scales = "free_y",
facet_ncol = 1,
facet_nrow = 1,
truth_data = truth_data %>% filter(target_end_date > sixweeks_before_forecast_date),
truth_source = "HealthData",
use_median_as_point = TRUE,
fill_by_model = TRUE,
title = "Weekly Influenza Incident Hospitalizations: observed and forecasted",
show_caption = FALSE,
fill_transparency = .3
)
}
dev.off()
# Build, save, and plot the ensemble
ensemble_forecast <- build_quantile_ensemble(forecast_data,
method = "median",
forecast_date = forecast_date,
model_name = "Flusight-ensemble",
location_data = hub_locations)
ensemble_forecast1 <- ensemble_forecast %>%
mutate(target = paste(horizon, temporal_resolution, "ahead", target_variable, sep = " ")) %>%
dplyr::select(forecast_date, target, target_end_date, location, type, quantile, value)
write.csv(ensemble_forecast1, paste0(flusight_path, "/data-forecasts/Flusight-ensemble/",forecast_date, "-Flusight-ensemble.csv"), row.names=FALSE)
pdf(paste0(output_dir, "ensemble-", forecast_date, ".pdf"))
for(i in starting_location){
plot_forecasts(
forecast_data = ensemble_forecast %>% filter(location %in% all_locations[i:(i+2)]),
facet = .~location,
facet_scales = "free_y",
facet_ncol = 1,
facet_nrow = 3,
truth_data = truth_data %>% filter(target_end_date > sixweeks_before_forecast_date),
truth_source = "HealthData",
title = "Weekly Influenza Incident Hospitalizations: observed and forecasted",
use_median_as_point = TRUE
)
}
dev.off()