| 119785 | // Source: https://github.com/Tom-Alexander/regression-js/blob/master/src/regression.js#L246 |
| 119786 | // License: https://github.com/Tom-Alexander/regression-js/blob/master/LICENSE |
| 119787 | function poly(data, x6, y1, order) { |
| 119788 | // use more efficient methods for lower orders |
| 119789 | if (order === 1) return linear(data, x6, y1); |
| 119790 | if (order === 2) return quad(data, x6, y1); |
| 119791 | const [xv, yv, ux, uy] = points(data, x6, y1), n = xv.length, lhs = [], rhs = [], k = order + 1; |
| 119792 | let i, j, l, v, c; |
| 119793 | for(i = 0; i < k; ++i){ |
| 119794 | for(l = 0, v = 0; l < n; ++l)v += Math.pow(xv[l], i) * yv[l]; |
| 119795 | lhs.push(v); |
| 119796 | c = new Float64Array(k); |
| 119797 | for(j = 0; j < k; ++j){ |
| 119798 | for(l = 0, v = 0; l < n; ++l)v += Math.pow(xv[l], i + j); |
| 119799 | c[j] = v; |
| 119800 | } |
| 119801 | rhs.push(c); |
| 119802 | } |
| 119803 | rhs.push(lhs); |
| 119804 | const coef = gaussianElimination(rhs), predict = (x)=>{ |
| 119805 | x -= ux; |
| 119806 | let y = uy + coef[0] + coef[1] * x + coef[2] * x * x; |
| 119807 | for(i = 3; i < k; ++i)y += coef[i] * Math.pow(x, i); |
| 119808 | return y; |
| 119809 | }; |
| 119810 | return { |
| 119811 | coef: uncenter(k, coef, -ux, uy), |
| 119812 | predict: predict, |
| 119813 | rSquared: rSquared(data, x6, y1, uy, predict) |
| 119814 | }; |
| 119815 | } |
| 119816 | function uncenter(k, a, x, y) { |
| 119817 | const z = Array(k); |
| 119818 | let i, j, v, c; // initialize to zero |