Last active
May 31, 2022 06:40
-
-
Save miniak/554259b91fd004aa4f6b61affc054d88 to your computer and use it in GitHub Desktop.
COVID-19 death rate by sex / age group in the Czech Republic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/miniak/554259b91fd004aa4f6b61affc054d88 | |
// https://onemocneni-aktualne.mzcr.cz/api/v2/covid-19 | |
const fetch = require('node-fetch'); | |
const recoveredUrl = 'https://onemocneni-aktualne.mzcr.cz/api/v2/covid-19/vyleceni.csv'; | |
const deathsUrl = 'https://onemocneni-aktualne.mzcr.cz/api/v2/covid-19/umrti.csv'; | |
// const ageThresholds = [0, 15, 25, 35, 45, 55, 65, 75, 85]; | |
const ageThresholds = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]; | |
// const region = 'CZ010'; | |
const region = undefined; | |
// const year = 2022; | |
const year = undefined; | |
async function downloadCVS(url) { | |
const response = await fetch(url); | |
const lines = (await response.text()).split('\n'); | |
const keys = lines.shift().split(','); | |
const result = []; | |
for (const line of lines) { | |
const obj = {}; | |
let index = 0; | |
for (const value of line.split(',')) { | |
obj[keys[index++]] = value; | |
} | |
result.push(obj); | |
} | |
return result; | |
} | |
function roundTo(value, places) { | |
const ratio = Math.pow(10, places); | |
return Math.round(value * ratio) / ratio; | |
} | |
function logBucket(bucket) { | |
const deathRate = bucket.deaths / bucket.resolved; | |
console.log(`${bucket.label} = ${roundTo(deathRate * 100, 2)}% (${bucket.deaths} / ${bucket.resolved})`); | |
} | |
function makeBucket(label) { | |
return { | |
label, | |
resolved: 0, | |
deaths: 0 | |
}; | |
} | |
function getBucket(age) { | |
for (let i = 0; i < ageThresholds.length - 1; i++) { | |
if (ageThresholds[i] <= age && age < ageThresholds[i + 1]) { | |
const min = ageThresholds[i]; | |
const max = ageThresholds[i + 1] - 1; | |
return { value: min, label: `${min}-${max}` }; | |
} | |
} | |
const value = ageThresholds[ageThresholds.length - 1]; | |
return { value, label: `${value}+` }; | |
} | |
function parseDate(value) { | |
const [year, month, day] = value.split('-'); | |
return new Date(year, month - 1, day); | |
} | |
async function main() { | |
const recovered = await downloadCVS(recoveredUrl); | |
const deaths = await downloadCVS(deathsUrl); | |
for (const sex of ['M', 'Z', '*']) { | |
console.log(`[${{'M': 'Male', 'Z': 'Female', '*': 'Both'}[sex]}]`); | |
const buckets = {}; | |
const total = makeBucket('total'); | |
function bucketize(age) { | |
const bucket = getBucket(age); | |
return buckets[bucket.value] = buckets[bucket.value] || makeBucket(bucket.label); | |
} | |
function shouldSkip(item) { | |
if (!item.pohlavi || (sex !== '*' && item.pohlavi !== sex)) return true; | |
if (region && item.kraj_nuts_kod !== region) return true; | |
if (year && parseDate(item.datum).getFullYear() !== year) return true; | |
return false; | |
} | |
for (const item of recovered) { | |
if (shouldSkip(item)) continue; | |
const age = Number.parseInt(item.vek); | |
++bucketize(age).resolved; | |
++total.resolved; | |
} | |
let sum = 0; | |
let count = 0; | |
for (const item of deaths) { | |
if (shouldSkip(item)) continue; | |
const age = Number.parseInt(item.vek); | |
++bucketize(age).resolved; | |
++total.resolved; | |
++bucketize(age).deaths; | |
++total.deaths; | |
sum += age; | |
++count; | |
} | |
for (item of Object.values(buckets)) { | |
logBucket(item); | |
} | |
logBucket(total); | |
console.log(`avg = ${roundTo(sum / count, 1)} years`); | |
} | |
} | |
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "covid-19", | |
"version": "1.0.0", | |
"description": "", | |
"main": "covid-19.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"node-fetch": "^2.6.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
total
2022