Public
Edited
Jan 9, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function percentiles(numbers) {
const sorted = d3.sort(numbers);
return d3.range(0, 101).map((q) => d3.quantileSorted(sorted, q / 100));
}
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
Insert cell
Insert cell
function averageQuantiles(fields) {
households.sort((a,b) => a.TOTEXP - b.TOTEXP)
const thresholds = [.1, .2, .3, .4, .5, .6, .7, .8, .9, .99]

const indeces = [0, ...thresholds.map(x => d3.quantileIndex(households.map(d => d.TOTEXP), x))]

const results = []
for (let i = 1; i < indeces.length; i++) {
const slice = households.slice(indeces[i - 1], indeces[i])

const data = {key: thresholds[i - 1], values: []}

let sums = {}
fields.forEach(field => {
data.values.push({ category: field, emissions: d3.mean(slice, d => d[field]) })

})

results.push(data)
}

return results
}
Insert cell
averageQuantiles(Object.keys(households[0]).slice(4, 17))
Insert cell
budgetHierarchy = {
const inputData = averageQuantiles(Object.keys(households[0]).slice(4, 17))

const root = {key: 'root', values: inputData }

return d3.hierarchy(root, d => d.values)
.sum(d => d.emissions)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cumulative = {
const cumulative = [...quantiles]
cumulative.forEach((d,i) => {
d.cumCO2 = d3.sum(quantiles.slice(0, i), d => d.CO2)
})

return cumulative

}
Insert cell
lorenzData = {

const total = d3.max(cumulative, d => d.cumCO2) / .94
return [...cumulative.map(d => ({quantile: d.quantile, cumCO2: d["cumCO2"], cumPct: d['cumCO2'] / total })), {quantile: 1, cumCO2: total, cumPct: 1}]

}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
CO2_categories_bottom99.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
d3 = require("d3@7", "d3-weighted-voronoi", "d3-voronoi-map", "d3-voronoi-treemap", 'seedrandom@2.4.3/seedrandom.min.js')
Insert cell
import {swatches} from '@d3/color-legend'
Insert cell
households.map(d => d.Total_CO2).sort((a,b) => a - b)
Insert cell
allHouseholds = [...households.map(d => d.Total_CO2).sort((a,b) => a - b), ...d3.range(128).map(d => d + 955) ]
Insert cell
{

const q = .01
households.sort((a,b) => a['Total_CO2'] - b['Total_CO2'])
const thresholds = d3.range(q, 1 + q, q).map(d => +(d).toFixed(2))

const indeces = [0, ...thresholds.map(x => d3.quantileIndex(allHouseholds, x))].slice(0, 100)


const results = []
for (let i = 1; i < indeces.length; i++) {
const slice = households.slice(indeces[i - 1], indeces[i])

const avgCO2 = d3.mean(slice, d => d.Total_CO2)
const avgExp = d3.mean(slice, d => d.TOTEXP)
const avgIncome = d3.mean(slice, d => d.FINCBTAX_M)

const goodsValues = {}
for (let key of goodsMultipliers.keys()) {
goodsValues[key] = +(d3.mean(slice, d => d[key]))?.toFixed(3)
}

const {Water, ...restProps} = goodsValues
results.push({quantile: +(i * q).toFixed(2), avgCO2: +(avgCO2).toFixed(3), avgIncome: +(avgIncome).toFixed(3), avgExp: +(avgExp).toFixed(3), ...restProps})
}

return [...results, top1]

}
Insert cell
Insert cell
getPercentile(74000 * .8623, households.map(d => d.FINCBTAX_M))
Insert cell
function getPercentile(val, data) {
const allData = [...data, ...d3.range(128).map((d) => d + 955)]
let count = 0
allData.forEach((v) => {
if (v < val) {
count++
} else if (v == val) {
count += 0.5
}
})
return (100 * count) / data.length
}
Insert cell
households.length / 100
Insert cell
d3.range(127)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more