Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thoughtfulbloke/5fc95f10c442da965364d130533618fd to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/5fc95f10c442da965364d130533618fd to your computer and use it in GitHub Desktop.
library(dplyr)
library(countrycode)
library(lubridate)
library(tidyr)
library(ggplot2)
library(ggthemes)
library(ggrepel)
# apple mobility data csv from
# https://www.apple.com/covid19/mobility
amt <- read.csv("~/Desktop/applemobilitytrends-2020-05-20.csv", colClasses = "character",
check.names = FALSE)
data_extent <- ncol(amt)
# EU CDC
# https://www.ecdc.europa.eu/en/geographical-distribution-2019-ncov-cases
euCDC <- read.csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv",
stringsAsFactors = FALSE)
depth <- amt %>%
filter(transportation_type == "driving", geo_type=="country/region") %>%
gather(key="Date", value="mobility", 7:data_extent) %>%
select(region, Date, transportation_type, mobility) %>%
mutate(Date = ymd(Date), mobility=as.numeric(mobility)) %>%
arrange(region, Date) %>%
group_by(region) %>%
mutate(sevendaymob = mobility + lag(mobility, 1) + lag(mobility, 2) + lag(mobility, 3) +
lag(mobility, 4) + lag(mobility, 5) + lag(mobility, 6)) %>%
summarise(depth = 100* min(sevendaymob, na.rm=T)/max(sevendaymob, na.rm=T)) %>%
arrange(depth)
apple <- amt %>%
filter(transportation_type == "driving", geo_type=="country/region") %>%
gather(key="Date", value="mobility", 7:data_extent) %>%
select(region, Date, transportation_type, mobility) %>%
mutate(Date = ymd(Date), mobility=as.numeric(mobility)) %>%
arrange(region, Date) %>%
group_by(region) %>%
mutate(sevendaymob = (mobility + lag(mobility, 1) + lag(mobility, 2) + lag(mobility, 3) +
lag(mobility, 4) + lag(mobility, 5) + lag(mobility, 6))/7) %>%
ungroup() %>%
filter(sevendaymob <60) %>%
arrange(region, Date) %>%
group_by(region) %>%
slice(1) %>%
ungroup() %>% select(region,speed = Date) %>% inner_join(depth) %>%
mutate(ccode = countrycode(region, 'country.name', 'iso3c'))
euCDC %>%
mutate(Date30 = dmy(dateRep)) %>%
arrange(countryterritoryCode, Date30) %>%
group_by(countryterritoryCode) %>%
mutate(total_cases = cumsum(cases)) %>%
ungroup() %>%
filter(total_cases >= 30) %>%
group_by(countryterritoryCode) %>%
slice(1) %>%
ungroup() %>%
select(ccode= countryterritoryCode, Date30) %>%
inner_join(apple) %>%
mutate(days_from_thirty = as.numeric(difftime(speed, Date30, units = "days"))) %>%
ggplot(aes(x=days_from_thirty, y=depth, label=region)) +
geom_point() + geom_label_repel() + theme_minimal() + ylab("Depth of driving immobility (percent min week/max week)") +
xlab("Speed of Lockdown (day of end of week after 30th case driving falls below 60% for week)") +
ggtitle("Speed (relative to 30th case) and strength (reduction in driving) of Lockdowns")+
labs(caption="data from EUCDC & Apple")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment