Created
April 11, 2020 08:21
-
-
Save gorkang/4776c857ba43c056fec22a2e9abc543c to your computer and use it in GitHub Desktop.
Expected and actual deaths in Spain according to MoMo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Libraries --------------------------------------------------------------- | |
library(tidyverse) | |
library(data.table) | |
# Read data --------------------------------------------------------------- | |
# DOWNLOAD DATA FROM: https://momo.isciii.es/public/momo/dashboard/momo_dashboard.html#datos | |
DF_raw = read_csv("~/Downloads/data", guess_max = 10000) | |
# Parameters -------------------------------------------------------------- | |
days_average = 7 | |
filter_last_days = 5 | |
# Data preparation -------------------------------------------------------- | |
DF = DF_raw %>% | |
# Filters | |
filter(ambito == "nacional") %>% | |
filter(cod_sexo == "all") %>% | |
filter(cod_gedad == "all") %>% | |
filter(fecha_defuncion < max(fecha_defuncion) - filter_last_days) %>% | |
mutate(DIFF = defunciones_observadas - defunciones_esperadas) %>% | |
select(fecha_defuncion, defunciones_observadas, defunciones_esperadas, DIFF) | |
DF_plot = tibble( | |
fecha_defuncion = DF$fecha_defuncion, | |
defunciones_esperadas = frollmean(DF[, "defunciones_esperadas"], days_average) %>% unlist(), | |
defunciones_observadas = frollmean(DF[, "defunciones_observadas"], days_average) %>% unlist() | |
) %>% | |
group_by(fecha_defuncion) %>% | |
summarise(defunciones_esperadas = sum(defunciones_esperadas, na.rm = FALSE), | |
defunciones_observadas = sum(defunciones_observadas, na.rm = FALSE)) %>% | |
pivot_longer(2:3) | |
# Plot -------------------------------------------------------------------- | |
plot1 = ggplot(DF_plot, aes(fecha_defuncion, value, color = name)) + | |
geom_line() + | |
theme_minimal(base_size = 14) + | |
scale_color_hue(l = 50) + | |
scale_y_continuous(limits = c(600, max(DF_plot$value, na.rm = TRUE)), | |
labels = seq(from = 600, to = max(DF_plot$value, na.rm = TRUE) + 100, by = 200), | |
breaks = seq(from = 600, to = max(DF_plot$value, na.rm = TRUE) + 100, by = 200)) + | |
theme(legend.position = "bottom", legend.title = element_blank()) + | |
labs(caption = paste0("Rolling average of last ", days_average, " days\n", | |
"Filtering out last ", filter_last_days, " days because of delayed reporting")) | |
ggsave("NOT_GITHUB/DEV/plots/plot1.png", plot1, width = 16, height = 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment