Correlation formula explanation needed d3.js

Posted by divakar on Stack Overflow See other posts from Stack Overflow or by divakar
Published on 2014-08-20T10:15:40Z Indexed on 2014/08/20 10:20 UTC
Read the original article Hit count: 230

Filed under:
function getCorrelation(xArray, yArray) {
  alert(xArray);
  alert(yArray);
  function sum(m, v) {return m + v;}
  function sumSquares(m, v) {return m + v * v;}
  function filterNaN(m, v, i) {isNaN(v) ? null : m.push(i); return m;}

  // clean the data (because we know that some values are missing)
  var xNaN = _.reduce(xArray, filterNaN , []);
  var yNaN = _.reduce(yArray, filterNaN , []);
  var include = _.intersection(xNaN, yNaN);
  var fX = _.map(include, function(d) {return xArray[d];});
  var fY = _.map(include, function(d) {return yArray[d];});

  var sumX = _.reduce(fX, sum, 0);
  var sumY = _.reduce(fY, sum, 0);
  var sumX2 = _.reduce(fX, sumSquares, 0);
  var sumY2 = _.reduce(fY, sumSquares, 0);
  var sumXY = _.reduce(fX, function(m, v, i) {return m + v * fY[i];}, 0);

  var n = fX.length;
  var ntor = ( ( sumXY ) - ( sumX * sumY / n) );
  var dtorX = sumX2 - ( sumX * sumX / n);
  var dtorY = sumY2 - ( sumY * sumY / n);

  var r = ntor / (Math.sqrt( dtorX * dtorY )); // Pearson ( http://www.stat.wmich.edu/s216/book/node122.html )
  var m = ntor / dtorX; // y = mx + b
  var b = ( sumY - m * sumX ) / n;

  // console.log(r, m, b);
  return {r: r, m: m, b: b};
}

I have finding correlation between the points i plot using this function which is not written by me. my

xarray=[120,110,130,132,120,118,134,105,120,0,0,0,0,137,125,120,127,120,160,120,148]

yarray=[80,70,70,80,70,62,69,70,70,62,90,42,80,72,0,0,0,0,78,82,68,60,58,82,60,76,86,82,70]

I can t able to understand the function perfectly. Can anybody explain it with the data i pasted here. I also wanted to remove the zeros getting calculated from this function.

© Stack Overflow or respective owner

Related posts about d3.js