Commit | Line | Data |
---|---|---|
05a1d116 DV |
1 | <html> |
2 | <head> | |
3 | <title>Linear Regression</title> | |
4 | <!--[if IE]> | |
5 | <script type="text/javascript" src="excanvas.js"></script> | |
6 | <![endif]--> | |
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; } | |
13 | </style> | |
14 | </head> | |
15 | <body> | |
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> | |
21 | ||
22 | <div id="demodiv" style="width: 480px; height: 320px;"></div> | |
23 | ||
24 | <script type="text/javascript"> | |
25 | var data = []; | |
26 | for (var i = 0; i < 120; i++) { | |
27 | data.push([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 | } | |
31 | ||
32 | g = new Dygraph( | |
33 | document.getElementById("demodiv"), | |
34 | data, | |
35 | { | |
36 | labels: ['X', 'Y1', 'Y2'], | |
37 | underlayCallback: drawLines, | |
38 | drawPoints: true | |
39 | } | |
40 | ); | |
41 | ||
42 | // coefficients of regression for each series. | |
43 | // if coeffs = [ null, [1, 2], null ] then we draw a regression for series 1 | |
44 | // only. The regression line is y = 1 + 2 * x. | |
45 | var coeffs = [ null, null, null ]; | |
46 | function regression(series) { | |
47 | // Only run the regression over visible points. | |
48 | var range = g.xAxisRange(); | |
49 | ||
50 | var sum_xy = 0.0, sum_x = 0.0, sum_y = 0.0, sum_x2 = 0.0, num = 0; | |
51 | for (var i = 0; i < g.numRows(); i++) { | |
52 | var x = g.getValue(i, 0); | |
53 | if (x < range[0] || x > range[1]) continue; | |
54 | ||
55 | var y = g.getValue(i, series); | |
56 | if (y == null) continue; | |
57 | ||
58 | num++; | |
59 | sum_x += x; | |
60 | sum_y += y; | |
61 | sum_xy += x * y; | |
62 | sum_x2 += x * x; | |
63 | } | |
64 | ||
65 | var a = (sum_xy - sum_x * sum_y / num) / (sum_x2 - sum_x * sum_x / num); | |
66 | var b = (sum_y - a * sum_x) / num; | |
67 | ||
68 | coeffs[series] = [b, a]; | |
2a07b0ad DV |
69 | if (console) { |
70 | console.log("coeffs(" + series + "): [" + b + ", " + a + "]"); | |
71 | } | |
05a1d116 DV |
72 | |
73 | g.updateOptions({}); // forces a redraw. | |
74 | } | |
75 | ||
76 | function clearLines() { | |
77 | for (var i = 0; i < coeffs.length; i++) coeffs[i] = null; | |
78 | g.updateOptions({}); | |
79 | } | |
80 | ||
81 | function drawLines(ctx, area, layout) { | |
82 | if (typeof(g) == 'undefined') return; // won't be set on the initial draw. | |
83 | ||
84 | var range = g.xAxisRange(); | |
85 | for (var i = 0; i < coeffs.length; i++) { | |
86 | if (!coeffs[i]) continue; | |
87 | var a = coeffs[i][1]; | |
88 | var b = coeffs[i][0]; | |
89 | ||
90 | var x1 = range[0]; | |
91 | var y1 = a * x1 + b; | |
92 | var x2 = range[1]; | |
93 | var y2 = a * x2 + b; | |
94 | ||
95 | var p1 = g.toDomCoords(x1, y1); | |
96 | var p2 = g.toDomCoords(x2, y2); | |
97 | ||
34bbe78b DV |
98 | var c = new RGBColor(g.getColors()[i - 1]); |
99 | c.r = Math.floor(255 - 0.5 * (255 - c.r)); | |
100 | c.g = Math.floor(255 - 0.5 * (255 - c.g)); | |
101 | c.b = Math.floor(255 - 0.5 * (255 - c.b)); | |
102 | var color = c.toHex(); | |
05a1d116 DV |
103 | ctx.save(); |
104 | ctx.strokeStyle = color; | |
105 | ctx.lineWidth = 1.0; | |
106 | ctx.beginPath(); | |
107 | ctx.moveTo(p1[0], p1[1]); | |
108 | ctx.lineTo(p2[0], p2[1]); | |
109 | ctx.closePath(); | |
110 | ctx.stroke(); | |
111 | ctx.restore(); | |
112 | } | |
113 | } | |
114 | ||
115 | </script> | |
116 | ||
117 | <div style="position: absolute; left: 500px; top: 150px;"> | |
118 | <input type=button style="color: green;" value="Regression (Y1)" onClick="regression(1)" /> | |
119 | <br/><br/> | |
120 | <input type=button style="color: blue;" value="Regression (Y2)" onClick="regression(2)" /> | |
121 | <br/><br/> | |
122 | <input type=button value="Clear Lines" onClick="clearLines()" /> | |
123 | </div> | |
124 | </body> | |
125 | </html> |