Mortality Table - Grouping and Aggregating Data
Overview
INterval data is data where each record represents an interval of time for a single individual.
As an example, consider a dataste where each record represents an individual who was alive at the
stated age. A column is included indicating whether the individual died that year.
Calculating Hazard Function
The following code utilizes the
group module
to group each record into age groups. Each group can then be used to calculate the hazard rate.
let gp = await import('/lib/group/v1.0.0/group.mjs');
let items = [{age:40, death:false},{age:40, death:true},{age:40, death:false},{age:41, death:false},{age:41, death:false}];
let groups = gp.group(items, p=>p.age);
let age40 = groups(40);
let total = age40.length;
let deaths = age40.filter(p=>p.death === true).length;
let answer= deaths/total;
Try it!