Public
Edited
Apr 24, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
theplot = {

const margin = {top: 10, right: 20, bottom: 20, left: 50};

const svg = d3.select(DOM.svg(width + margin.left + margin.right, height + margin.top + margin.bottom))
const g = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

const xScale = d3.scaleLinear().domain(xdomain).range([0, width - margin.right]).nice();
const yScale = d3.scaleLinear().domain(ydomain).range([height - margin.bottom, margin.top]).nice();

g.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale))
.append('text')
.text("X")
.attr("fill", "black")
.style("font-size", "11pt")
.attr('x', (width - margin.right) / 2)
.attr('y', margin.bottom + 15);

g.append("g")
.call(d3.axisLeft(yScale))
.attr("class", "y-axis")
.append('text')
.text("Y")
.attr("fill", "black")
.style("font-size", "11pt")
.attr('x', -margin.left / 2)
.attr('y', margin.top);

// Setup clipping path
const clipID = "plot-area-clip";

svg.append("defs")
.append("clipPath")
.attr("id", clipID)
.append("rect")
.attr("width", width)
.attr("height", height - margin.bottom)
.attr("x", 0)
.attr("y", 0);

const plotAreaGroup = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`)
.attr("clip-path", `url(#${clipID})`);
if (showdgp) {
for (let i = 0; i < regcoefs.length; i++) {
let thecol = regcoefs.map(d => d.col)[i]
let xRange = [ regcoefs.map(d => d.GMX)[i] - 1, regcoefs.map(d => d.GMX)[i] + 1];
let data = d3.range(xRange[0] - 3, xRange[1] + 3).map(function(x) {
return { x: x, y: -1 * x + regcoefs.map(d => d.alpha)[i]};
});

// Add model line for true DGP
plotAreaGroup
.datum(data)
.append("path")
.attr("fill", "none")
.attr("stroke", thecol)
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.x(function(d) { return xScale(d.x); })
.y(function(d) { return yScale(d.y); })
);
}
}

if (regtype.includes("Pooled")) {
let xRange = xdomain;
let data = d3.range(xRange[0], xRange[1]).map(function(x) {
return { x: x, y: regcoefs_pooled[0].pooledbeta * x + regcoefs_pooled[0].pooledalpha };
});
plotAreaGroup.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 1.5)
.attr("stroke-dasharray", "5,5")
.attr("d", d3.line()
.x(function(d) { return xScale(d.x); })
.y(function(d) { return yScale(d.y); })
);
}
if (regtype.includes("Fixed Effects (LSDV)")) {
for (let i = 0; i < regcoefs_FE.length; i++) {
let thecol = regcoefs.map(d => d.col)[i]
let xRange = [ regcoefs.map(d => d.GMX)[i] - 1, regcoefs.map(d => d.GMX)[i] + 1];
let dataFE = d3.range(xRange[0] - 3, xRange[1] + 3).map(function(x) {
return { x: x, y: regcoefs_FE.map(d => d.FEbeta)[i] * x + regcoefs_FE.map(d => d.FEalpha)[i]};
});
plotAreaGroup.append("path")
.datum(dataFE)
.attr("fill", "none")
.attr("stroke", thecol)
.attr("stroke-width", 1.5)
.attr("stroke-dasharray", "5,5")
.attr("d", d3.line()
.x(function(d) { return xScale(d.x); })
.y(function(d) { return yScale(d.y); })
);
}
}
if (regtype.includes("Random Effects")) {
let xRange = xdomain;
let dataRE = d3.range(xRange[0] - 1, xRange[1] + 1).map(function(x) {
return { x: x, y: regcoefs_RE[0].REbeta * x + regcoefs_RE[0].REalpha };
});
plotAreaGroup.append("path")
.datum(dataRE)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2)
.attr("stroke-dasharray", "5,10")
.attr("d", d3.line()
.x(function(d) { return xScale(d.x); })
.y(function(d) { return yScale(d.y); })
);
}
// Draw the sample
plotAreaGroup.selectAll(".point")
.data(paneldata)
.enter().append("circle")
.attr("class", "point")
.attr("r", 3)
.attr("cx", d => xScale(d.X))
.attr("cy", d => yScale(d.Y))
.attr("fill", !showgroups ? "black" : d => d.col);
return svg.node();
}
Insert cell
ydomain = [-1, Math.max(...paneldata.map(d => d.Y)) + 1]
Insert cell
xdomain = [-1, Math.max(...paneldata.map(d => d.X)) + 1]
Insert cell
height = 400
Insert cell
width = 650
Insert cell
import {d3} from "https://d3js.org/d3.v7.min.js"
Insert cell
paneldata@5.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
paneldata
Insert cell
regcoefs = Array.from(
d3.rollup(
paneldata,
([first]) => ({ GMX: first.GMX, col: first.col, alpha: first.alpha, beta: first.beta }),
d => `${d.alpha}_${d.beta}`
),
([, value]) => value
);
Insert cell
regcoefs_pooled = Array.from(
d3.rollup(
paneldata,
([first]) => ({ pooledalpha: first.pooledalpha, pooledbeta: first.pooledbeta }),
d => `${d.pooledalpha}`
),
([, value]) => value
);
Insert cell
regcoefs_FE = Array.from(
d3.rollup(
paneldata,
([first]) => ({ FEalpha: first.alpha, FEbeta: first.FEbeta }),
d => `${d.FEalpha}_${d.FEbeta}`
),
([, value]) => value
);
Insert cell
regcoefs_RE = Array.from(
d3.rollup(
paneldata,
([first]) => ({ REalpha: first.REalpha, REbeta: first.REbeta }),
d => `${d.REalpha}`
),
([, value]) => value
);
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