3 <title>Linear Regression
</title>
5 <script type=
"text/javascript" src=
"excanvas.js"></script>
7 <script type=
"text/javascript" src=
"../strftime/strftime-min.js"></script>
8 <script type=
"text/javascript" src=
"../rgbcolor/rgbcolor.js"></script>
9 <script type=
"text/javascript" src=
"../dygraph-canvas.js"></script>
10 <script type=
"text/javascript" src=
"../dygraph.js"></script>
11 <style type=
"text/css">
12 body { max-width:
640 px; }
16 <h2>Linear Regression Demo
</h2>
17 <p>Click the buttons to generate linear regressions over either data
18 series. If you zoom in and click the regression button, the regression will
19 only be run over visible points. Zoom back out to see what the local
20 regression looks like over the full data.
</p>
22 <div id=
"demodiv" style=
"width: 480px; height: 320px;"></div>
24 <script type=
"text/javascript">
26 for (var i =
0; i <
120; i++) {
28 i /
5.0 +
10.0 * Math.sin(i /
3.0),
29 30.0 - i /
5.0 -
10.0 * Math.sin(i /
3.0 +
1.0)]);
31 var labels = ['X', 'Y1', 'Y2'];
35 document.getElementById(
"demodiv"),
40 drawCallback: function(g, is_initial) {
41 if (!is_initial) return;
42 var c = g.getColors();
43 for (var i =
0; i < c.length; i++) orig_colors.push(c[i]);
48 // coefficients of regression for each series.
49 // if coeffs = [ null, [
1,
2], null ] then we draw a regression for series
1
50 // only. The regression line is y =
1 +
2 * x.
51 var coeffs = [ null, null, null ];
52 function regression(series) {
53 // Only run the regression over visible points.
54 var range = g.xAxisRange();
56 var sum_xy =
0.0, sum_x =
0.0, sum_y =
0.0, sum_x2 =
0.0, num =
0;
57 for (var i =
0; i < data.length; i++) {
59 if (x < range[
0] || x
> range[
1]) continue;
61 var y = data[i][series];
62 if (y == null) continue;
71 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
72 var b = (sum_y - a * sum_x) / num;
74 coeffs[series] = [b, a];
76 console.log(
"coeffs(" + series +
"): [" + b +
", " + a +
"]");
82 function updateChart() {
83 // Generate a new data set with the regression lines.
86 for (var i =
0; i < labels.length; i++) {
87 new_labels.push(labels[i]);
88 if (i) new_colors.push(orig_colors[i -
1]);
90 // Darken the series by
50% to generate its regression.
91 new_labels.push(labels[i] +
" Regression");
92 var c = new RGBColor(orig_colors[i -
1]);
93 c.r = Math.floor(
255 -
0.5 * (
255 - c.r));
94 c.g = Math.floor(
255 -
0.5 * (
255 - c.g));
95 c.b = Math.floor(
255 -
0.5 * (
255 - c.b));
96 new_colors.push(c.toHex());
101 for (var i =
0; i < data.length; i++) {
103 for (var j =
0; j < data[i].length; j++) {
104 new_data[i].push(data[i][j]);
106 new_data[i].push(coeffs[j][
0] + coeffs[j][
1] * data[i][
0]);
111 // TODO(danvk): set colors intelligently.
120 function clearLines() {
121 for (var i =
0; i < coeffs.length; i++) coeffs[i] = null;
125 // function drawLines(ctx, area, layout) {
126 // if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
128 // var range = g.xAxisRange();
129 // for (var i =
0; i < coeffs.length; i++) {
130 // if (!coeffs[i]) continue;
131 // var a = coeffs[i][
1];
132 // var b = coeffs[i][
0];
134 // var x1 = range[
0];
135 // var y1 = a * x1 + b;
136 // var x2 = range[
1];
137 // var y2 = a * x2 + b;
139 // var p1 = g.toDomCoords(x1, y1);
140 // var p2 = g.toDomCoords(x2, y2);
142 // var color = g.getColors()[i -
1];
144 // ctx.strokeStyle = color;
145 // ctx.lineWidth =
1.0;
147 // ctx.moveTo(p1[
0], p1[
1]);
148 // ctx.lineTo(p2[
0], p2[
1]);
157 <div style=
"position: absolute; left: 500px; top: 150px;">
158 <input type=button
style=
"color: green;" value=
"Regression (Y1)" onClick=
"regression(1)" />
160 <input type=button
style=
"color: blue;" value=
"Regression (Y2)" onClick=
"regression(2)" />
162 <input type=button
value=
"Clear Lines" onClick=
"clearLines()" />