Published
Edited
Jun 24, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function getLayoutLinear() {
var shapes = [];
var s_tmpl = {
type: 'rect',
layer: 'below',
xref: 'x',
yref: 'paper',
y0: 0,
y1: 1,
opacity: 0.2,
line: { width: 0 }
};
var xs = bag.map(v => v.d);
var colors = ['#AAAAAA', '#CCCCCC'];
for (var i = 0; i < xs.length - 1; i++) {
var s = JSON.parse(JSON.stringify(s_tmpl));
s.x0 = xs[i];
s.x1 = xs[i + 1];
s.fillcolor = colors[i % 2];
shapes.push(s);
}

var annotations = [];
var txts = bag.map(v => v.mediarelease);

var ycur = 0.99;
var movepos = true;
for (var i = 0; i < xs.length; i++) {
if (ycur > 1.) {
movepos = false;
}
// if (movepos) {
// ycur -= 0.1;
// } else {
// ycur = 0.01;
// }
annotations.push({
xref: 'x',
yref: 'paper',
x: xs[i],
y: ycur,
text: stringDivider(txts[i], 40),
// width: 150,
// text: txts[i],
showarrow: false,
textangle: -90,
font: {
size: 10,
color: '#555555'
}
});
}

var layout = {
width: 900,
yaxis: {
// range: [Math.log(0.1), Math.log(200)],
title: 'cumulative number of deaths (CH)'
},
shapes: shapes,
annotations: annotations,
margin: {
b: 40,
t: 40
},
legend: {
x: 0.7,
y: 0.1
}
};

return layout;
}
Insert cell
Insert cell
Insert cell
scurve_fit_u = scurve([
lm.parameterValues[0] * 1.3,
lm.parameterValues[1],
lm.parameterValues[2]
])
Insert cell
dIdt = doy =>
(lm.parameterValues[0] * lm.parameterValues[1] * exponential(doy)) /
Math.pow(exponential(doy) + 1, 2)
Insert cell
exponential = doy =>
Math.exp(-lm.parameterValues[1] * (doy - lm.parameterValues[2]))
Insert cell
scurve2_fit = scurve2(lm2.parameterValues)
Insert cell
scurve_fit = scurve(lm.parameterValues)
Insert cell
lm2 = {
var data = raw.filter(v => v.CH > 0);
var res = ml.levenbergMarquardt(
{ x: data.map(v => v.doy), y: data.map(v => v.CH) },
scurve2,
{
damping: 1.5,
initialValues: [
data[first_death_idx].doy,
data[data.length - 1].CH,
0.2,
data[0].doy,
0.2,
data[0].doy
],
minValues: [50, 1, 1e-1, 1, 1e-1, 1],
maxIterations: 1000,
errorTolerance: 1e-3
}
);
return res;
}
Insert cell
function scurve2([t0, N, k1, t1, k2, t2]) {
var res = function(t) {
var r2 = N / (1 + Math.exp(-k2 * (t - t2)));
if (t < t0) {
r2 = N / (1 + Math.exp(-k1 * (t - t1)));
}
return r2;
};
return res;
}
Insert cell
lm = {
var data = raw.filter(v => v.CH > 0);
var res = ml.levenbergMarquardt(
{ x: data.map(v => v.doy), y: data.map(v => v.CH) },
scurve,
{
damping: 1.5,
initialValues: [5 * data[data.length - 1].CH, 0.2, data[0].doy],
minValues: [1, 1e-1, 1],
maxIterations: 1000,
errorTolerance: 1e-3
}
);
return res;
}
Insert cell
function scurve([N, k, t0]) {
return t => N / (1 + Math.exp(-k * (t - t0)));
}
Insert cell
Insert cell
lr = {
var data = raw.filter(v => v.CH > 0);
var x = data.map(v => v.doy);
var y = data.map(v => Math.log(v.CH));
var lr = new ml.SimpleLinearRegression(x, y);
return lr;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
fT(new Date())
Insert cell
fT = d3.timeFormat("%Y-%m-%d")
Insert cell
fd0 = d3.format(".0f")
Insert cell
fd1 = d3.format(".1f")
Insert cell
fd2 = d3.format(".2f")
Insert cell
Insert cell
Insert cell
{
Date.prototype.isLeapYear = function() {
var year = this.getFullYear();
if ((year & 3) != 0) return false;
return year % 100 != 0 || year % 400 == 0;
};

// Get Day of Year
Date.prototype.getDOY = function() {
var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var mn = this.getMonth();
var dn = this.getDate();
var dayOfYear = dayCount[mn] + dn;
if (mn > 1 && this.isLeapYear()) dayOfYear++;
return dayOfYear;
};
}
Insert cell
Insert cell
Insert cell
ju = require(URL.createObjectURL(jsutil_fa))
Insert cell
jsutil_fa = FileAttachment("jsutil.js").blob()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more