vega-statistics
Version:
Statistical routines and probability distributions.
29 lines (23 loc) • 731 B
JavaScript
import ols from './ols.js';
import {points, visitPoints} from './points.js';
import rSquared from './r-squared.js';
export default function(data, x, y) {
const [xv, yv, ux, uy] = points(data, x, y);
let YL = 0, XY = 0, XYL = 0, X2Y = 0, n = 0, dx, ly, xy;
visitPoints(data, x, y, (_, dy) => {
dx = xv[n++];
ly = Math.log(dy);
xy = dx * dy;
YL += (dy * ly - YL) / n;
XY += (xy - XY) / n;
XYL += (xy * ly - XYL) / n;
X2Y += (dx * xy - X2Y) / n;
});
const [c0, c1] = ols(XY / uy, YL / uy, XYL / uy, X2Y / uy),
predict = x => Math.exp(c0 + c1 * (x - ux));
return {
coef: [Math.exp(c0 - c1 * ux), c1],
predict: predict,
rSquared: rSquared(data, x, y, uy, predict)
};
}