Created
September 29, 2020 23:28
-
-
Save bjcairns/1881b4fd266a8489f884831feceb0587 to your computer and use it in GitHub Desktop.
Download the COVID-19 case counts for England and fit a simple smoothed model accounting for day-of-the-week
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
library(dplyr) | |
library(mgcv) | |
library(ggplot2) | |
# Download | |
c19 <- readr::read_csv("https://coronavirus.data.gov.uk/downloads/csv/coronavirus-cases_latest.csv") | |
# Extract case counts for England and select useful variables | |
c19_Eng <- c19 %>% filter(`Area type` == "nation") %>% | |
transmute(date = `Specimen date`, cases = `Daily lab-confirmed cases`) | |
# Limit to date range with positive number of cases and prepare for regression | |
c19_Eng_reg <- c19_Eng %>% | |
filter(date > as.Date("2020-02-22") & date < as.Date("2020-10-01")) %>% | |
mutate(weekday = weekdays(date), day_count = as.integer(date - as.Date("2020-02-22"))) | |
# Generalised additive model of cases as a smooth function of time (since 2020-02-22) and accounting for a day-of-the-week effect | |
c19.gam <- gam(cases ~ s(day_count) + weekday - 1, data = c19_Eng_reg, subset = date < as.Date("2020-09-25"), family = nb) | |
# Add the fitted line to the data | |
c19_Eng_reg <- c19_Eng_reg %>% mutate(fitted = NA) | |
c19_Eng_reg$fitted[c19_Eng_reg$date < as.Date("2020-09-25")] <- c19.gam$fitted.values | |
# Plot the points and the line | |
print( | |
ggplot(c19_Eng_reg, aes(x = date, y = cases)) + geom_point() + geom_line(aes(x = date, y = fitted)) + scale_y_log10() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment