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">
29 var data =
"X,Y1,Y2\n";
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)]);
34 data += i +
"," + i +
"/120," + (i*(
120-i)) +
"/" + (
60*
60) +
"\n";
38 document.getElementById(
"demodiv"),
41 underlayCallback: drawLines,
50 // coefficients of regression for each series.
51 // if coeffs = [ null, [
1,
2], null ] then we draw a regression for series
1
52 // only. The regression line is y =
1 +
2 * x.
53 var coeffs = [ null, null, null ];
54 function regression(series) {
55 // Only run the regression over visible points.
56 var range = g.xAxisRange();
58 var sum_xy =
0.0, sum_x =
0.0, sum_y =
0.0, sum_x2 =
0.0, num =
0;
59 for (var i =
0; i < g.numRows(); i++) {
60 var x = g.getValue(i,
0);
61 if (x < range[
0] || x
> range[
1]) continue;
63 var y = g.getValue(i, series);
64 if (y == null) continue;
77 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
78 var b = (sum_y - a * sum_x) / num;
80 coeffs[series] = [b, a];
81 if (typeof(console) != 'undefined') {
82 console.log(
"coeffs(" + series +
"): [" + b +
", " + a +
"]");
85 g.updateOptions({}); // forces a redraw.
88 function clearLines() {
89 for (var i =
0; i < coeffs.length; i++) coeffs[i] = null;
94 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
97 function drawLines(ctx, area, layout) {
98 if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
100 var range = g.xAxisRange();
101 for (var i =
0; i < coeffs.length; i++) {
102 if (!coeffs[i]) continue;
103 var a = coeffs[i][
1];
104 var b = coeffs[i][
0];
111 // Scale up to percentages, rather than fractions.
115 var p1 = g.toDomCoords(x1, y1);
116 var p2 = g.toDomCoords(x2, y2);
118 var c = Dygraph.toRGB_(g.getColors()[i -
1]);
119 c.r = Math.floor(
255 -
0.5 * (
255 - c.r));
120 c.g = Math.floor(
255 -
0.5 * (
255 - c.g));
121 c.b = Math.floor(
255 -
0.5 * (
255 - c.b));
122 var color = toHex(c);
124 ctx.strokeStyle = color;
127 ctx.moveTo(p1[
0], p1[
1]);
128 ctx.lineTo(p2[
0], p2[
1]);
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()" />