Skip to content

Commit

Permalink
updates to imperial files closes #26.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickreich committed Apr 15, 2020
1 parent 8aa7cfe commit b07aa50
Show file tree
Hide file tree
Showing 11 changed files with 1,387 additions and 5 deletions.
38 changes: 33 additions & 5 deletions code/process_imperial_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ source("./code/get_next_saturday.R")
process_imperial_file <- function(sample_mat, location, timezero, qntls=c(0.01, 0.025, seq(0.05, 0.95, by=0.05), 0.975, 0.99)) {
require(tidyverse)

obs_data <- read_csv("data-processed/truth-cum-death.csv")

## create tables corresponding to the days for each of the targets
day_aheads <- tibble(
target = paste(1:7, "day ahead inc death"),
Expand All @@ -27,28 +25,50 @@ process_imperial_file <- function(sample_mat, location, timezero, qntls=c(0.01,
target = "1 wk ahead inc death",
target_cum = "1 wk ahead cum death",
dates = get_next_saturday(timezero))


## make cumulative death counts
obs_data <- read_csv("data-processed/truth-cum-death.csv") %>%
mutate(date = as.Date(date, "%m/%d/%y"))
last_obs_date <- as.Date(colnames(sample_mat)[1])-1
last_obs_death <- obs_data$value[which(obs_data$location=="US" & obs_data$date==last_obs_date)]
sample_mat_cum <- matrixStats::rowCumsums(as.matrix(sample_mat)) + last_obs_death

## indices and samples for incident deaths
which_days <- which(colnames(sample_mat) %in% as.character(day_aheads$dates))
which_weeks <- which(colnames(sample_mat) %in% as.character(week_aheads$dates))
samples_daily <- sample_mat[,which_days]
samples_weekly <- sample_mat[,which_weeks]
samples_daily_cum <- sample_mat_cum[,which_days]
samples_weekly_cum <- sample_mat_cum[,which_weeks]

## choosing quantile type=1 b/c more compatible with discrete samples
## other choices gave decimal answers
qntl_daily <- apply(samples_daily, FUN=function(x) quantile(x, qntls, type=1), MAR=2)
colnames(qntl_daily) <- day_aheads$target[which(day_aheads$dates %in% as.Date(colnames(qntl_daily)))]
colnames(qntl_daily) <- day_aheads$target[which(day_aheads$dates %in% as.Date(colnames(samples_daily)))]
qntl_daily_long <- as_tibble(qntl_daily) %>%
mutate(location=location, quantile = qntls, type="quantile") %>%
pivot_longer(cols=contains("day ahead"), names_to = "target")

## daily cumulative quantiles
qntl_daily_cum <- apply(samples_daily_cum, FUN=function(x) quantile(x, qntls, type=1), MAR=2)
colnames(qntl_daily_cum) <- day_aheads$target_cum[which(day_aheads$dates %in% as.Date(colnames(samples_daily)))]
qntl_daily_cum_long <- as_tibble(qntl_daily_cum) %>%
mutate(location=location, quantile = qntls, type="quantile") %>%
pivot_longer(cols=contains("day ahead"), names_to = "target")

if(is.null(dim(samples_weekly))){
## if only one week
qntl_weekly <- enframe(quantile(samples_weekly, qntls, type=1)) %>% select(value)
colnames(qntl_weekly) <- "1 wk ahead inc death"
qntl_weekly_long <- qntl_weekly %>%
mutate(location=location, quantile = qntls, type="quantile") %>%
pivot_longer(cols=contains("wk ahead"), names_to = "target")

qntl_weekly_cum <- enframe(quantile(samples_weekly_cum, qntls, type=1)) %>% select(value)
colnames(qntl_weekly_cum) <- "1 wk ahead cum death"
qntl_weekly_cum_long <- qntl_weekly_cum %>%
mutate(location=location, quantile = qntls, type="quantile") %>%
pivot_longer(cols=contains("wk ahead"), names_to = "target")
} else {
## if there are more than 1 weeks
qntl_weekly <- apply(samples_weekly, FUN=function(x) quantile(x, qntls, type=1), MAR=2)
Expand All @@ -57,9 +77,17 @@ process_imperial_file <- function(sample_mat, location, timezero, qntls=c(0.01,
mutate(location=location, quantile = qntls, type="quantile") %>%
pivot_longer(cols=contains("wk ahead"), names_to = "target")

qntl_weekly_cum <- apply(samples_weekly_cum, FUN=function(x) quantile(x, qntls, type=1), MAR=2)
colnames(qntl_weekly_cum) <- week_aheads$target[which(week_aheads$dates %in% as.Date(colnames(samples_weekly)))]
qntl_weekly_cum_long <- as_tibble(qntl_weekly_cum) %>%
mutate(location=location, quantile = qntls, type="quantile") %>%
pivot_longer(cols=contains("wk ahead"), names_to = "target")
}

qntl_dat_long <- bind_rows(qntl_daily_long, qntl_weekly_long)
qntl_dat_long <- bind_rows(
qntl_daily_long, qntl_weekly_long,
qntl_daily_cum_long, qntl_weekly_cum_long
)

point_ests <- qntl_dat_long %>%
filter(quantile==0.5) %>%
Expand Down
168 changes: 168 additions & 0 deletions data-processed/Imperial-ensemble1/2020-03-16-Imperial-ensemble1.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
target,location,type,quantile,value
1 day ahead cum death,US,point,NA,88
1 day ahead inc death,US,point,NA,16
1 wk ahead cum death,US,point,NA,203
1 wk ahead inc death,US,point,NA,39
2 day ahead cum death,US,point,NA,108
2 day ahead inc death,US,point,NA,19
3 day ahead cum death,US,point,NA,133
3 day ahead inc death,US,point,NA,24
4 day ahead cum death,US,point,NA,164
4 day ahead inc death,US,point,NA,31
5 day ahead cum death,US,point,NA,203
5 day ahead inc death,US,point,NA,39
6 day ahead cum death,US,point,NA,252
6 day ahead inc death,US,point,NA,49
1 day ahead cum death,US,quantile,0.01,64
1 day ahead cum death,US,quantile,0.025,67
1 day ahead cum death,US,quantile,0.05,69
1 day ahead cum death,US,quantile,0.1,72
1 day ahead cum death,US,quantile,0.15,75
1 day ahead cum death,US,quantile,0.2,77
1 day ahead cum death,US,quantile,0.25,79
1 day ahead cum death,US,quantile,0.3,81
1 day ahead cum death,US,quantile,0.35,83
1 day ahead cum death,US,quantile,0.4,85
1 day ahead cum death,US,quantile,0.45,87
1 day ahead cum death,US,quantile,0.5,88
1 day ahead cum death,US,quantile,0.55,90
1 day ahead cum death,US,quantile,0.6,92
1 day ahead cum death,US,quantile,0.65,93
1 day ahead cum death,US,quantile,0.7,95
1 day ahead cum death,US,quantile,0.75,97
1 day ahead cum death,US,quantile,0.8,100
1 day ahead cum death,US,quantile,0.85,103
1 day ahead cum death,US,quantile,0.9,107
1 day ahead cum death,US,quantile,0.95,113
1 day ahead cum death,US,quantile,0.975,120
1 day ahead cum death,US,quantile,0.99,132
1 day ahead inc death,US,quantile,0.01,2
1 day ahead inc death,US,quantile,0.025,3
1 day ahead inc death,US,quantile,0.05,4
Expand All @@ -29,6 +59,29 @@ target,location,type,quantile,value
1 day ahead inc death,US,quantile,0.95,31
1 day ahead inc death,US,quantile,0.975,35
1 day ahead inc death,US,quantile,0.99,42
1 wk ahead cum death,US,quantile,0.01,77
1 wk ahead cum death,US,quantile,0.025,85
1 wk ahead cum death,US,quantile,0.05,94
1 wk ahead cum death,US,quantile,0.1,109
1 wk ahead cum death,US,quantile,0.15,123
1 wk ahead cum death,US,quantile,0.2,137
1 wk ahead cum death,US,quantile,0.25,149
1 wk ahead cum death,US,quantile,0.3,160
1 wk ahead cum death,US,quantile,0.35,171
1 wk ahead cum death,US,quantile,0.4,182
1 wk ahead cum death,US,quantile,0.45,192
1 wk ahead cum death,US,quantile,0.5,203
1 wk ahead cum death,US,quantile,0.55,214
1 wk ahead cum death,US,quantile,0.6,225
1 wk ahead cum death,US,quantile,0.65,238
1 wk ahead cum death,US,quantile,0.7,252
1 wk ahead cum death,US,quantile,0.75,268
1 wk ahead cum death,US,quantile,0.8,286
1 wk ahead cum death,US,quantile,0.85,308
1 wk ahead cum death,US,quantile,0.9,340
1 wk ahead cum death,US,quantile,0.95,392
1 wk ahead cum death,US,quantile,0.975,446
1 wk ahead cum death,US,quantile,0.99,523
1 wk ahead inc death,US,quantile,0.01,3
1 wk ahead inc death,US,quantile,0.025,4
1 wk ahead inc death,US,quantile,0.05,6
Expand All @@ -52,6 +105,29 @@ target,location,type,quantile,value
1 wk ahead inc death,US,quantile,0.95,108
1 wk ahead inc death,US,quantile,0.975,129
1 wk ahead inc death,US,quantile,0.99,157
2 day ahead cum death,US,quantile,0.01,67
2 day ahead cum death,US,quantile,0.025,71
2 day ahead cum death,US,quantile,0.05,74
2 day ahead cum death,US,quantile,0.1,80
2 day ahead cum death,US,quantile,0.15,85
2 day ahead cum death,US,quantile,0.2,89
2 day ahead cum death,US,quantile,0.25,93
2 day ahead cum death,US,quantile,0.3,96
2 day ahead cum death,US,quantile,0.35,99
2 day ahead cum death,US,quantile,0.4,102
2 day ahead cum death,US,quantile,0.45,105
2 day ahead cum death,US,quantile,0.5,108
2 day ahead cum death,US,quantile,0.55,111
2 day ahead cum death,US,quantile,0.6,114
2 day ahead cum death,US,quantile,0.65,117
2 day ahead cum death,US,quantile,0.7,121
2 day ahead cum death,US,quantile,0.75,124
2 day ahead cum death,US,quantile,0.8,129
2 day ahead cum death,US,quantile,0.85,134
2 day ahead cum death,US,quantile,0.9,141
2 day ahead cum death,US,quantile,0.95,153
2 day ahead cum death,US,quantile,0.975,166
2 day ahead cum death,US,quantile,0.99,186
2 day ahead inc death,US,quantile,0.01,2
2 day ahead inc death,US,quantile,0.025,3
2 day ahead inc death,US,quantile,0.05,5
Expand All @@ -75,6 +151,29 @@ target,location,type,quantile,value
2 day ahead inc death,US,quantile,0.95,42
2 day ahead inc death,US,quantile,0.975,48
2 day ahead inc death,US,quantile,0.99,57
3 day ahead cum death,US,quantile,0.01,71
3 day ahead cum death,US,quantile,0.025,75
3 day ahead cum death,US,quantile,0.05,80
3 day ahead cum death,US,quantile,0.1,89
3 day ahead cum death,US,quantile,0.15,96
3 day ahead cum death,US,quantile,0.2,102
3 day ahead cum death,US,quantile,0.25,108
3 day ahead cum death,US,quantile,0.3,113
3 day ahead cum death,US,quantile,0.35,119
3 day ahead cum death,US,quantile,0.4,124
3 day ahead cum death,US,quantile,0.45,128
3 day ahead cum death,US,quantile,0.5,133
3 day ahead cum death,US,quantile,0.55,137
3 day ahead cum death,US,quantile,0.6,143
3 day ahead cum death,US,quantile,0.65,148
3 day ahead cum death,US,quantile,0.7,153
3 day ahead cum death,US,quantile,0.75,160
3 day ahead cum death,US,quantile,0.8,167
3 day ahead cum death,US,quantile,0.85,176
3 day ahead cum death,US,quantile,0.9,188
3 day ahead cum death,US,quantile,0.95,208
3 day ahead cum death,US,quantile,0.975,230
3 day ahead cum death,US,quantile,0.99,262
3 day ahead inc death,US,quantile,0.01,2
3 day ahead inc death,US,quantile,0.025,4
3 day ahead inc death,US,quantile,0.05,5
Expand All @@ -98,6 +197,29 @@ target,location,type,quantile,value
3 day ahead inc death,US,quantile,0.95,57
3 day ahead inc death,US,quantile,0.975,66
3 day ahead inc death,US,quantile,0.99,78
4 day ahead cum death,US,quantile,0.01,74
4 day ahead cum death,US,quantile,0.025,80
4 day ahead cum death,US,quantile,0.05,87
4 day ahead cum death,US,quantile,0.1,98
4 day ahead cum death,US,quantile,0.15,108
4 day ahead cum death,US,quantile,0.2,118
4 day ahead cum death,US,quantile,0.25,127
4 day ahead cum death,US,quantile,0.3,134
4 day ahead cum death,US,quantile,0.35,142
4 day ahead cum death,US,quantile,0.4,150
4 day ahead cum death,US,quantile,0.45,157
4 day ahead cum death,US,quantile,0.5,164
4 day ahead cum death,US,quantile,0.55,171
4 day ahead cum death,US,quantile,0.6,179
4 day ahead cum death,US,quantile,0.65,187
4 day ahead cum death,US,quantile,0.7,196
4 day ahead cum death,US,quantile,0.75,207
4 day ahead cum death,US,quantile,0.8,218
4 day ahead cum death,US,quantile,0.85,232
4 day ahead cum death,US,quantile,0.9,253
4 day ahead cum death,US,quantile,0.95,286
4 day ahead cum death,US,quantile,0.975,320
4 day ahead cum death,US,quantile,0.99,369
4 day ahead inc death,US,quantile,0.01,2
4 day ahead inc death,US,quantile,0.025,4
4 day ahead inc death,US,quantile,0.05,6
Expand All @@ -121,6 +243,29 @@ target,location,type,quantile,value
4 day ahead inc death,US,quantile,0.95,78
4 day ahead inc death,US,quantile,0.975,92
4 day ahead inc death,US,quantile,0.99,111
5 day ahead cum death,US,quantile,0.01,77
5 day ahead cum death,US,quantile,0.025,85
5 day ahead cum death,US,quantile,0.05,94
5 day ahead cum death,US,quantile,0.1,109
5 day ahead cum death,US,quantile,0.15,123
5 day ahead cum death,US,quantile,0.2,137
5 day ahead cum death,US,quantile,0.25,149
5 day ahead cum death,US,quantile,0.3,160
5 day ahead cum death,US,quantile,0.35,171
5 day ahead cum death,US,quantile,0.4,182
5 day ahead cum death,US,quantile,0.45,192
5 day ahead cum death,US,quantile,0.5,203
5 day ahead cum death,US,quantile,0.55,214
5 day ahead cum death,US,quantile,0.6,225
5 day ahead cum death,US,quantile,0.65,238
5 day ahead cum death,US,quantile,0.7,252
5 day ahead cum death,US,quantile,0.75,268
5 day ahead cum death,US,quantile,0.8,286
5 day ahead cum death,US,quantile,0.85,308
5 day ahead cum death,US,quantile,0.9,340
5 day ahead cum death,US,quantile,0.95,392
5 day ahead cum death,US,quantile,0.975,446
5 day ahead cum death,US,quantile,0.99,523
5 day ahead inc death,US,quantile,0.01,3
5 day ahead inc death,US,quantile,0.025,4
5 day ahead inc death,US,quantile,0.05,6
Expand All @@ -144,6 +289,29 @@ target,location,type,quantile,value
5 day ahead inc death,US,quantile,0.95,108
5 day ahead inc death,US,quantile,0.975,129
5 day ahead inc death,US,quantile,0.99,157
6 day ahead cum death,US,quantile,0.01,81
6 day ahead cum death,US,quantile,0.025,90
6 day ahead cum death,US,quantile,0.05,102
6 day ahead cum death,US,quantile,0.1,121
6 day ahead cum death,US,quantile,0.15,140
6 day ahead cum death,US,quantile,0.2,158
6 day ahead cum death,US,quantile,0.25,175
6 day ahead cum death,US,quantile,0.3,190
6 day ahead cum death,US,quantile,0.35,206
6 day ahead cum death,US,quantile,0.4,221
6 day ahead cum death,US,quantile,0.45,237
6 day ahead cum death,US,quantile,0.5,252
6 day ahead cum death,US,quantile,0.55,268
6 day ahead cum death,US,quantile,0.6,284
6 day ahead cum death,US,quantile,0.65,303
6 day ahead cum death,US,quantile,0.7,324
6 day ahead cum death,US,quantile,0.75,348
6 day ahead cum death,US,quantile,0.8,375
6 day ahead cum death,US,quantile,0.85,409
6 day ahead cum death,US,quantile,0.9,459
6 day ahead cum death,US,quantile,0.95,541
6 day ahead cum death,US,quantile,0.975,624
6 day ahead cum death,US,quantile,0.99,749
6 day ahead inc death,US,quantile,0.01,3
6 day ahead inc death,US,quantile,0.025,4
6 day ahead inc death,US,quantile,0.05,7
Expand Down

0 comments on commit b07aa50

Please sign in to comment.