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"),
41 drawCallback: function(g, is_initial) {
42 if (!is_initial) return;
43 var c = g.getColors();
44 for (var i =
0; i < c.length; i++) orig_colors.push(c[i]);
49 // coefficients of regression for each series.
50 // if coeffs = [ null, [
1,
2], null ] then we draw a regression for series
1
51 // only. The regression line is y =
1 +
2 * x.
52 var coeffs = [ null, null, null ];
53 function regression(series) {
54 // Only run the regression over visible points.
55 var range = g.xAxisRange();
57 var sum_xy =
0.0, sum_x =
0.0, sum_y =
0.0, sum_x2 =
0.0, num =
0;
58 for (var i =
0; i < data.length; i++) {
60 if (x < range[
0] || x
> range[
1]) continue;
62 var y = data[i][series];
63 if (y == null) continue;
76 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
77 var b = (sum_y - a * sum_x) / num;
79 coeffs[series] = [b, a];
80 if (typeof(console) != 'undefined') {
81 console.log(
"coeffs(" + series +
"): [" + b +
", " + a +
"]");
87 function updateChart() {
88 // Generate a new data set with the regression lines.
92 for (var i =
0; i < labels.length; i++) {
93 new_labels.push(labels[i]);
94 if (i) new_colors.push(orig_colors[i -
1]);
96 // Darken the series by
50% to generate its regression.
97 var label = labels[i] +
" Regression";
98 new_labels.push(label);
99 var c = new RGBColor(orig_colors[i -
1]);
100 c.r = Math.floor(
255 -
0.5 * (
255 - c.r));
101 c.g = Math.floor(
255 -
0.5 * (
255 - c.g));
102 c.b = Math.floor(
255 -
0.5 * (
255 - c.b));
103 new_colors.push(c.toHex());
112 for (var i =
0; i < data.length; i++) {
114 for (var j =
0; j < data[i].length; j++) {
115 new_data[i].push(data[i][j]);
117 new_data[i].push(coeffs[j][
0] + coeffs[j][
1] * data[i][
0]);
122 new_opts.file = new_data;
123 new_opts.labels = new_labels;
124 new_opts.colors = new_colors;
125 g.updateOptions(new_opts);
128 function clearLines() {
129 for (var i =
0; i < coeffs.length; i++) coeffs[i] = null;
135 <div style=
"position: absolute; left: 500px; top: 150px;">
136 <input type=button
style=
"color: green;" value=
"Regression (Y1)" onClick=
"regression(1)" />
138 <input type=button
style=
"color: blue;" value=
"Regression (Y2)" onClick=
"regression(2)" />
140 <input type=button
value=
"Clear Lines" onClick=
"clearLines()" />