library(tidyverse)1.10.a - Class Activity
This assignment is intended to get you familiar with the tidyverse using real time-series COVID-19 data.
The following code chunk scrapes the .csv file we will be analyzing from the web renames and selects the subset of columns that we will be using for analysis.
url <- "https://srhdpeuwpubsa.blob.core.windows.net/whdh/COVID/"
url_death <- paste0(url, "WHO-COVID-19-global-hosp-icu-data.csv")
covid_hos <- read_csv(url_death) |>
select(date = Date_reported,
country = Country,
region = WHO_region,
new_hos = Covid_new_hospitalizations_last_7days)This dataset contains COVID hospitalization data for all countries on a weekly basis since January 2020. The renamed columns retained for analysis are:
dateThe date of reporting.countryThe name of the country.regionThe name of the World Health Organization (WHO) region that the country belongs to.new_hosThe number of new reported hospitalizations over the last 7 days. days.
1 Problem
We will look at the hospitalization statistics for the US and Canada across the pandemic time period. Create a new dataframe called covid_hos_usc that retains only the rows that contain information for the “UNITED STATES OF AMERICA” and “CANADA”.
covid_hos_usc <- covid_hos |>
filter(country %in% c("UNITED STATES OF AMERICA", "CANADA"))2 Problem
We are going to make an assumption that NA values are equivalent to “no new hospitalizations reported. In other words, an NA value is treated as a zero value. Below is some code that will do that for our new dataset. This code also makes a time series plot of new hospitalizations over time.
covid_hos_usc <- covid_hos_usc |>
replace_na(list(new_hos = 0))
ggplot(covid_hos_usc, aes(x = date, y = new_hos)) +
geom_path()
The plot has an interesting look, lets zoom in on a few months in 2021. Note that this code shows you how you how to filter by dates when you have a date variable.
covid_hos_usc |>
filter(date > ymd("2021-01-01") & date < ymd("2021-07-01")) |>
ggplot(aes(x = date, y = new_hos)) +
geom_path()
Discuss with a partner: What seems to be going on with the time series plot in this visual? Why are the lines so jagged?
The plot appears to be connecting the lines for each observation between the US and Canada, rather than plotting them as two separate time series. Because the values are so different from each other for the two countries, it gives the plot a very jagged appearance. It would be more appropriate to plot the values as separate time series
3 Problem
The following two chunks of code show two different ways to plot country level time series. The first uses color to separate the countries. This is nice to have both time series on the same plot, but the vast difference in the reported numbers for Canada makes it hard to see the hospitalization patterns in that country.
covid_hos_usc |>
ggplot(aes(x = date, y = new_hos, color = country)) +
geom_path() +
theme(legend.position = "bottom")
An alternative is to make what we call a “facet wrap” plot (more on this later) where the y-axis range can be adjusted for each plot.
covid_hos_usc |>
ggplot(aes(x = date, y = new_hos)) +
geom_path() +
facet_wrap(~country, ncol = 1, scales = "free_y")
Discuss with a partner: What do you notice in the time series plots of hospitalizations in the US and Canada? What do you wonder?
I notice that the US seems to have spikes in hospitalizations in 2023 and beyond that Canada does not. I also notice that Canada has a spike in hospitalizations much earlier than the US, which doesn’t report hospitalizations until halfway through 2020. Given my recollections of the pandemic, the US had MANY hospitalizations in March and April in New York, so this is likely a case of the observations being truly missing, rather than zero.
4 Problem
Use the round_date() function to aggregate the data in monthly blocks. This will involve first rounding the data by month, then grouping by month AND country, and then summarizing the total number of hospitalizations per month (i.e., sum()). Then, adapt the facet_wrap plot code to remake the plot of hospitalizations over time.
covid_hos_usc_month <- covid_hos_usc |>
mutate(month = round_date(date, unit = "month")) |>
group_by(month, country) |>
summarize(new_hos = sum(new_hos))
# Then, plot
ggplot(covid_hos_usc_month, aes(x = month, y = new_hos)) +
geom_path() +
facet_wrap(~country, ncol = 1, scales = "free_y")
Discuss with a partner: What effect did the use of aggregated monthly values have on the visual?
It made the visual a little less detailed and now focuses more on the major spikes. Some of the week to week variations get smoothed out in the new aggregation.
5 Problem
Use the year() function to extract the year from each observation and determine the total number of annual cases by year for both countries using a group_by/summarize strategy. Then, answer some questions about these annual statistics in your canvas quiz. You may optionally arrange the results by country and year to better see some of the statistics you need to obtain.
covid_hos_usc |>
mutate(year = year(date)) |>
group_by(year, country) |>
summarize(new_hos = sum(new_hos)) |>
arrange(country, new_hos)# A tibble: 14 × 3
# Groups: year [7]
year country new_hos
<dbl> <chr> <dbl>
1 2025 CANADA 0
2 2026 CANADA 0
3 2024 CANADA 5933
4 2023 CANADA 26193
5 2020 CANADA 32846
6 2021 CANADA 65624
7 2022 CANADA 110088
8 2026 UNITED STATES OF AMERICA 142412
9 2025 UNITED STATES OF AMERICA 378303
10 2024 UNITED STATES OF AMERICA 509673
11 2023 UNITED STATES OF AMERICA 1003355
12 2020 UNITED STATES OF AMERICA 1315148
13 2022 UNITED STATES OF AMERICA 2185376
14 2021 UNITED STATES OF AMERICA 2769076