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">
25 var data =
"X,Y1,Y2\n";
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)]);
30 data += i +
"," + i +
"/120," + (i*(
120-i)) +
"/" + (
60*
60) +
"\n";
34 document.getElementById(
"demodiv"),
37 underlayCallback: drawLines,
45 // coefficients of regression for each series.
46 // if coeffs = [ null, [
1,
2], null ] then we draw a regression for series
1
47 // only. The regression line is y =
1 +
2 * x.
48 var coeffs = [ null, null, null ];
49 function regression(series) {
50 // Only run the regression over visible points.
51 var range = g.xAxisRange();
53 var sum_xy =
0.0, sum_x =
0.0, sum_y =
0.0, sum_x2 =
0.0, num =
0;
54 for (var i =
0; i < g.numRows(); i++) {
55 var x = g.getValue(i,
0);
56 if (x < range[
0] || x
> range[
1]) continue;
58 var y = g.getValue(i, series);
59 if (y == null) continue;
72 var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num);
73 var b = (sum_y - a * sum_x) / num;
75 coeffs[series] = [b, a];
76 if (typeof(console) != 'undefined') {
77 console.log(
"coeffs(" + series +
"): [" + b +
", " + a +
"]");
80 g.updateOptions({}); // forces a redraw.
83 function clearLines() {
84 for (var i =
0; i < coeffs.length; i++) coeffs[i] = null;
88 function drawLines(ctx, area, layout) {
89 if (typeof(g) == 'undefined') return; // won't be set on the initial draw.
91 var range = g.xAxisRange();
92 for (var i =
0; i < coeffs.length; i++) {
93 if (!coeffs[i]) continue;
102 // Scale up to percentages, rather than fractions.
106 var p1 = g.toDomCoords(x1, y1);
107 var p2 = g.toDomCoords(x2, y2);
109 var c = new RGBColor(g.getColors()[i -
1]);
110 c.r = Math.floor(
255 -
0.5 * (
255 - c.r));
111 c.g = Math.floor(
255 -
0.5 * (
255 - c.g));
112 c.b = Math.floor(
255 -
0.5 * (
255 - c.b));
113 var color = c.toHex();
115 ctx.strokeStyle = color;
118 ctx.moveTo(p1[
0], p1[
1]);
119 ctx.lineTo(p2[
0], p2[
1]);
128 <div style=
"position: absolute; left: 500px; top: 150px;">
129 <input type=button
style=
"color: green;" value=
"Regression (Y1)" onClick=
"regression(1)" />
131 <input type=button
style=
"color: blue;" value=
"Regression (Y2)" onClick=
"regression(2)" />
133 <input type=button
value=
"Clear Lines" onClick=
"clearLines()" />