Warning: package 'broom' was built under R version 4.2.3
library(knitr)# source https://github.com/nytimes/covid-19-data.gitus_states_long <- covid19nytimes::refresh_covid19nytimes_states()# The following filter is to restrict the data to that originally posted at# https://outsiderdata.netlify.app/post/covid-cases-vs-deaths/# Should you wish to update the models with the latest data, remove the# following statement.us_states_long <- us_states_long %>%filter(date <ymd("2020-12-06"))# if link is broken# load("../data/us_states_long.rdata")# use data from November 15 to stay consistent with text narrativecutoff_start <-as.Date("2020-03-15") # not widespread enough until thencutoff_end <-max(us_states_long$date) -7# discard last week since there are reporting lagsus_states_long <- us_states_long %>%filter(date >= cutoff_start)us_states_long <- us_states_long %>%filter(date <= cutoff_end)# Remove tiny territoriesterritories <-c("Guam", "Northern Mariana Islands")us_states_long <- us_states_long %>%filter(!(location %in% territories))save(us_states_long, file ="us_states_long.rdata")
Create rolling average changes
In [2]:
# Create rolling average changes# pivot wider# this will also be needed when we create lagsus_states <- us_states_long %>%# discard dates before cases were tracked.filter(date >as.Date("2020-03-01")) %>%pivot_wider(names_from ="data_type", values_from ="value") %>%rename(state = location) %>%select(date, state, cases_total, deaths_total) %>%mutate(state =as_factor(state)) %>%arrange(state, date) %>%group_by(state) %>%# smooth the data with 7 day moving averagemutate(cases_7day = (cases_total -lag(cases_total, 7)) /7) %>%mutate(deaths_7day = (deaths_total -lag(deaths_total, 7)) /7)
Warning: Removed 7 rows containing missing values or values outside the scale range
(`geom_line()`).
Removed 7 rows containing missing values or values outside the scale range
(`geom_line()`).
From the New York Times: A couple of observations are obvious. First when cases start to rise, deaths follow with a lag. Second, we have had three spikes in cases so far and in each successive instance the mortality has risen by a smaller amount. This suggests that, thankfully, we are getting better at treating this disease. It is NOT a function of increased testing because positivity rates have not been falling.