Overview
This example utilizes the following dataset.
copy
The cases column represents the number of observed cancer cases in the given region.
Transforming the Data
The first step is to transform the data to use One Hot Encoding
let data2 = data.map(p=>{
let record = {
u1:0,
u2:0,
u3:0,
u4:0,
u5:0,
u6:0,
u7:0,
u8:0,
e:0
};
record.population = p.population;
record.cases = p.cases;
if(p.age === '15-24') record.u1 = 1;
if(p.age === '25-34') record.u2 = 1;
if(p.age === '35-44') record.u3 = 1;
if(p.age === '45-54') record.u4 = 1;
if(p.age === '55-64') record.u5 = 1;
if(p.age === '65-74') record.u6 = 1;
if(p.age === '75-84') record.u7 = 1;
if(p.age === '85+') record.u8 = 1;
if(p.city === 'Lemur City') record.e=1;
return record;
});
Maximum Likelihood
Next, we assume that the mean parameter to the Poisson distribution can be modeled as
{% \mu_t = population \times exp[\beta_0 + \beta_1 u_1 + \beta_2 u_2 + ... + \beta_7 u_7] %}
Then we use
newtons algorithm
is used to find the
maximum likelihood
values of {% \beta_0,...,\beta_7 %}
let op = await import('/lib/numeric/optimization/v1.0.0/newton.mjs');
let f = function(b0, b1, b2, b3, b4, b5, b6, b7, b9){
let logL = pl.logLikelihood(data2, item=>item.cases, item=>{
let eta = b0+b1*item.u1 + b2*item.u2 + b3*item.u3 + b4*item.u4 + b5*item.u5 + b6*item.u6 + b7*item.u7 + b9*item.e;
return item.population * Math.exp(eta);
});
return -1*logL;
}
let iterator = op.iterator(f, [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1],{
speed:op.linesearch.bisection(f)
});
for(let i=0;i<30;i++){
iterator.iterate();
}
let result = iterator.x;
Poisson Regression
The above maximum Likelihood code is encapsulated in the Poisson Regression Library.
let op = await import('/lib/glm/v1.0.0/poisson.mjs');
let iterator = pl.newton(data2,'cases', ['u1','u2','u3','u4','u5','u6','u7','e'],{
exposure:'population',
linesearch:'bisection'
});
for(let i=0;i<30;i++){
iterator.iterate();
}
let result = iterator.x;