Skip to content

Instantly share code, notes, and snippets.

@gka
Created March 25, 2020 17:51
Show Gist options
  • Save gka/c4b378123167420b3ba51df65b40cec5 to your computer and use it in GitHub Desktop.
Save gka/c4b378123167420b3ba51df65b40cec5 to your computer and use it in GitHub Desktop.
covid-19 cases growth rates
needs(dplyr, readr, reshape2, tidyr, zoo)
read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv") %>%
rename(country = `Country/Region`,
province = `Province/State`) %>%
select(-Lat, -Long) %>%
pivot_longer(-c(country, province), names_to = "date", values_to = "confirmed") %>%
mutate(date = as.Date(date, "%m/%d/%y")) %>%
mutate(country = ifelse(country == "China" & province == "Hong Kong",
"Hong Kong", country)) %>%
select(-province) %>%
arrange(date) %>%
group_by(date, country) %>%
mutate(confirmed = sum(confirmed)) %>%
distinct() %>%
ungroup() %>%
filter(confirmed > 100 & country != "Diamond Princess" & country != 'China') %>%
group_by(country) %>%
filter(n() > 11) %>%
arrange(country, date) %>%
# fix values for March 12 by averaging the days before and after
mutate(confirmed = ifelse(date == "2020-03-12",
lag(confirmed)+(lead(confirmed)-lag(confirmed))/2,
confirmed)) %>%
arrange(country, date) %>%
# compute growth rate for past 5 days
mutate(daysago = lag(confirmed, n=5)) %>%
mutate(growth = 100* (( confirmed / daysago )^(1/5) - 1)) %>%
# and take a 3-day rolling average from it
mutate(growth = (rollapplyr(growth, 3, mean, fill=NA))) %>%
select(country, date, growth) %>%
pivot_wider(names_from = "country", values_from = "growth") %>%
filter(date > '2020-02-29') %>%
arrange(desc(date)) %>%
write_csv('growth-rates.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment