Regressão Linear, de Potência e Polinomial em Javascript

Eu custei a conseguir fazer funcionar e a achar na internet, então resolvi trazer pra cá.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
var regression = {
    
    linear: function(x, y){
    
        var lr = {};
        var n = y.length;
        var sum_x = 0;
        var sum_y = 0;
        var sum_xy = 0;
        var sum_xx = 0;
        var sum_yy = 0;
        
        for (var i = 0; i < y.length; i++) {
            sum_x += x[i];
            sum_y += y[i];
            sum_xy += (x[i] * y[i]);
            sum_xx += (x[i] * x[i]);
            sum_yy += (y[i] * y[i]);
        } 
        
        lr['slope'] = ((n * sum_xy) - (sum_x * sum_y)) / ((n * sum_xx) - (sum_x * sum_x));
        lr['intercept'] = (sum_y - (lr.slope * sum_x)) / n;
        lr['r2'] = Math.pow((n*sum_xy - sum_x*sum_y) / Math.sqrt(((n * sum_xx) - (sum_x * sum_x)) * ((n * sum_yy) - (sum_y * sum_y))), 2);
        lr['fn'] = function (x) { 
            return this.slope * x + this.intercept; 
        };
        
        return lr;
        
    },

    power: function(data_x, data_y) {
    
        var n = data_x.length;
        if (n < 2 || n != data_y.length) {
            return false;
        }
    
        var sumX = 0;
        var sumY = 0;
        var sumXX = 0;
        var sumXY = 0;
        for (var i = 0; i < n; i++) {
            var x = Math.log(data_x[i]);
            var y = Math.log(data_y[i]);
            sumX += x;
            sumY += y;
            var xx = x * x;
            sumXX += xx;
            var xy = x * y;
            sumXY += xy;
        }
        var sxx = sumXX - (sumX * sumX) / n;
        var sxy = sumXY - (sumX * sumY) / n;
        var xbar = sumX / n;
        var ybar = sumY / n;
        
        var b = sxy / sxx;
        var a = Math.pow(Math.exp(1.0), ybar - b * xbar);
    
        return {
            a: a,
            b: b
        };
    
    },
    
    polynomial: function(data_x, data_y, order) {
        
        var itemCount = data_x.length;
        
        if (itemCount < order + 1) {
            //return false;
        }
        
        var validItems = 0;
        
        var data = {};
        var validItems = 0;
        
        var item;
        for(item = 0; item < itemCount; item++){
            
            var x = data_x[item];
            var y = data_y[item];

            if (!isNaN(x) && !isNaN(y)){
                
                if (typeof data[0] != "object") {
                    data[0] = {};
                }
                
                if (typeof data[1] != "object") {
                    data[1] = {};
                }
                
                data[0][validItems] = x;
                data[1][validItems] = y;
                
                validItems++;
                
            }
            
        }
        
        if (validItems < order + 1) {
            //return false;
        }
        
        var equations = order + 1;
        var coefficients = order + 2;
        
        var result = {};
        var matrix = {};
        
        var sumX = 0;
        var sumY = 0;
        
        for(item = 0; item < validItems; item++){
            
            sumX += data[0][item];
            sumY += data[1][item];
            
            var eq;
            for(eq = 0; eq < equations; eq++){
                
                var coe;
                for(coe = 0; coe < coefficients - 1; coe++){
                    
                    if (typeof matrix[eq] != "object") {
                        matrix[eq] = {};
                    }
                    
                    if (typeof matrix[eq][coe] == "undefined") {
                         matrix[eq][coe] = 0;
                    }
                    
                    matrix[eq][coe] += Math.pow(data[0][item], eq + coe);
                    
                }
                
                if (typeof matrix[eq][coefficients - 1] == "undefined") {
                    matrix[eq][coefficients - 1] = 0;
                }
                
                matrix[eq][coefficients - 1] += data[1][item] * Math.pow(data[0][item],eq);
                
            }
            
        }
        
        var subMatrix = regression.calculateSubMatrix(matrix);
        
        var eq;
        for (eq = 1; eq < equations; eq++) {
            
            matrix[eq][0] = 0;
            
            var coe;
            for (coe = 1; coe < coefficients; coe++) {
                matrix[eq][coe] = subMatrix[eq - 1][coe - 1];
            }
            
        }
        
        for (eq = equations - 1; eq > -1; eq--) {
            
            var value = matrix[eq][coefficients - 1];
            
            var coe;
            for (coe = eq; coe < coefficients -1; coe++) {
                value -= matrix[eq][coe] * (isNaN(result[coe]) ? 0 : result[coe]);
            }
            
            result[eq] = value / matrix[eq][eq];
            
        }
        
        var meanY = sumY / validItems;
        var yObsSquare = 0;
        var yRegSquare = 0;
        
        for (item = 0; item < validItems; item++) {
            var yCalc = 0;
            
            var eq;
            for (eq = 0; eq < equations; eq++) {
                yCalc += result[eq] * Math.pow(data[0][item],eq);
            }
            
            yRegSquare += Math.pow(yCalc - meanY, 2);
            yObsSquare += Math.pow(data[1][item] - meanY, 2);
            
        }
        var rSquare = yRegSquare / yObsSquare;
        
        result['r2'] = rSquare;
        
        return result;
        
    },
    
    calculateSubMatrix: function(matrix){
        
        var equations = (typeof matrix == "object" ? Object.keys(matrix).length : 0);
        var coefficients = (typeof matrix[0] == "object" ? Object.keys(matrix[0]).length : 0);
        
        var result = {};
        
        var eq;
        for (eq = 1; eq < equations; eq++) {
            
            var factor = matrix[0][0] / matrix[eq][0];
            
            var coe;
            for (coe = 1; coe < coefficients; coe++) {
                
                if (typeof result[eq - 1] != "object") {
                    result[eq - 1] = {};
                }
                
                result[eq - 1][coe -1] = matrix[0][coe] - matrix[eq][coe] * factor;
                
            }
            
        }
        
        if (equations == 1) {
            return result;
        }
        
        // check for zero pivot element
        if (result[0][0] == 0) {
            
            var found = false;
            
            var i;
            for (i = 0; i < result.length; i ++) {
                
                if (result[i][0] != 0) {
                    found = true;
                    var temp = result[0];
                    var j;
                    for (j = 0; j < result[i].length; j++) {
                        result[0][j] = result[i][j];
                    }
                    for (j = 0; j < temp.length; j++) {
                        result[i][j] = temp[j];
                    }
                    break;
                }
                
            }
            
            if (!found) {
                
                var i, j;
                
                var retorno = {};
                
                for (i = 0; i < equations; i++) {
                    for (j = 0; j < coefficients; j++) {
                        if (typeof retorno[i] != "object") {
                            retorno[i] = {};
                        }
                        retorno[i][j] = 0;
                    }
                }
                
                return retorno;
                
            }
            
        }
        
        var subMatrix = regression.calculateSubMatrix(result);
        
        for (eq = 1; eq < equations -  1; eq++) {
            result[eq][0] = 0;
            
            var coe;
            for (coe = 1; coe < coefficients - 1; coe++) {
                result[eq][coe] = subMatrix[eq - 1][coe - 1];
            }
            
        }
        
        return result;
        
    }
    
}

Para usar:

1
2
3
4
5
6
7
8
regression.linear(x, y);
retorno: { slope, intercept, r2, fn(x) }

regression.power(x, y);
retorno: { a, b }

regression.polynomial(x, y, order);
retorno: { 0, 1, 2, ..., r2 }

Ainda tenho que limpar o código, comentar, etc, mas já dá pra vocês usarem.

Portei o código da biblioteca Jfree de Java.


Tags: , , , , , , , , , ,
This entry was posted on terça-feira, setembro 11th, 2012 at 11:06 and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.