Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

preprocess.R 14 KB

You have to be logged in to leave a comment. Sign In
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  1. library(readr)
  2. library(dplyr)
  3. library(tidyr)
  4. library(tidyselect)
  5. library(lubridate)
  6. # Reading raw data --------------------------------------------------------
  7. # Google Mobility Report (GMR) dataset
  8. raw_dataset <- read_csv(file = 'data/raw/Global_Mobility_Report.csv',
  9. col_types = paste(c(rep('c', 7),
  10. 'D',
  11. rep('d', 6)),
  12. collapse=''))
  13. colnames(raw_dataset) <- c('locality_code', 'locality_name', 'region_name',
  14. 'county_name', 'metro_area', 'iso_3166_2',
  15. 'census_fips', 'date', 'retail_recreation',
  16. 'grocery_pharmacy', 'parks', 'transit_stations',
  17. 'workplaces', 'residential')
  18. # Remove new columns
  19. raw_dataset <- raw_dataset %>%
  20. select(-c('iso_3166_2', 'census_fips'))
  21. # COVID19 dataset from ECDC
  22. covid <- read_delim(file = 'data/raw/COVID19_worldwide_raw.csv', na = '',
  23. col_types = cols('c', 'i', 'i', 'i', 'i', 'i', 'c', 'c',
  24. 'c', 'i', 'c', 'c'),
  25. delim = ',')
  26. colnames(covid) <- c('date', 'day', 'month', 'year', 'new_cases', 'new_deaths',
  27. 'locality_name', 'country_id', 'territory_id',
  28. 'pop_data_2018', 'continent', 'cumulative')
  29. covid <- covid %>%
  30. select(-c('cumulative'))
  31. # COVID-19 data for Réunion and Hong Kong that are missing in the ECDC dataset
  32. # Source: John Hopkins University https://github.com/CSSEGISandData/COVID-19
  33. covid_hk_re <- read_delim(file = 'data/raw/hk-reunion-covid-19.csv',
  34. col_types = cols('c', 'c', 'd', 'd'),
  35. delim = ',')
  36. covid_hk_re %>%
  37. mutate(locality_name = ifelse(locality_name == 'Reunion',
  38. 'Réunion',
  39. locality_name)) -> covid_hk_re
  40. # Country details from UN Data
  41. country_details <- read_delim(file = 'data/raw/UN_dataset.tsv', delim = '\t',
  42. col_types = paste(c('c',
  43. rep('d', 173),
  44. 'c'),
  45. collapse=''))
  46. # Preprocessing step ---------------------------------------------------------
  47. ####
  48. #
  49. # Working on GMR dataset
  50. #
  51. ####
  52. # Create a long table from the wide original version
  53. preprocessed_dataset <- pivot_longer(raw_dataset, cols=7:12, names_to = 'plot_name',
  54. values_to = 'variation')
  55. rm(raw_dataset)
  56. # Remove rows on metrpolitan area
  57. preprocessed_dataset <- preprocessed_dataset %>%
  58. filter(is.na(metro_area)) %>%
  59. select(-c(metro_area))
  60. ####
  61. #
  62. # Working on COVID19 dataset
  63. #
  64. ####
  65. covid %>%
  66. # These columns are useless
  67. select(-c('day', 'month', 'year', 'country_id', 'territory_id',
  68. 'pop_data_2018', 'continent')) %>%
  69. # Convert date to Date
  70. mutate(date, date = dmy(covid$date)) %>%
  71. # Replace _ by space in country names
  72. mutate(locality_name,
  73. locality_name = gsub('_',
  74. ' ',
  75. locality_name)) -> covid
  76. covid <- rbind(covid, covid_hk_re)
  77. rm(covid_hk_re)
  78. # Some countries have different names between the datasets
  79. covid %>%
  80. mutate(locality_name, locality_name = case_when(
  81. locality_name == 'United States of America' ~ 'United States',
  82. locality_name == 'United Republic of Tanzania' ~ 'Tanzania',
  83. locality_name == 'Guinea Bissau' ~ 'Guinea-Bissau',
  84. locality_name == 'Bahamas' ~ 'The Bahamas',
  85. locality_name == 'Myanmar' ~ 'Myanmar (Burma)',
  86. locality_name == 'Cote dIvoire' ~ 'Côte d\'Ivoire',
  87. TRUE ~ locality_name)
  88. ) -> covid
  89. # Some rows are missing like March 2nd and 3rd for Brazil. Apparently,
  90. # this happens when there is no case/death. Complete with these rows.
  91. covid %>%
  92. complete(date = seq.Date(min(date), max(date), by='day'), locality_name) %>%
  93. mutate(new_cases = ifelse(is.na(new_cases), 0, new_cases)) %>%
  94. mutate(new_deaths = ifelse(is.na(new_deaths), 0, new_deaths)) -> covid
  95. ####
  96. #
  97. # Merge World COVID19 and GMR datasets
  98. #
  99. ####
  100. preprocessed_dataset <- left_join(preprocessed_dataset, covid,
  101. by = c('date', 'locality_name'))
  102. rm(covid)
  103. ####
  104. #
  105. # Working on dataset after 1st merge
  106. #
  107. ####
  108. # Create column with accumulate cases/death
  109. preprocessed_dataset %>%
  110. group_by(locality_name, plot_name, region_name) %>%
  111. arrange(date) %>%
  112. dplyr::mutate(acc_cases = cumsum(new_cases)) %>%
  113. dplyr::mutate(acc_deaths = cumsum(new_deaths)) %>%
  114. ungroup -> preprocessed_dataset
  115. # Set new_cases and new_deaths to NA for regions, because our COVID19 dataset
  116. # only has data for countries.
  117. preprocessed_dataset %>%
  118. mutate(new_cases = ifelse(!is.na(region_name),
  119. NA,
  120. new_cases)) %>%
  121. mutate(new_deaths = ifelse(!is.na(region_name),
  122. NA,
  123. new_deaths)) -> preprocessed_dataset
  124. ####
  125. #
  126. # Working on country details dataset (UN)
  127. #
  128. ####
  129. # Before merging to get more info about the countries, we must make sure all
  130. # country names are the same.
  131. #unique(preprocessed_dataset$locality_name)[which(
  132. # unique(preprocessed_dataset$locality_name) %in%
  133. # unique(country_details$region_name) == FALSE
  134. # )
  135. # ]
  136. country_details %>%
  137. mutate(region_name = case_when(
  138. region_name == 'Bolivia (Plurinational State of)' ~ 'Bolivia',
  139. region_name == 'Bahamas' ~ 'The Bahamas',
  140. region_name == 'Republic of Korea' ~ 'South Korea',
  141. region_name == 'Cabo Verde' ~ 'Cape Verde',
  142. region_name == 'China, Hong Kong SAR' ~ 'Hong Kong',
  143. region_name == 'Republic of Moldova' ~ 'Moldova',
  144. region_name == 'Lao People\'s Democratic Republic' ~ 'Laos',
  145. region_name == 'Myanmar' ~ 'Myanmar (Burma)',
  146. region_name == 'United Rep. of Tanzania' ~ 'Tanzania',
  147. region_name == 'United States of America' ~ 'United States',
  148. region_name == 'Venezuela (Boliv. Rep. of)' ~ 'Venezuela',
  149. region_name == 'Viet Nam' ~ 'Vietnam',
  150. TRUE ~ region_name)
  151. ) -> country_details
  152. # In some datastes US appears as United States, and in others as United States
  153. # of America. The naming was fixed earlier, but we have two rows for US. Fix.
  154. # ids <- which(country_details$region_name == 'United States')
  155. # Some variables are there for US, others are there only for United States.
  156. # Merge.
  157. country_details[88,][,17:42] <- country_details[217,][,17:42]
  158. country_details[88,][,45:51] <- country_details[217,][,45:51]
  159. country_details[88,][,53:55] <- country_details[217,][,53:55]
  160. country_details[88,][,63:72] <- country_details[217,][,63:72]
  161. country_details[88,][,77:80] <- country_details[217,][,77:80]
  162. country_details[88,][,90:93] <- country_details[217,][,90:93]
  163. country_details[88,][,97:104] <- country_details[217,][,97:104]
  164. country_details[88,][,112:114] <- country_details[217,][,112:114]
  165. country_details[88,][,116:118] <- country_details[217,][,116:118]
  166. country_details[88,][,128:133] <- country_details[217,][,128:133]
  167. country_details[88,][,143:148] <- country_details[217,][,143:148]
  168. country_details[88,][,150] <- country_details[217,][,150]
  169. country_details[88,][,152:157] <- country_details[217,][,152:157]
  170. country_details[88,][,159:175] <- country_details[217,][,159:175]
  171. country_details <- country_details[-217, ]
  172. ####
  173. #
  174. # Merge country details and preprocessed_dataset
  175. #
  176. ####
  177. preprocessed_dataset <- left_join(preprocessed_dataset, country_details,
  178. by = c('locality_name' = 'region_name'))
  179. rm(country_details)
  180. # Remove rows that are related to regions
  181. preprocessed_dataset %>%
  182. filter(is.na(region_name)) %>%
  183. select(-c('region_name', 'county_name')) -> preprocessed_dataset
  184. colnames(preprocessed_dataset)[10:182] %>%
  185. # Make them all lowercase
  186. tolower %>%
  187. # Replace space by underscore
  188. gsub(' ', '_', .) %>%
  189. gsub('-', '_', .) %>%
  190. gsub('_+', '_', .) %>%
  191. gsub(',', '', .) -> colnames(preprocessed_dataset)[10:182]
  192. # Create columns for lethality rate
  193. preprocessed_dataset %>%
  194. group_by(locality_name) %>%
  195. arrange(date) %>%
  196. mutate(lethality_rate_percent = (acc_deaths[n()]/acc_cases[n()])*100) %>%
  197. ungroup() -> preprocessed_dataset
  198. # Bring plot_names from row to column
  199. preprocessed_dataset %>%
  200. group_by(plot_name) %>%
  201. mutate(row = row_number()) %>%
  202. tidyr::pivot_wider(names_from = plot_name, values_from = variation) %>%
  203. select(-row) -> preprocessed_dataset
  204. # Add n_days_since_1st_case column
  205. preprocessed_dataset %>%
  206. group_by(locality_name) %>%
  207. mutate(first_case_date = min(date[acc_cases > 0])) %>%
  208. mutate(n_days_since_1st_case =
  209. if_else(acc_cases > 0,
  210. as.numeric(date - min(date[acc_cases > 0])),
  211. 0)) %>%
  212. ungroup() -> preprocessed_dataset
  213. # Add n_days_since_1st_death column
  214. preprocessed_dataset %>%
  215. group_by(locality_name) %>%
  216. mutate(first_death_date = min(date[acc_deaths > 0])) %>%
  217. mutate(n_days_since_1st_death =
  218. if_else(acc_deaths > 0,
  219. as.numeric(date - min(date[acc_deaths > 0])),
  220. 0)) %>%
  221. ungroup() -> preprocessed_dataset
  222. # There is a warning here, because 15 countries don't have any date with
  223. # deaths. So the minimum of nothing will throw a warning.
  224. # preprocessed_dataset %>%
  225. # group_by(locality_name) %>%
  226. # arrange(date) %>%
  227. # filter(date == '2020-04-17') %>%
  228. # filter(acc_deaths == 0) %>%
  229. # select(locality_name) %>%
  230. # unique
  231. # Set manually first case for countries whose first case happened before Feb 15
  232. # Wikipedia contributors, "2019–20 coronavirus pandemic", Wikipedia, The Free
  233. # Encyclopedia,
  234. # https://en.wikipedia.org/w/index.php?title=2019%E2%80%9320_coronavirus_pandemic&oldid=952187370
  235. # (accessed April 21, 2020).
  236. preprocessed_dataset %>%
  237. mutate(first_case_date = case_when(
  238. # locality_name == 'China' ~ dmy('01-12-2019'),
  239. locality_name == 'Thailand' ~ dmy('13-01-2020'),
  240. locality_name == 'Japan' ~ dmy('16-01-2020'),
  241. locality_name == 'South Korea' ~ dmy('20-01-2020'),
  242. locality_name == 'United States' ~ dmy('20-01-2020'),
  243. locality_name == 'Taiwan' ~ dmy('21-01-2020'),
  244. locality_name == 'Hong Kong' ~ dmy('22-01-2020'),
  245. locality_name == 'Singapore' ~ dmy('23-01-2020'),
  246. locality_name == 'Vietnam' ~ dmy('23-01-2020'),
  247. locality_name == 'France' ~ dmy('24-01-2020'),
  248. locality_name == 'Nepal' ~ dmy('24-01-2020'),
  249. locality_name == 'Australia' ~ dmy('25-01-2020'),
  250. locality_name == 'Canada' ~ dmy('25-01-2020'),
  251. locality_name == 'Malaysia' ~ dmy('25-01-2020'),
  252. locality_name == 'Cambodia' ~ dmy('27-01-2020'),
  253. locality_name == 'Germany' ~ dmy('27-01-2020'),
  254. locality_name == 'Sri Lanka' ~ dmy('27-01-2020'),
  255. locality_name == 'Finalnd' ~ dmy('29-01-2020'),
  256. locality_name == 'United Arab Emirates' ~ dmy('29-01-2020'),
  257. locality_name == 'India' ~ dmy('30-01-2020'),
  258. locality_name == 'Italy' ~ dmy('30-01-2020'),
  259. locality_name == 'Philippines' ~ dmy('30-01-2020'),
  260. locality_name == 'Spain' ~ dmy('31-01-2020'),
  261. locality_name == 'Sweden' ~ dmy('31-01-2020'),
  262. locality_name == 'United Kingdom' ~ dmy('31-01-2020'),
  263. locality_name == 'Belgium' ~ dmy('04-02-2020'),
  264. locality_name == 'Egypt' ~ dmy('14-01-2020'),
  265. TRUE ~ first_case_date
  266. )
  267. ) -> preprocessed_dataset
  268. # Fix n_days since 1st case for countries that had 1st case before Feb 11
  269. countries <- c('Thailand', 'Japan', 'South Korea', 'United States', 'Taiwan',
  270. 'Hong Kong', 'Singapore', 'Vietnam', 'France', 'Nepal',
  271. 'Australia', 'Canada', 'Malaysia', 'Cambodia', 'Germany',
  272. 'Sri Lanka', 'Finland', 'United Arab Emirates', 'India',
  273. 'Italy', 'Philippines', 'Spain', 'Sweden', 'United Kingdom',
  274. 'Belgium', 'Egypt')
  275. preprocessed_dataset %>%
  276. group_by(locality_name) %>%
  277. mutate(n_days_since_1st_case =
  278. if_else(locality_name %in% countries,
  279. as.numeric(date - first_case_date)+1,
  280. n_days_since_1st_case)) %>%
  281. ungroup() -> preprocessed_dataset
  282. rm(countries)
  283. # Check documentation and see if variable names are correct ###
  284. # According to the file below, maternal mortality rate is per 100k livebirths,
  285. # not 100k people.
  286. # SYB62_246_201907_Population growth and indicators of fertility and mortality.pdf
  287. id = which(colnames(preprocessed_dataset) ==
  288. 'maternal_mortality_ratio_(deaths_per_100000_population)_2015')
  289. colnames(preprocessed_dataset)[id] <- paste0('maternal_mortality_ratio_(deaths',
  290. '_per_100000_livebirths)_2015')
  291. # According to the file below, it's participation rate.
  292. # SYB62_329_201904_Labour Force and Unemployment.pdf
  293. id = which(colnames(preprocessed_dataset) == paste0('labour_force_participatio',
  294. 'n_total_2019'))
  295. colnames(preprocessed_dataset)[id] <- paste0('labour_force_participation_rate_',
  296. 'total_2019')
  297. id = which(colnames(preprocessed_dataset) == paste0('labour_force_participatio',
  298. 'n_female_2019'))
  299. colnames(preprocessed_dataset)[id] <- paste0('labour_force_participation_rate_',
  300. 'female_2019')
  301. id = which(colnames(preprocessed_dataset) == paste0('labour_force_participatio',
  302. 'n_male_2019'))
  303. colnames(preprocessed_dataset)[id] <- paste0('labour_force_participation_rate_',
  304. 'male_2019')
  305. rm(id)
  306. # Saving final preprocessed dataset ---------------------------------------
  307. # Save full dataset
  308. write_tsv(x = preprocessed_dataset, path = 'data/preprocessed/DIB_dataset.tsv', quote_escape = FALSE)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...