-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path42-covid19.Rmd
More file actions
270 lines (193 loc) · 8.67 KB
/
Copy path42-covid19.Rmd
File metadata and controls
270 lines (193 loc) · 8.67 KB
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
# Covid 19 {#covid19}
> An example of an R Notebook, rendered at `r format(as.POSIXct(Sys.time()), tz="Asia/Tokyo",usetz=TRUE)`
## Introduction
The following site of Johns Hopkins University is famous:
* https://coronavirus.jhu.edu/map.html
In this article, we study a coronavirus data collected by Johns Hopkins University called "JHU Covid-19 global time series data". Since the original data requires reshaping, we use the data provided by RamiKrispin in the following site.
* https://github.com/RamiKrispin/coronavirus/tree/master/csv
See also the R package `coronavirus` at
* https://CRAN.R-project.org/package=coronavirus
* For installation:
```{r eval=FALSE}
install.packages("coronavirus")
```
```{r}
library(tidyverse)
library(coronavirus)
library(owidR)
```
We can directly download and read the data from:
* https://github.com/RamiKrispin/coronavirus/raw/master/csv/coronavirus.csv
It is updated daily.
In this note, we use the original JHU data and transform it using `dplyr` in the form similar to the Krispin's.
Our world in data also provides vairous data related to covid-19. We will study a little by using `owid_covid`.
## Krispin's Package
```{r eval = FALSE, cache = TRUE}
coronavirus_tv <- read_csv("https://github.com/RamiKrispin/coronavirus/raw/master/csv/coronavirus.csv")
```
```{r eval = FALSE}
write_csv(coronavirus_tv, "data/coronavirus_tv.csv")
```
```{r echo=FALSE}
coronavirus_tv <- read_csv("data/coronavirus_tv.csv")
```
```{r cache = TRUE}
COUNTRY <- "Japan"
df_tv <- select(coronavirus_tv, c(date, country, type, cases, population))
df_tv0 <- filter(df_tv, country %in% COUNTRY)
df_tv_confirmed <- filter(df_tv0, type == "confirmed")
df_tv_confirmed_pp <- mutate(df_tv_confirmed, confirmed_pp = cases*100000/population)
ggplot(df_tv_confirmed_pp) + geom_line(aes(x = date, y = confirmed_pp), col = "red") +
geom_smooth(aes(x = date, y = confirmed_pp), formula = y~x, method="loess", span = 0.1, se=FALSE) +
labs(x = "Date", y = "Number of Confirmed Cases per 100,000",
title = paste("Number of Confirmed Cases per 100,000 ", COUNTRY))
```
```{r cache = TRUE}
COUNTRIES <- c("US", "Germany", "India", "South Africa","Korea, South", "Japan")
start_date <- as.Date("2021-07-01")
end_date <- Sys.Date()
coronavirus_tv %>% select(c(date, country, type, cases, population)) %>%
filter(country %in% COUNTRIES) %>%
filter(date >=start_date & df_tv0$date <= end_date) %>%
filter(type == "confirmed") %>%
mutate(confirmed_pp = cases*100000/population) %>%
ggplot() +
geom_smooth(aes(x = date, y = confirmed_pp, color = country), formula=y~x, method="loess", se=FALSE, span=0.1) +
labs(x = "Date", y = "Number of Confirmed Cases per 100,000",
title = "Number of Confirmed Cases per 100,000")
```
```{r cache = TRUE}
COUNTRIES <- c("US", "Germany", "India", "South Africa","Korea, South", "Japan")
start_date <- as.Date("2021-07-01")
end_date <- Sys.Date()
df_tv <- select(coronavirus_tv, c(date, country, type, cases, population))
df_tv0 <- filter(df_tv, country %in% COUNTRIES)
df_tv1 <- filter(df_tv0, date >=start_date & df_tv0$date <= end_date)
df_tv1_confirmed <- filter(df_tv1, type == "confirmed")
df_tv1_confirmed_pp <- mutate(df_tv1_confirmed, confirmed_pp = cases*100000/population)
ggplot(df_tv1_confirmed_pp) +
geom_smooth(aes(x = date, y = confirmed_pp, color = country), formula=y~x, method="loess", se=FALSE, span=0.1) +
labs(x = "Date", y = "Number of Confirmed Cases per 100,000",
title = "Number of Confirmed Cases per 100,000")
```
## Data of Johns Hopkins Universiy
### Importing Raw Data
We import the original Johns Hopkins Github data.
* COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University: https://github.com/CSSEGISandData/COVID-19
* We use [time series data](https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_time_series)
```{r cache = TRUE}
# IMPORT RAW DATA: Johns Hopkins Github data
confirmedraw <- 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")
glimpse(confirmedraw)
confirmedraw %>% slice(1:10)# %>% datatable() # Check latest date at the end of data as tibble
```
We need to tranform data. The other data are similar.
```{r cache = TRUE}
deathsraw <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")
deathsraw %>% slice(1)
```
```{r cache = TRUE}
recoveredraw <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv")
recoveredraw %>% slice(1)
# Note differences in the number of rows/columns
```
### Tidying and Combining: To create country level and global combined data
#### Convert each data set from wide to long
```{r cache = TRUE}
confirmed <- confirmedraw %>%
dplyr::rename(province = "Province/State", country = "Country/Region", lat = "Lat", long = "Long") %>%
pivot_longer(-c(province, country, lat, long), names_to = "date", values_to ="confirmed") %>%
mutate(date = as.Date(date, "%m/%d/%y")) %>%
group_by(province, country) %>% arrange(date) %>%
mutate(confirmed = confirmed - lag(confirmed)) %>%
slice(-1) %>% ungroup() %>%
relocate(date, .before = province) %>%
group_by(country, province) %>%
arrange(province, date)
```
Check the data.
```{r cache = TRUE}
confirmed %>% filter(country == "Japan") %>% ggplot() + geom_line(aes(x = date, y = confirmed))
```
```{r cache = TRUE}
df_tv %>% filter(country == "Japan") %>% filter(type == "confirmed") %>% ggplot() + geom_line(aes(x = date, y = cases))
```
_The `dplyr::rename` seems to have conflict with other `rename` function._
```{r cache = TRUE}
deaths <- deathsraw %>%
dplyr::rename(province = "Province/State", country = "Country/Region", lat = Lat, long = Long) %>%
pivot_longer(-c(province, country, lat, long), names_to = "date", values_to ="death") %>%
mutate(date = as.Date(date, "%m/%d/%y")) %>%
group_by(province, country) %>% arrange(date) %>%
mutate(death = death - lag(death)) %>%
slice(-1) %>% ungroup() %>%
relocate(date, .before = province) %>%
arrange(province, date)
```
```{r cache = TRUE}
recovered <- recoveredraw %>%
dplyr::rename(province = "Province/State", country = "Country/Region", lat = Lat, long = Long) %>%
pivot_longer(-c(province, country, lat, long), names_to = "date", values_to ="recovered") %>%
mutate(date = as.Date(date, "%m/%d/%y")) %>%
group_by(province, country) %>% arrange(date) %>%
mutate(recovered = recovered - lag(recovered)) %>%
slice(-1) %>% ungroup() %>%
relocate(date, .before = province) %>%
arrange(province, date)
```
#### Final data: combine all three
```{r cache = TRUE}
coronavirus_jhu <- full_join(confirmed, deaths) %>% full_join(recovered) %>%
pivot_longer(c(confirmed, death, recovered), names_to = "cases") %>%
arrange(cases, province, country, date)
coronavirus_jhu %>% slice(1:10) # %>% datatable()
```
### Aggregated by Countries
The list of countries classified in provinces.
```{r cache = TRUE}
coronavirus_jhu %>% filter(!is.na(province)) %>% distinct(country)
```
Check the data associated with provinces.
If we are only interested in countries, the following is a possibility.
```{r cache = TRUE}
coronavirus_jhu_country <- coronavirus_jhu %>%
group_by(date, country, cases) %>%
summarize(value = sum(value)) %>%
arrange(cases, country, date)
coronavirus_jhu_country # %>% datatable()
```
## `owid_covid`
owid_covid: Get the Our World in Data covid-19 dataset
```
owid_covid()
```
See the detail at the [GitHub site](https://github.com/owid/covid-19-data/tree/master/public/data#data-on-covid-19-coronavirus-by-our-world-in-data).
* Example
```{r cash=TRUE}
covid <- owid_covid()
```
```{r}
glimpse(covid)
```
```{r}
tdpm <- covid %>% drop_na(`total_deaths_per_million`) %>%
group_by(location) %>%
summarize(`total_deaths_per_million` = max(`total_deaths_per_million`)) %>%
arrange(desc(`total_deaths_per_million`))
DT::datatable(tdpm)
```
```{r}
covid %>% drop_na(`total_deaths_per_million`) %>%
group_by(location) %>%
summarize(`total_deaths_per_million` = max(`total_deaths_per_million`), .groups = "drop") %>%
arrange(desc(`total_deaths_per_million`)) %>%
ggplot(aes(x = `total_deaths_per_million`)) + geom_histogram(bins = 30)
```
```{r}
covid %>% filter(location == "Japan") %>% arrange(desc(date))
```
```{r}
covid %>% filter(location == "Japan") %>% drop_na(new_deaths_smoothed) %>%
ggplot(aes(date, new_deaths_smoothed)) + geom_col() +
labs(title=paste("Covid-19 Death/Day in Japan as of ", Sys.Date()))
```