Created
September 5, 2015 21:42
-
-
Save IceCreamYou/7da531a34536105d7398 to your computer and use it in GitHub Desktop.
Indicates the linear correlation of a three-dimensional data set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns the r^2 value for a three-dimensional data set. | |
* | |
* `r` is the Pearson product-moment correlation coefficient, a measure of | |
* linear correlation. | |
* | |
* @param {Object[]} data | |
* An array of vectors with `x`, `y`, and `z` properties representing points | |
* in the data set. | |
* | |
* @return {Number} | |
* The r^2 value for the input data. | |
*/ | |
function rsquared3(data) { | |
var avgX = 0, | |
avgY = 0, | |
avgZ = 0, | |
avg = 0, | |
sseX = 0, | |
sseY = 0, | |
sseZ = 0, | |
n = data.length, | |
i; | |
for (i = 0; i < n; i++) { | |
avgX += data[i].x; | |
avgY += data[i].y; | |
avgZ += data[i].z; | |
} | |
avgX /= n; | |
avgY /= n; | |
avgZ /= n; | |
for (i = 0; i < n; i++) { | |
avg += (data[i].x - avgX) * (data[i].y - avgY) * (data[i].z - avgZ); | |
sseX += (data[i].x - avgX) * (data[i].x - avgX); | |
sseY += (data[i].y - avgY) * (data[i].y - avgY); | |
sseZ += (data[i].z - avgZ) * (data[i].z - avgZ); | |
} | |
avg /= n; | |
var r = avg / Math.sqrt((sseX * sseY * sseZ) / (n * n * n)); | |
return r*r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment