-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.R
430 lines (322 loc) · 20.9 KB
/
global.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# Load required package
library(shiny)
library(shinyWidgets)
library(shinythemes)
library(shinydashboard)
library(magrittr) # http://blog.fens.me/r-magrittr/
library(dplyr)
library(reshape2)
library(leaflet) # interactive maps, https://rstudio.github.io/leaflet/
library(ggplot2)
library(plotly)
library(geojsonio)
library(data.table)
library(stringr)
# setting the color
covid_col = "#cc4c02" # base map brown
covid_cul = "#403d3d" #accumulate case gray dark_world
covid_new = "#ff0400" #accumulate new case red_world
covid_other_col = "#662506"
sars_col = "#045a8d"
h1n1_col = "#4d004b"
ebola_col = "#016c59"
# setting the log file
#addHandler(handler = writeToFile, logger="System", file="system.log")
world_country_date_by_define_case = function(input, file_name) {
# init missing data check
if (sum(is.na(input))/(sum(!is.na(input))+sum(is.na(input))) >= 0.1) {
stop(paste0("Error: Too much missing data for the covid-19 ", switch(file_name, 'Case' = 'Case', 'Recovery' = 'Recovery', 'Death' = 'Death'), " file. "))
}
# rought inset missing value
input[is.na(input)] = 0
# Consider the Lat and Long is based on the province unit and not complete.
# In this instance, remove the Lat and Long
input = input %>% select(-c('Lat', 'Long'))
# -------------------------------------------------
# | Province/State | Country/Region | Date | Value |
# -------------------------------------------------
input_by_date = reshape2::melt(input, id = c('Province/State', 'Country/Region'))
# This Case param is accumulate case
# -------------------------------------------------
# | Province | Country | Date | (Accumulate) Case |
# -------------------------------------------------
names(input_by_date) = c('Province', 'Country', 'Date', 'Case')
# Notice: Taiwan should be one part of China. One China Policy!!! Zero toleration.
input_by_date$Province[input_by_date$Country=="Taiwan*"] = 'Taiwan'
input_by_date$Country[input_by_date$Country=="Taiwan*"] = 'Taiwan'
# country name change
input_by_date$Country[input_by_date$Country=="Korea, South"] = "Republic of Korea"
input_by_date$Country[input_by_date$Country=="Congo (Brazzaville)" | input_by_date$Country=="Republic of the Congo"] = "Republic of the Congo"
input_by_date$Country[input_by_date$Country=="Congo (Kinshasa)"] = "Democratic Republic of the Congo"
input_by_date$Country[input_by_date$Country=="Gambia"] = "The Gambia"
input_by_date$Country[input_by_date$Country=="Bahamas"] = "The Bahamas"
input_by_date$Country[input_by_date$Country=="Czechia"] = "Czech Republic"
input_by_date$Country[input_by_date$Country=="Holy See"] = "Holy See (Vatican City)"
input_by_date$Country[input_by_date$Country=="Iran"] = "Iran (Islamic Republic of)"
input_by_date$Country[input_by_date$Country=="Laos"] = "Lao People's Democratic Republic"
input_by_date$Country[input_by_date$Country=="Moldova"] = "Moldova, Republic of"
input_by_date$Country[input_by_date$Country=="Laos"] = "Lao People's Democratic Republic"
input_by_date$Country[input_by_date$Country=="Tanzania"] = "Tanzania, United Republic of"
input_by_date$Country[input_by_date$Country=="United Kingdom"] = "UK"
input_by_date$Country[input_by_date$Country=="US"] = "USA"
input_by_date$Country[input_by_date$Country=="China"] = "Mainland China"
# ------------------------------------
# | Country | Date | Accumulate Case |
# ------------------------------------
world_cases_country_date = input_by_date %>% group_by(Country, Date) %>% summarise('Accumulate_Case' = sum(Case))
# -----------------------------------------------
# | Country | Date | Accumulate Case | New Case |
# -----------------------------------------------
unique_country = unique(world_cases_country_date$Country)
country_case_with_new_by_date_list = lapply(unique_country, function(country_name) {
country_case_by_date = world_cases_country_date[world_cases_country_date$Country == country_name, ]
country_case_by_date$New_Case = country_case_by_date$Accumulate_Case - c(country_case_by_date$Accumulate_Case[1], country_case_by_date$Accumulate_Case[1:length(country_case_by_date$Accumulate_Case)-1])
country_case_by_date
})
world_cases_with_new_country = data.frame(do.call('rbind', country_case_with_new_by_date_list))
# check the global accumulate number
param1 = sum(input[,ncol(input)])
latest_day = names(input)[ncol(input)]
param2 = sum((world_cases_country_date %>% filter(Date == latest_day))$Accumulate_Case)
if(param1 == param2) {
print(str_c('Until ', latest_day, ', the global ', switch(file_name, 'Case' = 'Case', 'Recovery' = 'Recovery', 'Death' = 'Death'), ' is ', param2, ' .'))
} else {
stop(str_c('Global ', switch(file_name, 'Case' = 'Case', 'Recovery' = 'Recovery', 'Death' = 'Death'), ' ', param1, ' should be equal to ', param2, ' .'))
}
# -----------------------------------------------
# | Country | Date | Accumulate Case | New Case |
# -----------------------------------------------
world_cases_with_new_country
}
##############################################################
# Import Data #
##############################################################
# Note: data.table:fread is more effeicient for csv: https://www.r-bloggers.com/how-data-tables-fread-can-save-you-a-lot-of-time-and-memory-and-take-input-from-shell-commands/
# I take Shanghai of China as an example, and this data seems robust.
# The Lat and Long is basing the provice of this country.
# ----------------------------------------------------------
# | Province/State | Country/Region | Lat | Long | Date... |
# ----------------------------------------------------------
world_cases = as.data.frame(data.table::fread("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"))
print(str_c("The country name length(include ship): ", length(unique(world_cases$`Country/Region`))))
# ----------------------------------------------------------
# | Province/State | Country/Region | Lat | Long | Date... |
# ----------------------------------------------------------
world_death = as.data.frame(data.table::fread("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv"))
# ----------------------------------------------------------
# | Province/State | Country/Region | Lat | Long | Date... |
# ----------------------------------------------------------
world_recovery = as.data.frame(data.table::fread("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv"))
# they should have country consistence
if((length(unique(world_cases$`Country/Region`)) != length(unique(world_death$`Country/Region`))) || (length(unique(world_cases$`Country/Region`)) != length(unique(world_recovery$`Country/Region`)))) {
stop(paste0("Error: Data import has inconsistence !!!!!"))
}
if(sum(unique(world_cases$`Country/Region`) %in% unique(world_death$`Country/Region`)) != sum(unique(world_recovery$`Country/Region`) %in% unique(world_death$`Country/Region`))) {
stop(paste0("Error: Data import county has inconsistence !!!!!"))
}
# -----------------------------------------------
# | Country | Date | Accumulate Case | New Case |
# -----------------------------------------------
world_cases_date_with_new = world_country_date_by_define_case(world_cases, 'Case')
# ------------------------------------------------
# | Country | Date | Accumulate_Death | New Death |
# ------------------------------------------------
world_death_date_with_new = world_country_date_by_define_case(world_death, 'Death')
names(world_death_date_with_new) = c('Country', 'Date', 'Accumulate_Death', 'New_Death')
# ------------------------------------------------------
# | Country | Date | Accumulate_Recovery | New Recovery |
# ------------------------------------------------------
world_recovery_with_new = world_country_date_by_define_case(world_recovery, 'Recovery')
names(world_recovery_with_new) = c('Country', 'Date', 'Accumulate_Recovery', 'New_Recovery')
# before merging, check again
if(nrow(world_cases_date_with_new) != nrow(world_death_date_with_new) || nrow(world_death_date_with_new) != nrow(world_recovery_with_new)) {
stop(paste0("Error: Data maniputaion has inconsistence !!!!!"))
}
# merging
temp = inner_join(world_cases_date_with_new, world_death_date_with_new, by = c("Country", "Date"))
# -------------------------------------------------------------------------------------------------------------------
# | Country | Date | Accumulate Case | New Case | Accumulate_Death | New_Death | Accumulate_Recovery | New_Recovery |
# -------------------------------------------------------------------------------------------------------------------
cv_cases = inner_join(temp, world_recovery_with_new, by = c("Country", "Date"))
# Accumulate Case = Accumulate_Death + Accumulate_Recovery + Active_Case
cv_cases$Active_Case = cv_cases$Accumulate_Case - cv_cases$Accumulate_Death - cv_cases$Accumulate_Recovery
##############################################################
# Import country alpha3 and population #
##############################################################
# I need the countryname, alpha3, population, latitude and logitude
countries = read.csv("input_data/countries_codes_and_coordinates.csv")
# Just easy to join
names(countries)[2] = 'Country'
print(str_c("countries' country name length(include HK, Macao and Taiwan): ", length(unique(countries$Country))))
# ensure get the not null data
countries = countries %>% filter(!is.na(population)) %>% filter(!is.na(alpha3)) %>% filter(!is.na(latitude)) %>% filter(!is.na(longitude))
print(str_c("After removing missing data, countries' country name length : ", length(unique(countries$Country))))
print("The following countries from cv_cases not be includeed in countries to get population, latitude and longitude. They can be ignored !!")
# Cabo Verde, Diamond Princess, MS Zaandam and West Bank and Gaza
print(cv_cases %>% filter(!(Country %in% countries$Country)) %>% select(Country) %>% unique)
##############################################################
# Add population, lat and lang #
##############################################################
# Just notice
# class(countries$Country) => factor, then if you use inner_join's by with character. you would get joining character vector and factor, coercing into character vector
countries$Country = as.character(countries$Country)
# length(unique(cv_cases$Country)) => 184
# length(unique((inner_join(cv_cases, countries, by = "Country"))$Country)) => 169
# some country is gone, will do some stupid manual fix
# target_countries = unique(cv_cases$Country)
# do.call('rbind', lapply(target_countries, function(country_name) {
# if(nrow(countries %>% filter(Country == country_name)) == 0) {
# country_name
# } else {
# NULL
# }
# }))
# Cabo Verde, Diamond Princess, MS Zaandam and West Bank and Gaza which is valid in cv_cases but not in countries, would be removed in inner_join
cv_cases = inner_join(cv_cases, countries, by = "Country")
# put Taiwan, HK, Mac into (Mainland) China
mainLand = cv_cases %>% filter(Country == "Mainland China")
taiwan = countries %>% filter(Country == "Taiwan")
HK = countries %>% filter(Country == "Hong Kong")
Macao = countries %>% filter(Country == "Macao")
mainLand$population = mainLand$population + taiwan$population + HK$population + Macao$population
cv_cases[cv_cases$Country == "Mainland China", 'population'] = mainLand$population
cv_cases[cv_cases$Country == "Mainland China", 'Country'] = "China"
# remove taiwan record
cv_cases = cv_cases %>% filter(Country != 'Taiwan')
# 100K per index
cv_cases$per100k = as.numeric(format(round(cv_cases$Accumulate_Case/(cv_cases$population/100000), 1), nsmall = 1))
cv_cases$newper100k = as.numeric(format(round(cv_cases$New_Case/(cv_cases$population/100000),1), nsmall=1))
cv_cases$activeper100k = as.numeric(format(round(cv_cases$Active_Case/(cv_cases$population/100000),1), nsmall=1))
# sum cv case counts by date
# cv_aggregated => draw the bar and line from histroy to defined plot day tracing
cv_aggregated = cv_cases %>% group_by(Date) %>% summarise(Accumulate_Case = sum(Accumulate_Case), New_Case = sum(New_Case), New_Recovery = sum(New_Recovery), New_Death = sum(New_Death))
cv_aggregated$Date = as.Date(cv_aggregated$Date,"%m/%d/%y") # fix the Date factor
cv_aggregated$Region = "Global"
# current_date = as.Date(max(cv_cases$date), "%Y-%m-%d")
cv_cases$Date = as.Date(cv_cases$Date,"%m/%d/%y")
today_cv = cv_cases %>% filter(Date == max(cv_cases$Date))
cv_max_date = today_cv$Date[1]
cv_min_date = min(cv_cases$Date)
# Temp way to fix one stange
#cv_aggregated[1, c(3:5)] = 0
# load the geo info
worldcountry = geojson_read("input_data/countries.geojson", what = "sp")
worldcountry_df = data.frame(Country = worldcountry$ADMIN, alpha2 = worldcountry$ISO_A2, alpha3 = worldcountry$ISO_A3)
# the name is different but actually exist
# cv_cases %>% filter(!(Country %in% worldcountry_df$Country)) %>% select(Country, alpha3) %>% unique %>% nrow
# using the alpha code to fetch
print('The following countries exist in cv_cases but without geo information')
print(cv_cases %>% filter(!(alpha3 %in% worldcountry_df$alpha3)) %>% select(Country, alpha3) %>% unique)
# remove Kosovo record
cv_cases = cv_cases %>% filter(Country != 'Kosovo')
# just select the column what we need
cv_cases = cv_cases %>% select("Country", "Date", "Accumulate_Case", "New_Case", "Accumulate_Death", "New_Death",
"Accumulate_Recovery", "New_Recovery", "Active_Case", "alpha3", "latitude", "longitude", "population", "per100k", "newper100k", "activeper100k")
print("-------- Until now all cv_cases contry with valid geo info in worldcountry, but please using alpha3 -------- ")
# # data clean
# if (sum(is.na(world_cases))/(sum(!is.na(world_cases))+sum(is.na(world_cases))) >= 0.1) {
# stop(paste0("Error: Too much missing data for the covid-19 CSV file. "))
# }
# world_cases[is.na(world_cases)] = 0
# # Consider the Lat and Long is based on the province unit and not complete.
# # In this instance, remove the Lat and Long
# world_cases = world_cases %>% select(-c('Lat', 'Long'))
# world_cases_by_date = reshape2::melt(world_cases, id =c('Province/State', 'Country/Region'))
# # This Case param is accumulate case
# # -------------------------------------------------
# # | Province | Country | Date | (Accumulate) Case |
# # -------------------------------------------------
# names(world_cases_by_date) = c('Province', 'Country', 'Date', 'Case')
# # Notice: Taiwan should be one part of China. One China Policy!!! Zero toleration.
# world_cases_by_date$Province[world_cases_by_date$Country=="Taiwan*"] = 'Taiwan'
# world_cases_by_date$Country[world_cases_by_date$Country=="Taiwan*"] = 'China'
# # country name change
# world_cases_by_date$Country[world_cases_by_date$Country=="Korea, South"] = "Republic of Korea"
# world_cases_by_date$Country[world_cases_by_date$Country=="Congo (Brazzaville)" | world_cases_by_date$Country=="Republic of the Congo"] = "Congo"
# world_cases_by_date$Country[world_cases_by_date$Country=="Congo (Kinshasa)"] = "Democratic Republic of the Congo"
# world_cases_by_date$Country[world_cases_by_date$Country=="Gambia, The"] = "The Gambia"
# world_cases_by_date$Country[world_cases_by_date$Country=="Bahamas, The"] = "The Bahamas"
# # ------------------------------------
# # | Country | Date | Accumulate Case |
# # ------------------------------------
# world_cases_country = world_cases_by_date %>% group_by(Country, Date) %>% summarise('Accumulate_Case' = sum(Case))
# # -----------------------------------------------
# # | Country | Date | New Case | Accumulate Case |
# # -----------------------------------------------
# unique_country = unique(world_cases_country$Country)
# country_case_by_date_list = lapply(unique_country, function(country_name) {
# country_case_by_date = world_cases_country[world_cases_country$Country == country_name, ]
# country_case_by_date$New_Cases = country_case_by_date$Accumulate_Case - c(country_case_by_date$Accumulate_Case[1], country_case_by_date$Accumulate_Case[1:length(country_case_by_date$Accumulate_Case)-1])
# country_case_by_date
# })
# world_cases_country = data.frame(do.call('rbind', country_case_by_date_list))
# # check the global accumulate number
# param1 = sum(world_cases[,ncol(world_cases)])
# latest_day = names(world_cases)[ncol(world_cases)]
# param2 = sum(world_cases_country %>% filter(Date == latest_day) %>% select(Accumulate_Case))
# if(param1 == param2) {
# print(str_c('Until ', latest_day, ', the global cases is ', param2, ' .'))
# } else {
# stop(str_c('Global cases1 ', param1, ' should be equal to ', param2, ' .'))
# }
# # ----------------------------------------------------------
# # | Province/State | Country/Region | Lat | Long | Date... |
# # ----------------------------------------------------------
# world_death = as.data.frame(data.table::fread("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv"))
# world_recovery = as.data.frame(data.table::fread("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv"))
# # Accumulate Case = Active Case + Accumulate Recovery + Accumulate Death
# # ----------------------------------------------------------------------------------------------------------------------------------------------
# # | Country | Date | Lat | Long | New Case | Active Case | New Recovery | New Death | Accumulate Case | Accumulate Recovery | Accumulate Death |
# # ----------------------------------------------------------------------------------------------------------------------------------------------
# # -----------------------------------------------
# # | Country | Date | New Case | Accumulate Case |
# # -----------------------------------------------
# world_cases_country$New_Cases = world_cases_country$Accumulate_Case - c(world_cases_country$Accumulate_Case[1], world_cases_country$Accumulate_Case[1:(length(world_cases_country$Accumulate_Case)-1)])
# Note: data.table:fread is more effeicient for csv:
# https://www.r-bloggers.com/how-data-tables-fread-can-save-you-a-lot-of-time-and-memory-and-take-input-from-shell-commands/
# dim() => see the row and columns
# mode() => basic structure like numeric, complex, character and logical, class() is not basic structure like data.frame, matrix
#cv_cases = as.data.frame(data.table::fread("input_data/coronavirus.csv"))
# countries = read.csv("input_data/countries_codes_and_coordinates.csv")
# worldcountry = geojson_read("input_data/countries.geo.json", what = "sp")
##############################################################
# Import Data Check #
##############################################################
# covid-19 country should be all in the world country name
# if (all(unique(cv_cases$Country) %in% countries$country) == FALSE) {
# print("Error: inconsistent country names")
# }
# remove the NULL population country
# countries %>% filter(is.na(population)) => no important tiny island
# countries = countries[!is.na(countries$population), ]
#
##############################################################
# Set mapping colour for each outbreak #
##############################################################
# covid_col = "#cc4c02"
# covid_cul = "#111111" #brown
# covid_new = "#f63621" #red
# covid_other_col = "#662506"
# sars_col = "#045a8d"
# h1n1_col = "#4d004b"
# ebola_col = "#016c59"
# ##############################################################
# # Data Manipulation #
# ##############################################################
# cv_cases = merge(cv_cases, countries, by = "country")
# cv_cases$per100k = as.numeric(format(round(cv_cases$cases/(cv_cases$population/100000), 1), nsmall = 1))
# cv_cases$newper100k = as.numeric(format(round(cv_cases$new_cases/(cv_cases$population/100000),1), nsmall=1))
# cv_cases$activeper100k = as.numeric(format(round(cv_cases$active_cases/(cv_cases$population/100000),1), nsmall=1))
# # sum cv case counts by date
# # cv_cases %>% group_by(date) %>% summarise(sum(cases))
# # cv_aggregated = aggregate(cv_cases$cases, by=list(Category=cv_cases$date), FUN=sum)
# cv_aggregated = cv_cases %>% group_by(date) %>% summarise(cases = sum(cases), new_cases = sum(new_cases), new_recovered = sum(new_recovered), new_deaths = sum(new_deaths))
# names(cv_aggregated) = c("date", "cases", "new_cases", "new_recovered", "new_deaths")
# cv_aggregated$region = "Global"
# cv_aggregated$date = as.Date(cv_aggregated$date,"%Y-%m-%d")
# # Temp way to fix one stange
# cv_aggregated[1, c(3:5)] = 0
# # current_date = as.Date(max(cv_cases$date), "%Y-%m-%d")
# today_cv = cv_cases %>% filter(date == max(cv_cases$date))
# cv_max_date = today_cv$date[1]
# cv_min_date = min(cv_cases$date)