viewof n_1 = Inputs.range([10, 1000], {value: 100, step: 1, label: tex`\text{sample size, }n`});
viewof beta0_1 = Inputs.range([-2, 2], {value: -1, step: 0.1, label: tex`\text{intercept, }\beta_0`})
viewof beta1_1 = Inputs.range([-2, 2], {value: 1, step: 0.1, label: tex`\text{slope, }\beta_1`})
viewof sigmaepsilon_1 = Inputs.range([0, 1], {value: 0.5, step: 0.1, label: tex`\text{sd of error, }\sigma`})Sampling Distribution of Beta 0 and Beta 1 of Simple Linear Regression
function simulate_and_estimate(n, beta0, beta1, sigmaepsilon, nrep){
const x = d3.range(-1, 1, 2/n);
const xbar = d3.mean(x)
const SSx = d3.sum(math.dotPow(math.subtract(x, xbar), 2))
var data = [];
var coef = [];
for (let j = 1; j <= nrep; j++){
var dataset = x.map(x => ({sample_id: j, x: x, y: beta0 + beta1*x + jstat.normal.sample(0,sigmaepsilon)}))
data.push(dataset);
let y = dataset.map(d => d.y)
let ybar = d3.mean(y)
let SSxy = d3.sum(math.dotMultiply(math.subtract(x, xbar), math.subtract(y, ybar)) )
let b1 = SSxy/SSx
let b0 = ybar - b1*xbar
//let linear_model = lm('y ~ x', dataset);
coef.push(
{
sample_id: j,
//intercept: linear_model.coefficients[0],
//lutning: linear_model.coefficients[1],
intercept: b0,
lutning: b1
}
)
}
return [data, coef]
}b0samp_range = [d3.min(data_estimates_1[1].map( d => d.intercept)),d3.max(data_estimates_1[1].map( d => d.intercept))]
b1samp_range = [d3.min(data_estimates_1[1].map( d => d.lutning)),d3.max(data_estimates_1[1].map( d => d.lutning))]
sampdist_theo = {
const x = d3.range(-1, 1, 2/n_1);
const xbar = d3.mean(x)
const sumsquares = d3.sum(math.dotPow(math.subtract(x, xbar), 2))
const stdb0 = sigmaepsilon_1*math.sqrt((1/n_1) + ((xbar**2)/sumsquares))
const stdb1 = sigmaepsilon_1/math.sqrt(sumsquares)
const x_b0 = d3.range(beta0_1 - 4*stdb0, beta0_1 + 4*stdb0, 1/500);
const x_b1 = d3.range(beta1_1 - 4*stdb1, beta1_1 + 4*stdb1, 1/500);
var pdfb0 = [];
var pdfb1 = [];
for (let j = 1; j <= x_b0.length; j++){
pdfb0.push({
x: x_b0[j-1],
pdf: jstat.normal.pdf(x_b0[j-1], beta0_1, stdb0)
})
}
for (let j = 1; j <= x_b1.length; j++){
pdfb1.push({
x: x_b1[j-1],
pdf: jstat.normal.pdf(x_b1[j-1], beta1_1, stdb1),
})
}
return [pdfb0, pdfb1]
}histintercept = Plot.plot({
width: 400, // or a dynamic value based on `width` variable
height: 300,
style: {fontSize: "16px"},
caption:html`
<span style="color:midnightblue">Sampling distribution for b<sub>0</sub> </span> <br>
<span style="color:steelblue">Histogram of estimates</span><br>
🟠 Population β<sub>0</sub> <br>
🔵 Estimated b<sub>0</sub> for the current data.
`,
y: {axis: false},
x: {domain: [beta0_1 -0.5, beta0_1 + 0.5]},
marks: [
Plot.rectY(data_estimates_1[1].filter((d,i) => i<= (sampleno_1-1)), Plot.binX({y: d => (d.length/sampleno_1)/(10/sampleno_1)}, {x: "intercept", thresholds: d3.range(b0samp_range[0], b0samp_range[1], 10/sampleno_1), fill: "steelblue", opacity: 0.5}) ),
Plot.ruleY([0]),
Plot.dot([{beta0: beta0_1}], {x: "beta0", y: 0, r: 5, fill: "orange"}),
Plot.dot([data_estimates_1[1][sampleno_1-1]], {x: "intercept", y: 0, r: 5, fill: "steelblue"}),
Plot.lineY(sampdist_theo[0], {x: "x", y: "pdf", stroke: "midnightblue", strokeWidth: 2.5})
],
})histlutning = Plot.plot({
width: 400, // or a dynamic value based on `width` variable
height: 300,
style: {fontSize: "16px"},
caption:html`
<span style="color:midnightblue">Sampling distribution for b<sub>1</sub></span> <br>
<span style="color:steelblue">Histogram of estimates</span><br>
🟠 Population β<sub>1</sub> <br>
🔵 Estimated b<sub>1</sub> for the current data.
`,
y: {axis: false},
x: {domain: [beta1_1 -0.5, beta1_1 + 0.5]},
marks: [
Plot.rectY(data_estimates_1[1].filter((d,i) => i<= (sampleno_1-1)), Plot.binX({y: d => (d.length/sampleno_1)/(10/sampleno_1)}, {x: "lutning", thresholds: d3.range(b1samp_range[0], b1samp_range[1], 10/sampleno_1), fill: "steelblue", opacity: 0.5}) ),
Plot.ruleY([0]),
Plot.dot([{beta1: beta1_1}], {x: "beta1", y: 0, r: 5, fill: "orange"}),
Plot.dot([data_estimates_1[1][sampleno_1-1]], {x: "lutning", y: 0, r: 5, fill: "steelblue"}),
Plot.lineY(sampdist_theo[1], {x: "x", y: "pdf", stroke: "midnightblue", strokeWidth: 2.5})
],
})