Public
Edited
Jul 10, 2023
Insert cell
Insert cell
Insert cell
Plot.plot({
y: { label: "proportion of elementary schools with security officers" },
marks: [Plot.barY(es_rollup, { x: "network", y: "proportion" })]
})
Insert cell
Insert cell
Plot.plot({
marginLeft: 100,
marginRight: 100,
y: {
grid: true
},
marks: [
Plot.boxX(hs_data, {
y: (d) => (d.security_guards > 0 ? "security" : "no security"),
x: "StudentCount",
fy: "network"
})
]
})
Insert cell
es_rollup = d3
.flatRollup(
raw_data.filter(
(d) => d.network.includes("Network") && d.primary_category === "ES"
),
(v) => d3.mean(v, (d) => d.security_guards > 0),
(d) => d.network
)
.map(([network, proportion]) => ({
network: Number(network.split(" ")[1]),
proportion
}))
.filter((d) => d.network < 14)
Insert cell
hs_data = raw_data.filter(
(d) => d.network.includes("Network") && d.primary_category === "HS"
)
Insert cell
Schools With Security Guards - School profiles and guard count.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
data_for_analysis = raw_data
.map((d) => ({
...d,
has_security_guards: Number(d.security_guards > 0),
log_count: Math.log(d.StudentCount) - Math.log(1000)
}))
.map((r) => {
const factor = "primary_category";
// get uniqe factor levels
const factorLevels = [...new Set(raw_data.map((r) => r[factor]))];
const sortedLevels = factorLevels.sort();
// for each factor level, add a new column to our dataframe
// e.g. race.f1, race.f2 ...
sortedLevels.forEach(
(level) => (r[factor + ".f" + level] = r[factor] === level ? 1 : 0)
);
return r;
})
.map((r) => {
const factor = "network";
// get uniqe factor levels
const factorLevels = [...new Set(raw_data.map((r) => r[factor]))];
const sortedLevels = factorLevels.sort();
// for each factor level, add a new column to our dataframe
// e.g. race.f1, race.f2 ...
sortedLevels.forEach(
(level) => (r[factor + ".f" + level] = r[factor] === level ? 1 : 0)
);
return r;
})
Insert cell
d3.mean(raw_data.map((d) => d.StudentCount))
Insert cell
import { lm, summary } from "@chrispahm/linear-models-in-observable-notebooks"
Insert cell
model = lm(
"has_security_guards ~ primary_category.fHS + log_count*primary_category.fHS + network.fNetwork 1 + network.fNetwork 3 + network.fNetwork 4 + network.fNetwork 5 + network.fNetwork 6 + network.fNetwork 7 + network.fNetwork 8 + network.fNetwork 9 + network.fNetwork 10 + network.fNetwork 11 + network.fNetwork 12 + network.fNetwork 13 + network.fNetwork 14 + network.fNetwork 15 + network.fNetwork 16 + network.fNetwork 17ss",
data_for_analysis.filter(
(d) =>
d.governance === "District" &&
d.primary_category !== "MS" &&
d.network.includes("Network")
)
)
Insert cell
summary(model)
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