# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .jl
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Julia 1.3.1
#     language: julia
#     name: julia-1.3
# ---

using Pkg
Pkg.add("StatsBase")
Pkg.add("CSV")
Pkg.add("DataFrames")
Pkg.add("Gadfly")

# ## Data
#
# Downloading csv data used by Johns-Hopkins Repository https://github.com/HopkinsIDD/nCoV-Sandbox from
#   - google spreadsheet: https://docs.google.com/spreadsheets/d/1jS24DjSPVWa4iuxuD4OAXrE3QeI8c9BC1hSlqr-NMiU/edit#gid=1841943470
#

# +
download("https://docs.google.com/spreadsheets/d/1jS24DjSPVWa4iuxuD4OAXrE3QeI8c9BC1hSlqr-NMiU/export?format=csv&id=1jS24DjSPVWa4iuxuD4OAXrE3QeI8c9BC1hSlqr-NMiU&gid=1187587451", 
         "data/covid19-international.csv")

download("https://docs.google.com/spreadsheets/d/1jS24DjSPVWa4iuxuD4OAXrE3QeI8c9BC1hSlqr-NMiU/export?format=csv&id=1jS24DjSPVWa4iuxuD4OAXrE3QeI8c9BC1hSlqr-NMiU&gid=1202885709",
         "data/covid19-usa.csv")

download("https://docs.google.com/spreadsheets/d/1jS24DjSPVWa4iuxuD4OAXrE3QeI8c9BC1hSlqr-NMiU/export?format=csv&id=1jS24DjSPVWa4iuxuD4OAXrE3QeI8c9BC1hSlqr-NMiU&gid=1180262595",
         "data/covid19-beijing.csv")

# +
## loading and combining datasets
using CSV
using DataFrames
missingstrings=["","NA"]
dfi = CSV.read("data/covid19-international.csv",header=2,copycols=true,missingstrings=missingstrings)
dfu = CSV.read("data/covid19-usa.csv",header=1,copycols=true,missingstrings=missingstrings)
dfb = CSV.read("data/covid19-beijing.csv",header=1,copycols=true,missingstrings=missingstrings)

# selecting 
D = vcat( [ select(d,[:country, :gender, :age, :death, :recovered]) for d in (dfi,dfu,dfb) ]...)
describe(D)
# -

# ## Age distribution of cases and outcome

using StatsBase
is0missing(x) = string(x) in [ "missing", "0" ]
D.outcome = [ is0missing(d) ? ( is0missing(r) ? :unconfirmed : :recovered) : ( is0missing(r) ? :died : :error) for (d,r) in zip(D.death, D.recovered)]
D.died = is0missing.(D.death)
by(D,:outcome, nrow)


# The data is incomplete: for a considerable number of cases it is unconfirmed whether the patient recovered or died.

using Gadfly
plot(D, x=:age, color=:outcome, Geom.histogram(bincount=10))

?Stat.histogram

sort(by(D,:country,nrow),2, rev=true)

# ## Variables available for case data
# The datasets do not contain information on 
# - medical preconditions
# - medical interventions
#
# The following lists all variables available in the loaded datasets.

describe(dfu)

describe(dfb)

describe(dfi)

StarcoderdataJupyterScriptsDedupFiltered