Created
December 15, 2021 22:43
-
-
Save johnmackintosh/af5dd3892b6f180de74ac5bd92ba6c87 to your computer and use it in GitHub Desktop.
rolling 28 day positive case counts in Scotland council areas using PHS open data and {cusumcharter}
This file contains hidden or 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
library(dplyr) | |
library(data.table) | |
library(ggplot2) | |
library(cusumcharter) | |
library(purrr) | |
library(tidyr) | |
library(ggExtra) | |
# make the link dynamic | |
part1 <- "https://www.opendata.nhs.scot/dataset/" | |
part2 <- "b318bddf-a4dc-4262-971f-0ba329e09b87/" | |
part3 <- "resource/427f9a25-db22-4014-a3bc-893b68243055/" | |
part4 <- "download/trend_ca_" | |
part5 <- ".csv" | |
today <- gsub('-','',as.character(Sys.Date())) | |
link <- paste0(part1, part2, part3, part4, today, part5, sep = '') | |
dates <- seq.Date(as.Date(Sys.Date()-28), as.Date(Sys.Date()-1), by = '1 day') | |
DT <- data.table::fread(link) | |
DT[, Date := as.character(Date)] | |
DT[, Date := as.IDate(Date, format = "%Y%m%d")] | |
positives <- DT[Date >= as.Date(Sys.Date() -28),.(Date, CAName, DailyPositive)][] | |
# ggplot(positives,aes(Date, DailyPositive)) + | |
# geom_line() + | |
# geom_point() + | |
# facet_wrap(~ CAName, ncol = 4, scales = "free_y") + | |
# theme_minimal() + | |
# labs(x = NULL, y = NULL) + | |
# ggExtra::rotateTextX() | |
p <- positives %>% | |
group_by(CAName) %>% | |
group_nest() %>% | |
mutate(cusums = purrr::map(.x = .$data, | |
.f = ~ cusum_control_median(.x$DailyPositive))) %>% | |
tidyr::unnest( cols = c(data, cusums)) %>% | |
cusum_control_plot(., | |
xvar = Date, | |
facet_var = CAName, | |
facet_scales = 'free_y', | |
scale_type = "date", | |
datebreaks = "3 days", | |
title_text = "CUSUM Rolling 28 Day Positive Cases in Scottish Council Areas") | |
p <- p + facet_wrap( ~ CAName, ncol = 4, scales = 'free_y') + | |
labs(subtitle = "Limits based on median over last 28 days") + | |
rotateTextX() | |
print(p) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As at 2021-12-15