-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
152 lines (116 loc) · 3.1 KB
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(function Main() {
const plot = document.querySelector('.plot');
const equationEl = document.querySelector('.equation');
const form = document.querySelector('.form');
const rSquared = document.querySelector('.rSquared');
const plotDimensions = {
x: 400,
y: 400,
};
let regressionLine = {};
let average = {};
const sum = (a, b) => a + b;
const plotPoints = (array) => {
let points = '';
for (let i = 0; i < array.length; i++) {
const { x } = array[i];
let { y } = array[i];
// normalize y coordinate
y = plotDimensions.y - y;
points += `
<circle cx="${x}" cy="${y}" r="2" fill="#f00" />
`;
}
plot.innerHTML = points;
};
const plotRegression = (x1, x2, y1, y2) => {
// normalize y coordinate
y1 = plotDimensions.y - y1;
y2 = plotDimensions.y - y2;
plot.innerHTML += `
<line x1='${x1}' x2='${x2}' y1='${y1}' y2='${y2}' stroke='blue' />
`;
};
const calcAverageCoords = (array) => {
const total = array.reduce(
(accumulator, current) => ({
x: accumulator.x + current.x,
y: accumulator.y + current.y,
}),
{ x: 0, y: 0 },
);
const x = total.x / array.length;
const y = total.y / array.length;
return {
x,
y,
};
};
const calcRegressionLine = (array) => {
// 1. Get average of x, y
average = calcAverageCoords(array);
// 2. slope = ∑(x - meanX)(y - meanY) / ∑(x - meanX)^2
const numerator = array.map(pt => (pt.x - average.x) * (pt.y - average.y)).reduce(sum);
const denominator = array
.map((pt) => {
const diff = pt.x - average.x;
return diff * diff;
})
.reduce(sum);
const M = numerator / denominator;
// 3. y-intercept = y - M(x)
const B = average.y - average.x * M;
regressionLine = {
equation: `y = ${M}x + ${B}`,
M,
B,
};
};
const calcRSquared = (array) => {
const { M, B } = regressionLine;
const numerator = array
.map((pt) => {
const estimate = pt.x * M + B;
const diff = estimate - average.y;
return diff * diff;
})
.reduce(sum);
const denominator = array
.map((pt) => {
const diff = pt.y - average.y;
return diff * diff;
})
.reduce(sum);
const result = numerator / denominator;
rSquared.innerHTML = `
r<sup>2</sup> = ${result}
`;
};
const calcRegressionLinePoints = () => {
const { M, B, equation } = regressionLine;
const pt1 = {
x: 0,
y: 0 * M + B,
};
const pt2 = {
x: 400,
y: 400 * M + B,
};
equationEl.innerHTML = equation;
plotRegression(pt1.x, pt2.x, pt1.y, pt2.y);
};
form.addEventListener('submit', (e) => {
e.preventDefault();
let points;
try {
points = JSON.parse(e.target.elements.points.value);
} catch (err) {
window.alert(`The JSON data you entered was unable to be parsed. \n\n ${err}`);
return;
}
plotPoints(points);
calcRegressionLine(points);
calcRegressionLinePoints();
calcRSquared(points);
});
}());