UNPKG

lumenize

Version:

Illuminating the forest AND the trees in your data.

43 lines (39 loc) 1.24 kB
// Generated by CoffeeScript 1.10.0 (function() { var correlate; correlate = function(points, xField, yField) { var description, div, i, intercept, len, n, point, rSquared, slope, sumX, sumX2, sumXY, sumY, sumY2; if (xField == null) { xField = 'x'; } if (yField == null) { yField = 'y'; } n = points.length; sumX = 0; sumY = 0; sumXY = 0; sumX2 = 0; sumY2 = 0; for (i = 0, len = points.length; i < len; i++) { point = points[i]; sumX += point[xField]; sumY += point[yField]; sumXY += point[xField] * point[yField]; sumX2 += point[xField] * point[xField]; sumY2 += point[yField] * point[yField]; } div = (n * sumX2) - (sumX * sumX); intercept = ((sumY * sumX2) - (sumX * sumXY)) / div; slope = ((n * sumXY) - (sumX * sumY)) / div; rSquared = Math.pow((n * sumXY - sumX * sumY) / Math.sqrt((n * sumX2 - sumX * sumX) * (n * sumY2 - sumY * sumY)), 2); description = "y = " + slope + " * x + " + intercept + " with R^2 of " + rSquared; return { intercept: intercept, slope: slope, rSquared: rSquared, description: description }; }; exports.correlate = correlate; }).call(this);