Public
Edited
Nov 1, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
regression_result = regression
.regressionLinear()
.x((d) => d.gun_regulation_index)
.y((d) => d.deaths_per_100K)(data)
Insert cell
regression = require("d3-regression")
Insert cell
Insert cell
// The data stored in a CSV
data = FileAttachment("gun_regulation_index_data.csv").csv({ typed: true })
Insert cell
// The commented code below generates the data from scratch using a copied/pasted
// table from the article and an API call to the US Census Bureau.

// {
// let data = d3.csvParse(
// // This is just a copy and paste of Table 1 here:
// // https://journals.lww.com/epidem/fulltext/2023/11000/the_era_of_progress_on_gun_mortality__state_gun.3.aspx
// `state,gun_regulation_index,gun_deaths
// Alabama −1 1046 1034 - 12
// Alaska 0 177 177 0 0
// Arizona 8 1094 1205 111 -
// Arkansas 0 541 541 0 0
// California 20 3184 4833 1649 -
// Colorado 5 812 870 58 -
// Connecticut 16 172 292 120 -
// Delaware 12 111 135 24 -
// Florida 6 2704 2964 260 -
// Georgia 2 1571 1614 43 -
// Hawaii 12 66 102 36 -
// Idaho 2 242 250 8 -
// Illinois 7 1490 1689 199 -
// Indiana 3 997 1039 42 -
// Iowa 0 288 288 0 0
// Kansas −2 383 368 - 15
// Kentucky −1 772 763 - 9
// Louisiana 3 987 1013 26 -
// Maine 5 123 136 13 -
// Maryland 7 707 800 93 -
// Massachusetts 8 242 354 112 -
// Michigan 3 1230 1293 63 -
// Minnesota 2 432 451 19 -
// Mississippi 3 587 605 18 -
// Missouri −4 1144 1090 - 54
// Montana 2 194 198 4 -
// Nebraska 10 171 211 40 -
// Nevada 6 498 538 40 -
// New Hampshire 4 132 143 11 -
// New Jersey 9 485 654 169 -
// New Mexico 3 383 396 13 -
// New York 11 900 1348 448 -
// North Carolina 5 1409 1511 102 -
// North Dakota 2 90 93 3 -
// Ohio 2 1524 1573 49 -
// Oklahoma 5 766 807 41 -
// Oregon 3 513 535 22 -
// Pennsylvania 6 1555 1716 161 -
// Rhode Island 4 49 58 9 -
// South Carolina −2 891 872 - 19
// South Dakota −1 108 106 - 2
// Tennessee 0 1148 1148 0 0
// Texas 1 3353 3400 47 -
// Utah 4 370 396 26 -
// Vermont 1 78 79 1 -
// Virginia 3 1049 1106 57 -
// Washington 5 686 759 73 -
// West Virginia 5 332 350 18 -
// Wisconsin 6 664 732 68 -
// Wyoming 2 101 103 2 -`
// .replace(/ \t/g, ",")
// .replace(/−/g, "-"),
// d3.autoType
// );

// // We need to add state population data from 2016, which
// // we can get the Census API:
// let results = await (
// await fetch(
// "https://api.census.gov/data/2016/acs/acs5?get=NAME,B01003_001E&for=state"
// )
// ).json();
// let columns = results[0];
// results = results.slice(1).map(function (a) {
// return Object.fromEntries(d3.zip(columns, a));
// });
// let state_pops = d3.rollup(
// results,
// (a) => parseInt(a[0].B01003_001E),
// (o) => o.NAME
// );

// // Join the state populations to the copied data
// data.forEach(function (o) {
// let population = state_pops.get(o.state);
// o.deaths_per_100K = (100000 * o.gun_deaths) / population;
// });

// return data;
// }
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