Mortality Table - Grouping and Aggregating Data
Overview
Lifetime data is a simplified dataset with no censoring, where for each record, we know the time of the event occuring.
As an example, the following dataset represents a set of records that includes the age of the individual at the time of
death.
Calculating Hazard Function
The following code demonstrates calculating hazard rate for the age 67. It iterates through the ages
1 to 100, creates an array, and then puts all records in that for which the individual was still alive
beginning at the given age. The length of each array represents the number of individuals who had
survived up to that point. Counting the number of deaths that occurred for individuals in each array then
gives the hazard rate for that age, when divided by the number of individuals in that array.
let items = [{death:40},{death:67},{death:78},{death:77},{death:62}];
let rows = [];
for(let i=1;i<100;i++){
let row = [];
rows.push(row);
for(let item of items) {
if(item.death <= i) row.push(item);
}
}
let age67 = rows[66]
let total = age67.length;
let deaths = age67.filter(p=>p.death === 67).length;
let answer= deaths/total;
Try it!