Skip to content

Instantly share code, notes, and snippets.

@miniak
Last active May 31, 2022 06:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miniak/554259b91fd004aa4f6b61affc054d88 to your computer and use it in GitHub Desktop.
Save miniak/554259b91fd004aa4f6b61affc054d88 to your computer and use it in GitHub Desktop.
COVID-19 death rate by sex / age group in the Czech Republic
// 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();
{
"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"
}
}
@miniak
Copy link
Author

miniak commented May 31, 2022

total

[Male]
0-9 = 0% (2 / 162591)
10-19 = 0% (6 / 277010)
20-29 = 0.01% (18 / 230626)
30-39 = 0.03% (95 / 305487)
40-49 = 0.09% (335 / 361441)
50-59 = 0.43% (1066 / 249099)
60-69 = 2.41% (3926 / 162903)
70-79 = 8.84% (8838 / 99938)
80-89 = 19.91% (6772 / 34021)
90+ = 25.97% (1721 / 6627)
total = 1.21% (22779 / 1889743)
avg = 75.7 years

[Female]
0-9 = 0% (4 / 152543)
10-19 = 0% (1 / 270359)
20-29 = 0.01% (14 / 233347)
30-39 = 0.02% (54 / 322464)
40-49 = 0.05% (202 / 408218)
50-59 = 0.19% (541 / 284198)
60-69 = 1.1% (1868 / 169559)
70-79 = 4.59% (5382 / 117382)
80-89 = 12.02% (6623 / 55094)
90+ = 17.26% (2815 / 16312)
total = 0.86% (17504 / 2029476)
avg = 79.3 years

[Both]
0-9 = 0% (6 / 315134)
10-19 = 0% (7 / 547369)
20-29 = 0.01% (32 / 463973)
30-39 = 0.02% (149 / 627951)
40-49 = 0.07% (537 / 769659)
50-59 = 0.3% (1607 / 533297)
60-69 = 1.74% (5794 / 332462)
70-79 = 6.54% (14220 / 217320)
80-89 = 15.03% (13395 / 89115)
90+ = 19.77% (4536 / 22939)
total = 1.03% (40283 / 3919219)
avg = 77.3 years

2022

[Male]
0-9 = 0% (1 / 60852)
10-19 = 0% (3 / 123594)
20-29 = 0% (2 / 94353)
30-39 = 0.01% (9 / 129027)
40-49 = 0.02% (32 / 136233)
50-59 = 0.1% (90 / 86693)
60-69 = 0.59% (304 / 51177)
70-79 = 2.37% (729 / 30714)
80-89 = 7.07% (698 / 9873)
90+ = 12.79% (250 / 1955)
total = 0.29% (2118 / 724471)
avg = 76.9 years

[Female]
0-9 = 0% (1 / 56460)
10-19 = 0% (0 / 120568)
20-29 = 0% (1 / 99853)
30-39 = 0% (6 / 138373)
40-49 = 0.01% (18 / 157888)
50-59 = 0.06% (63 / 106593)
60-69 = 0.3% (175 / 59274)
70-79 = 1.29% (477 / 36927)
80-89 = 4.89% (694 / 14194)
90+ = 9.42% (378 / 4011)
total = 0.23% (1813 / 794141)
avg = 80.3 years

[Both]
0-9 = 0% (2 / 117312)
10-19 = 0% (3 / 244162)
20-29 = 0% (3 / 194206)
30-39 = 0.01% (15 / 267400)
40-49 = 0.02% (50 / 294121)
50-59 = 0.08% (153 / 193286)
60-69 = 0.43% (479 / 110451)
70-79 = 1.78% (1206 / 67641)
80-89 = 5.78% (1392 / 24067)
90+ = 10.53% (628 / 5966)
total = 0.26% (3931 / 1518612)
avg = 78.5 years

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment