Public
Edited
Apr 30, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
currentData = dataAt(year)
Insert cell
x = d3.scaleLog([2, 1e5], [margin.left, width - margin.right])
Insert cell
fy = function(range){return d3.scaleLinear(range, [height - margin.bottom, margin.top])}
Insert cell
radius = d3.scaleSqrt([0, 5e8], [0, width / 30])
Insert cell
colorscale = d3.scaleOrdinal([1, 2, 3, 4], d3.schemeCategory10)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80, ","))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text("Government revenue per capita (USD) →"))
Insert cell
fyAxis = function(ay, annotation){
return g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(ay))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(annotation))}
Insert cell
fgrid = function(ay){
return g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(ay.ticks())
.join("line")
.attr("y1", d => 0.5 + ay(d))
.attr("y2", d => 0.5 + ay(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));}
Insert cell
function labelText(d, quantity){
return [d.name, "population: " + d3.format(",.9r")(d.population),
"gov. rev. per cap. : $" + d3.format(",.5r")(d.govRevCap),
quantity + ": " + d[quantity].toFixed(2)].join("\n");
}
Insert cell
margin = ({top: 20, right: 20, bottom: 35, left: 40})
Insert cell
height = 560
Insert cell
d3 = require("d3@5")
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
data = d3.csvParse(await FileAttachment("lives saved data.csv").text())
Insert cell
countriesnested = d3.nest()
.key(function(d) { return d.YEAR; })
.entries(data);
Insert cell
countries = d3.nest()
.key(function(d) { return d.COUNTRY; })
.entries(data)
.map(d => d.key)
//.filter(d => d.length > 0)
.sort()
Insert cell
mapped = countriesnested.map( function(d){
var obd = {year : +d.key,
data : d.values.map( function(e){
var obe = {
year : +d.key,
name: e.COUNTRY,
iso : e.ISO,
id : +e.ID,
economy : +e.INCOME,
population : +e.POPULATION,
govRevCap : +e.GOVREVPERCAP,
u5deaths : +e.U5DEATHS,
u5Pop : +e.U5POP,
births : +e.BIRTHS,
matdeaths : +e.MATDEATHS,
mortality : +e.U5MORTALITY,
matMortality : +e.MATMORTALITY}
return obe;
})}
return obd;
; })
Insert cell
function recomputedata(d){
var e = { ...d }
var computed = computeResult(e, "mortality");
e.mortality = computed[0];
var computed2 = computeResult(e, "matMortality");
e.matmortality = computed2[0];
var computedRev = getRevenue(d);
e.govRevCap = computedRev[3];
return e;
}
Insert cell
dataAt = function(year){
var res = mapped.filter(function(d) {return d.year == year})
if (res.length > 0)
{
var data2 = res[0].data.map(function(d){return recomputedata(d);})
return data2;
}
}
Insert cell
test = dataAt(1999)[20]
Insert cell
computeResult(test, "mortality")
Insert cell
function getRevenue(d)
{
var newAbsRev = (d.govRevCap * (govRevenue)) * d.population;
var additionalPerCapita = d.govRevCap * govRevenue;
var newPerCapita = d.govRevCap + additionalPerCapita;
return [govRevenue, newAbsRev, additionalPerCapita, newPerCapita];
}
Insert cell
betaU5M = 1472;
Insert cell
betaMMR = 576.6;
Insert cell
function computeResult(d, type)
{
// returns [improved outome, lives saved, original outome]

var computedRevenue = getRevenue(d);

var increase = computedRevenue[0];

if (type == "mortality")
{
if (d.mortality > 0)
{
var effectOnU5m = 0;
if (d.govRevCap > 0 )
{
effectOnU5m = Math.min(betaU5M / d.govRevCap * increase, d.mortality)
}
var livesSaved = effectOnU5m * d.u5Pop / 1000;
var u5m = d.mortality - effectOnU5m;
return [u5m, d.govRevCap > 0 ? livesSaved : NaN, +d.mortality];
}
else
return [NaN, NaN, NaN];

}
else if (type == "matMortality" )
{
if (d.matMortality > 0)
{
var effectOnMmr = 0;
if ( d.govRevCap > 0 )
{
effectOnMmr = Math.min(betaMMR / Math.pow(Math.log(d.govRevCap), 2.0) * increase, d.matMortality);
}
var livesSaved = effectOnMmr * d.BIRTHS / 100000;
var mmr = d.matMortality - effectOnMmr;
return [mmr, d.govRevCap > 0 ? livesSaved : NaN, +d.matMortality];
}
else
{
return [NaN, NaN, NaN]
}
}
else if(type == "PScompletion")
{

}
else if(type == "Immunisation")
{

}
}
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