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>
10 For production (minified) code, use:
11 <script type=
"text/javascript" src=
"dygraph-combined.js"></script>
13 <script type=
"text/javascript" src=
"../dygraph-dev.js"></script>
15 <style type=
"text/css">
16 body { max-width:
640 px; }
20 <h2>Linear Regression Demo
</h2>
21 <p>Click the buttons to generate linear regressions over either data
22 series. If you zoom in and click the regression button, the regression will
23 only be run over visible points. Zoom back out to see what the local
24 regression looks like over the full data.
</p>
26 <div id=
"demodiv" style=
"width: 480px; height: 320px;"></div>
28 <script type=
"text/javascript">
30 for (var i =
0; i <
120; i++) {
32 i /
5.0 +
10.0 * Math.sin(i /
3.0),
33 30.0 - i /
5.0 -
10.0 * Math.sin(i /
3.0 +
1.0)]);
35 var labels = ['X', 'Y1', 'Y2'];
39 document.getElementById(
"demodiv"),
45 drawCallback: function(g, is_initial) {
46 if (!is_initial) return;
47 var c = g.getColors();
48 for (var i =
0; i < c.length; i++) orig_colors.push(c[i]);
53 // coefficients of regression for each series.
54 // if coeffs = [ null, [
1,
2], null ] then we draw a regression for series
1
55 // only. The regression line is y =
1 +
2 * x.
56 var coeffs = [ null, null, null ];
57 function regression(series) {
58 // Only run the regression over visible points.
59 var range = g.xAxisRange();
61 var sum_xy =
0.0, sum_x =
0.0, sum_y =
0.0, sum_x2 =
0.0, num =
0;
62 for (var i =
0; i < data.length; i++) {
64 if (x < range[
0] || x
> range[
1]) continue;
66 var y = data[i][series];
67 if (y == null) continue;
80 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
81 var b = (sum_y - a * sum_x) / num;
83 coeffs[series] = [b, a];
84 if (typeof(console) != 'undefined') {
85 console.log(
"coeffs(" + series +
"): [" + b +
", " + a +
"]");
91 function updateChart() {
92 // Generate a new data set with the regression lines.
96 for (var i =
0; i < labels.length; i++) {
97 new_labels.push(labels[i]);
98 if (i) new_colors.push(orig_colors[i -
1]);
100 // Darken the series by
50% to generate its regression.
101 var label = labels[i] +
" Regression";
102 new_labels.push(label);
103 var c = new RGBColorParser(orig_colors[i -
1]);
104 c.r = Math.floor(
255 -
0.5 * (
255 - c.r));
105 c.g = Math.floor(
255 -
0.5 * (
255 - c.g));
106 c.b = Math.floor(
255 -
0.5 * (
255 - c.b));
107 new_colors.push(c.toHex());
116 for (var i =
0; i < data.length; i++) {
118 for (var j =
0; j < data[i].length; j++) {
119 new_data[i].push(data[i][j]);
121 new_data[i].push(coeffs[j][
0] + coeffs[j][
1] * data[i][
0]);
126 new_opts.file = new_data;
127 new_opts.labels = new_labels;
128 new_opts.colors = new_colors;
129 g.updateOptions(new_opts);
132 function clearLines() {
133 for (var i =
0; i < coeffs.length; i++) coeffs[i] = null;
139 <div style=
"position: absolute; left: 500px; top: 150px;">
140 <input type=button
style=
"color: green;" value=
"Regression (Y1)" onClick=
"regression(1)" />
142 <input type=button
style=
"color: blue;" value=
"Regression (Y2)" onClick=
"regression(2)" />
144 <input type=button
value=
"Clear Lines" onClick=
"clearLines()" />