Javascript library for Pearson and / or Spearman corrections

Is there a Javascript library for Spearman and / or Pearson correlation ?

+5
source share
4 answers

There is this

http://stevegardner.net/2012/06/11/javascript-code-to-calculate-the-pearson-correlation-coefficient/

besides what you could try:

http://www.jstat.org/download

alternatively, if none of them adjusts the score, and you do not want to write it yourself, you can always use:

http://www.rforge.net/Rserve/

with

http://www.gardenersown.co.uk/education/lectures/r/correl.htm

to do this.

0
source

Try the following:

function spearmanCorrelation(multiList, p1, p2){
    N=multiList[p1].length;
    order=[];
    sum=0;

    for(i=0;i<N;i++){
        order.push([multiList[p1][i], multiList[p2][i]]);
    }

    order.sort(function(a,b){
        return a[0]-b[0]
    });

    for(i=0;i<N;i++){
        order[i].push(i+1);
    }

    order.sort(function(a,b){
        return a[1]-b[1]
    });

    for(i=0;i<N;i++){
        order[i].push(i+1);
    }
    for(i=0;i<N;i++){
        sum+=Math.pow((order[i][2])-(order[i][3]), 2);

    }

    r=1-(6*sum/(N*(N*N-1)));

    return r;
}
+1
source

, - :

const pcorr = (x, y) => {
  let sumX = 0,
    sumY = 0,
    sumXY = 0,
    sumX2 = 0,
    sumY2 = 0;
  const minLength = x.length = y.length = Math.min(x.length, y.length),
    reduce = (xi, idx) => {
      const yi = y[idx];
      sumX += xi;
      sumY += yi;
      sumXY += xi * yi;
      sumX2 += xi * xi;
      sumY2 += yi * yi;
    }
  x.forEach(reduce);
  return (minLength * sumXY - sumX * sumY) / Math.sqrt((minLength * sumX2 - sumX * sumX) * (minLength * sumY2 - sumY * sumY));
};
pcorr([20, 54, 54, 65, 45], [22, 11, 21, 34, 87]);
+1

I used the Spearson project here on Github. I tested it for Spearman's correlation, and it gives exact values ​​for that.

I just uploaded the file spearson.jsto the /librepo folder . Here's how to use it in a browser:

<script src="spearson.js"></script>

<script>
    var x = [3, 4, 5];
    var y = [.1, .2, .3];
    var corr = spearson.correlation.spearman(x, y);
</script>

Similarly, you can use correlation.pearsonPearson's correlation.

0
source

All Articles