Public
Edited
May 19, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rows = 20
Insert cell
Insert cell
gun_ownership_and_death_rates = FileAttachment(
"gun_ownership_and_death_rates.csv"
).csv({ typed: true })
Insert cell
Insert cell
{
if (get_data == 1) {
let state_names_and_abbreviations = await FileAttachment(
"state_names_and_abbreviations.csv"
).csv();
let name_to_abbr = d3.rollup(
state_names_and_abbreviations,
(a) => a[0],
(o) => o.name
);

let gun_ownership_by_state = await FileAttachment(
"gun_ownership_by_state.csv"
).csv({ typed: true });

let gun_deaths_by_state = await FileAttachment(
"gun_deaths_by_state.csv"
).csv();
gun_deaths_by_state = d3.rollup(
gun_deaths_by_state.filter((o) => o.YEAR == "2021"),
(a) => a[0],
(o) => o.STATE
);

let data = gun_ownership_by_state.map(function (o) {
let abbr = name_to_abbr.get(o.state).abbr;
let death_rate = gun_deaths_by_state.get(abbr).RATE;
return {
abbr,
state: o.state,
ownership_percentage: o.gunOwnershipPercentage,
death_rate
};
});

return data;
} else {
return md`The raw data comes in a couple of different files from different sites, as described above. This code takes those raw files and generates the simplified file that powers the visualization. Hit the "Get data" button above exactly once, if you want to regenerate the data.`;
}
}
Insert cell
Insert cell
f = {
let [b, m] = regression.coef;
return (x) => m * x + b;
}
Insert cell
regression = jstat.models.ols(
gun_ownership_and_death_rates.map((o) => o.death_rate),
gun_ownership_and_death_rates.map((o) => [1, o.ownership_percentage])
)
Insert cell
jstat = require("jstat")
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