Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lag_us <- 7
- cases <-
- read.csv("../nytimes_covid-19-data/us-states.csv") %>%
- as_tibble() %>%
- mutate(date = as.Date(date)) %>%
- select(date, state, cases) %>%
- arrange(date, state) %>%
- pivot_wider(id_cols = c("date"), names_from = c("state"), values_from = c("cases")) %>%
- replace(., is.na(.), 0) %>%
- mutate(total = rowSums(select(., !contains("date")))) %>%
- select(date, total) %>%
- rename(total_cases = total) %>%
- mutate(new_cases = c(1, diff(total_cases))) %>%
- select(date, new_cases)
- deaths <-
- read.csv("../nytimes_covid-19-data/us-states.csv") %>%
- as_tibble() %>%
- mutate(date = as.Date(date) - lag_us) %>%
- select(date, state, deaths) %>%
- arrange(date, state) %>%
- pivot_wider(id_cols = c("date"), names_from = c("state"), values_from = c("deaths")) %>%
- replace(., is.na(.), 0) %>%
- mutate(total = rowSums(select(., !contains("date")))) %>%
- select(date, total) %>%
- rename(total_deaths = total) %>%
- mutate(new_deaths = c(0, diff(total_deaths))) %>%
- select(date, new_deaths)
- data_cfr <-
- cases %>%
- left_join(deaths, by = "date") %>%
- mutate(cfr_us = new_deaths / new_cases) %>%
- mutate(cfr_us = ifelse(is.finite(cfr_us), cfr_us, NA)) %>%
- mutate(cfr_us_trend = ts(cfr_us, frequency = 7) %>% mstl() %>% trendcycle) %>%
- mutate(cfr_us_trend = ifelse(is.na(cfr_us), NA, cfr_us_trend)) %>%
- filter(date >= as.Date("2020-04-01"))
- g_cfr <-
- ggplot(data = data_cfr) +
- theme_classic() +
- theme(axis.text.x = element_text(angle = 45)) +
- theme(axis.text.x = element_text(vjust = 0.7)) +
- theme(axis.text.x = element_text(hjust = 0.8)) +
- theme(legend.title = element_blank()) +
- #theme(legend.position = "none") +
- geom_line(size = 1.0, aes(x = as.Date(date), y = cfr_us_trend, color = "USA")) +
- scale_x_date(date_labels = "%m/%d") +
- labs(title = "Case Fatality Rate in the US", x = "Date", y = "Case Fatality Rate") +
- scale_y_continuous(labels = scales::percent)
- print(g_cfr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement