4 <meta http-equiv=
"X-UA-Compatible" content=
"IE=EmulateIE7; IE=EmulateIE9">
5 <title>Linear Regression
</title>
7 <script type=
"text/javascript" src=
"../excanvas.js"></script>
9 <script type=
"text/javascript" src=
"../strftime/strftime-min.js"></script>
10 <script type=
"text/javascript" src=
"../rgbcolor/rgbcolor.js"></script>
11 <script type=
"text/javascript" src=
"../dygraph-canvas.js"></script>
12 <script type=
"text/javascript" src=
"../dygraph.js"></script>
13 <style type=
"text/css">
14 body { max-width:
640 px; }
18 <h2>Linear Regression Demo
</h2>
19 <p>Click the buttons to generate linear regressions over either data
20 series. If you zoom in and click the regression button, the regression will
21 only be run over visible points. Zoom back out to see what the local
22 regression looks like over the full data.
</p>
24 <div id=
"demodiv" style=
"width: 480px; height: 320px;"></div>
26 <script type=
"text/javascript">
28 for (var i =
0; i <
120; i++) {
30 i /
5.0 +
10.0 * Math.sin(i /
3.0),
31 30.0 - i /
5.0 -
10.0 * Math.sin(i /
3.0 +
1.0)]);
33 var labels = ['X', 'Y1', 'Y2'];
37 document.getElementById(
"demodiv"),
43 drawCallback: function(g, is_initial) {
44 if (!is_initial) return;
45 var c = g.getColors();
46 for (var i =
0; i < c.length; i++) orig_colors.push(c[i]);
51 // coefficients of regression for each series.
52 // if coeffs = [ null, [
1,
2], null ] then we draw a regression for series
1
53 // only. The regression line is y =
1 +
2 * x.
54 var coeffs = [ null, null, null ];
55 function regression(series) {
56 // Only run the regression over visible points.
57 var range = g.xAxisRange();
59 var sum_xy =
0.0, sum_x =
0.0, sum_y =
0.0, sum_x2 =
0.0, num =
0;
60 for (var i =
0; i < data.length; i++) {
62 if (x < range[
0] || x
> range[
1]) continue;
64 var y = data[i][series];
65 if (y == null) continue;
78 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
79 var b = (sum_y - a * sum_x) / num;
81 coeffs[series] = [b, a];
82 if (typeof(console) != 'undefined') {
83 console.log(
"coeffs(" + series +
"): [" + b +
", " + a +
"]");
89 function updateChart() {
90 // Generate a new data set with the regression lines.
94 for (var i =
0; i < labels.length; i++) {
95 new_labels.push(labels[i]);
96 if (i) new_colors.push(orig_colors[i -
1]);
98 // Darken the series by
50% to generate its regression.
99 var label = labels[i] +
" Regression";
100 new_labels.push(label);
101 var c = new RGBColor(orig_colors[i -
1]);
102 c.r = Math.floor(
255 -
0.5 * (
255 - c.r));
103 c.g = Math.floor(
255 -
0.5 * (
255 - c.g));
104 c.b = Math.floor(
255 -
0.5 * (
255 - c.b));
105 new_colors.push(c.toHex());
114 for (var i =
0; i < data.length; i++) {
116 for (var j =
0; j < data[i].length; j++) {
117 new_data[i].push(data[i][j]);
119 new_data[i].push(coeffs[j][
0] + coeffs[j][
1] * data[i][
0]);
124 new_opts.file = new_data;
125 new_opts.labels = new_labels;
126 new_opts.colors = new_colors;
127 g.updateOptions(new_opts);
130 function clearLines() {
131 for (var i =
0; i < coeffs.length; i++) coeffs[i] = null;
137 <div style=
"position: absolute; left: 500px; top: 150px;">
138 <input type=button
style=
"color: green;" value=
"Regression (Y1)" onClick=
"regression(1)" />
140 <input type=button
style=
"color: blue;" value=
"Regression (Y2)" onClick=
"regression(2)" />
142 <input type=button
value=
"Clear Lines" onClick=
"clearLines()" />