/
example_code.R
337 lines (272 loc) · 9.85 KB
/
example_code.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Some example code on how to use the {tidycovid19}
# Not part of the package itself
# Some more code that uses the package can be found in the 'add_code' directory.
# --- Some visuals -------------------------------------------------------------
library(tidycovid19)
merged <- download_merged_data(cached = TRUE, silent = TRUE)
plot_covid19_spread(merged)
plot_covid19_spread(merged, highlight = "DEU",
intervention = "lockdown")
plot_covid19_spread(merged, highlight = c("ITA", "ESP", "FRA", "DEU", "USA"),
intervention = "lockdown")
plot_covid19_spread(merged, highlight = c("ITA", "ESP", "FRA", "DEU", "USA"),
exclude_others = TRUE, intervention = "lockdown")
plot_covid19_spread(
per_capita = TRUE, per_capita_x_axis = TRUE,
population_cutoff = 10,
min_cases = 0.1,
highlight = c("ITA", "ESP", "FRA", "DEU", "USA", "BEL", "FRA", "NLD", "GBR"),
intervention = "lockdown"
)
# --- Customize shiny app ------------------------------------------------------
shiny_covid19_spread(plot_options = list(
type = "deaths", min_cases = 100, min_by_ctry_obs = 10,
edate_cutoff = 40, per_capita = FALSE, cumulative = FALSE, change_ave = 7,
highlight = c("FRA", "DEU", "ITA", "ESP", "GBR", "USA"),
intervention = "lockdown"
))
# --- Example clipping code produced by shiny_covid19_spread() ------------------
# Code generated by shiny_covid19_spread() of the {tidycovid19} package
# See: https://github.com/joachim-gassen/tidycovid19
# Run in R/Rstudio. See https://www.r-project.org and https://www.rstudio.com
# Uncomment the following to install the {tidycovid19} package
# remotes::install_github("joachim-gassen/tidycovid19)
library(tidycovid19)
plot_covid19_spread(
type = "deaths", min_cases = 100, min_by_ctry_obs = 7,
edate_cutoff = 30, per_capita = FALSE,
highlight = c("BEL", "CHN", "FRA", "DEU", "IRN", "ITA", "KOR",
"NLD", "ESP", "CHE", "GBR", "USA"),
intervention = NULL
)
# --- Find data inconsitencies in JHU CSSE data --------------------------------
library(tidycovid19)
library(dplyr)
df <- download_jhu_csse_covid19_data(cached = TRUE, silent = TRUE)
df %>%
group_by(iso3c) %>%
filter(recovered < lag(recovered) |
recovered > lead(recovered)) -> odd_recovered
df %>%
group_by(iso3c) %>%
filter(deaths < lag(deaths) |
deaths > lead(deaths)) -> odd_deaths
df %>%
group_by(iso3c) %>%
filter(confirmed < lag(confirmed) |
confirmed > lead(confirmed))
# --- Use old PDF scraping code ------------------------------------------------
# Install old package version that still contains the PDF scraping code
# remotes::install_github("joachim-gassen/tidycovid19", ref = "0990bc6")
library(tidycovid19)
library(tidyverse)
library(pdftools)
library(png)
pdf_url <- "https://www.gstatic.com/covid19/mobility/2020-04-05_BR_Mobility_Report_en.pdf"
pdf_convert(pdf_url, pages = 1, filenames = "google_cmr_de_p1.png", verbose = FALSE)
bitmaps <- tidycovid19:::extract_line_graph_bitmaps(pdf_url, 1)
png_file <- tempfile("bitmap_", fileext = ".png")
writePNG(bitmaps[[1]][[1]], "bitmap.png")
df <- tidycovid19:::parse_line_graph_bitmap(bitmaps[[1]][[1]])
# Make sure that you reinstall the current version of the package after you
# are done exploring the PDF scraping code
# remotes::install_github("joachim-gassen/tidycovid19")
# --- Use regional data --------------------------------------------------------
library(tidyverse)
library(tidycovid19)
df <- download_google_cmr_data(type = "country_region", cached = TRUE)
df %>% filter(iso3c == "DEU") %>%
ggplot(aes(x = date, y = retail_recreation, color = region)) +
geom_line()
# --- Plot Oxford Data ---------------------------------------------
library(tidyverse)
library(tidycovid19)
df <- download_oxford_npi_data(type = "index", cached = TRUE)
df %>% group_by(date) %>%
summarise(
mn_si = mean(stringency_index, na.rm = TRUE),
ci_si = 1.96*(sd(stringency_index, na.rm = TRUE)/sqrt((n() - 1)))
) %>%
ggplot(aes(x = date, y = mn_si)) +
geom_line() +
geom_errorbar(
aes(
ymin= mn_si - ci_si,
ymax = mn_si + ci_si
),
width = 0.2
)
df <- download_oxford_npi_data(type = "measures", cached = TRUE)
df %>% filter(
npi_type != "Emergency investment in healthcare",
npi_type != "Investment in vaccines",
npi_measure != 0
) %>%
ggplot(aes(x = date, fill = npi_type, weight = npi_measure)) +
geom_histogram(position = "stack", binwidth = 7)
# --- Plot daily new cases as bar graph ----------------------------------------
# Suggestion by AndreaPi (issue #19)
library(tidyverse)
library(tidycovid19)
library(zoo)
df <- download_merged_data(cached = TRUE)
df %>%
filter(iso3c == "USA") %>%
mutate(
new_cases = confirmed - lag(confirmed),
ave_new_cases = rollmean(new_cases, 7, na.pad=TRUE, align="right")
) %>%
filter(!is.na(new_cases), !is.na(ave_new_cases)) %>%
ggplot(aes(x = date)) +
geom_bar(aes(y = new_cases), stat = "identity", fill = "lightblue") +
geom_line(aes(y = ave_new_cases), color ="red") +
theme_minimal()
df %>%
filter(iso3c == "USA") %>%
mutate(
new_deaths = deaths - lag(deaths),
ave_new_deaths = rollmean(new_deaths, 7, na.pad=TRUE, align="right")
) %>%
filter(!is.na(new_deaths), !is.na(ave_new_deaths)) %>%
ggplot(aes(x = date)) +
geom_bar(aes(y = new_deaths), stat = "identity", fill = "lightblue") +
geom_line(aes(y = ave_new_deaths), color ="red") +
theme_minimal()
df %>%
filter(iso3c == "DEU") %>%
mutate(
new_cases_by_100k = 1e5*((confirmed - lag(confirmed))/population),
ave_new_cases_by_100k = rollmean(new_cases_by_100k, 7, na.pad=TRUE, align="right")
) %>%
filter(!is.na(new_cases_by_100k), !is.na(ave_new_cases_by_100k)) %>%
ggplot(aes(x = date)) +
geom_bar(aes(y = new_cases_by_100k), stat = "identity", fill = "lightblue") +
geom_line(aes(y = ave_new_cases_by_100k), color ="red") +
theme_minimal()
df %>%
filter(iso3c == "DEU") %>%
mutate(
new_deaths_by_100k = (deaths - lag(deaths))/(0.1*population),
ave_new_deaths_by_100k = rollmean(new_deaths_by_100k, 7, na.pad=TRUE, align="right")
) %>%
filter(!is.na(new_deaths_by_100k), !is.na(ave_new_deaths_by_100k)) %>%
ggplot(aes(x = date)) +
geom_bar(aes(y = new_deaths_by_100k), stat = "identity", fill = "lightblue") +
geom_line(aes(y = ave_new_deaths_by_100k), color ="red") +
theme_minimal()
# --- New Our World in Data data -----------------------------------------------
library(tidyverse)
library(tidycovid19)
library(ggridges)
df <- download_merged_data(cached = TRUE, silent = TRUE)
nobs <- df %>%
group_by(iso3c) %>%
summarise(
nobs_hosp = sum(!is.na(hosp_patients)),
nobs_icu = sum(!is.na(icu_patients)),
nobs_vacc = sum(!is.na(total_vaccinations)),
.groups = "drop"
) %>%
filter(
nobs_hosp != 0 | nobs_icu != 0 | nobs_vacc != 0
) %>%
arrange(iso3c)
has_vacc_data <- df %>%
select(iso3c, total_vaccinations, gdp_capita, deaths, confirmed, population) %>%
group_by(iso3c) %>%
filter(
!all(is.na(confirmed)) & !all(is.na(deaths)) & !all(is.na(population)) &
!all(is.na(gdp_capita))
) %>%
summarise(
has_vacc_data = sum(!is.na(total_vaccinations)) > 0,
gdp_capita = mean(gdp_capita),
cases = max(1e5*(confirmed/population), na.rm = TRUE),
deaths = max(1e5*(deaths/population), na.rm = TRUE),
.groups = "drop"
)
plot_sel_bias <- function(df, xvar, xlab) {
xvar <- enquo(xvar)
ggplot(
data = df,
aes(
x = !!xvar, y = has_vacc_data,
fill = has_vacc_data, height = stat(density)
)
) +
geom_density_ridges(
stat = "binline", bins = 20, scale = 0.95
) +
scale_x_log10(labels = scales::comma_format(accuracy = 0.1)) +
labs(
x = xlab,
y = "",
title = "OWID provides vaccination data"
) +
theme_ridges() +
theme(
legend.position = "none",
plot.title.position = "plot"
)
}
plot_sel_bias(has_vacc_data, gdp_capita, "GDP per capita (in 2010 US-$, log-scaled)")
plot_sel_bias(has_vacc_data, cases, "Covid-19 cases per 100,000 inhabitants (log-scaled)")
plot_sel_bias(has_vacc_data, deaths, "Covid-19 deaths per 100,000 inhabitants (log-scaled)")
mod <- glm(
has_vacc_data ~ log(gdp_capita) + log(deaths),
data = has_vacc_data %>% filter(deaths > 0),
family = "binomial"
)
summary(mod)
clevel <- df %>%
group_by(iso3c) %>%
filter(any(!is.na(total_vaccinations))) %>%
mutate(
vacc_1e5pop = 1e5*(total_vaccinations/population),
cases_1e5pop = 1e5*(confirmed/population),
deaths_1e5pop = 1e5*(deaths/population)
) %>%
summarise(
vacc_1e5pop = max(vacc_1e5pop, na.rm = TRUE),
cases_1e5pop = max(cases_1e5pop, na.rm = TRUE),
deaths_1e5pop = max(deaths_1e5pop, na.rm = TRUE),
gdp_capita = max(gdp_capita, na.rm = TRUE),
.groups = "drop"
) %>%
na.omit()
plot_clevel_vacc_by_x <- function(df, xvar, xlab) {
xvar <- enquo(xvar)
ggplot(df, aes(x = !!xvar, y = vacc_1e5pop)) +
geom_point() +
scale_x_log10() +
scale_y_log10() +
theme_minimal() +
labs(
x = xlab,
y = "Vaccinations per 100,000 inhabitants"
) +
ggrepel::geom_text_repel(aes(label = iso3c)) +
geom_smooth(method = "lm", formula = "y ~x")
}
plot_clevel_vacc_by_x(clevel, gdp_capita, "National income per capita (2010 US-$)")
plot_clevel_vacc_by_x(clevel, cases_1e5pop, "Cases per 100,000 inhabitants")
plot_clevel_vacc_by_x(clevel, deaths_1e5pop, "Deaths per 100,000 inhabitants")
plot_clevel_vacc_by_x(
clevel %>% filter(iso3c != "GIN"),
gdp_capita, "National income per capita (2010 US-$)"
)
mod <- lm(
log(vacc_1e5pop) ~ log(gdp_capita) + log(deaths_1e5pop),
data = clevel
)
summary(mod)
mod <- lm(
log(vacc_1e5pop) ~ log(gdp_capita) + log(deaths_1e5pop),
data = clevel %>% filter(iso3c != "GIN")
)
summary(mod)
mod <- lm(
log(vacc_1e5pop) ~ log(gdp_capita) + log(deaths_1e5pop),
data = clevel %>% filter(!iso3c %in% c("CHN", "GIN"))
)
summary(mod)