-
Notifications
You must be signed in to change notification settings - Fork 664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trying to implement CORR() using custom function logic, but it is not working #1966
Comments
If you provide some example data and expected output it makes it easier to help out. From the AlaSQL Bot Your implementation of the Here are a few potential issues to check:
Here's an improved implementation suggestion:
alasql.aggr.CORR = function(valueX, valueY, accumulator, stage) {
if (stage === 1) {
// Initialize the accumulator object
accumulator = {
sumX: 0,
sumY: 0,
sumXY: 0,
sumX2: 0,
sumY2: 0,
count: 0
};
}
if (stage === 1 || stage === 2) {
if (typeof valueX === 'number' && typeof valueY === 'number') {
accumulator.sumX += valueX;
accumulator.sumY += valueY;
accumulator.sumXY += valueX * valueY;
accumulator.sumX2 += valueX * valueX;
accumulator.sumY2 += valueY * valueY;
accumulator.count++;
}
}
if (stage === 3) {
const { count, sumX, sumY, sumXY, sumX2, sumY2 } = accumulator;
const numerator = (count * sumXY) - (sumX * sumY);
const denominatorX = Math.sqrt((count * sumX2) - (sumX * sumX));
const denominatorY = Math.sqrt((count * sumY2) - (sumY * sumY));
const denominator = denominatorX * denominatorY;
if (denominator === 0 || count === 0) {
return null;
}
const result = numerator / denominator;
return result;
}
return accumulator;
}; Debugging Steps:
If the issue persists, you might want to inspect the input data to confirm it's as expected. |
I confirmed that there is no null or undefined in the dataset. This is the test dataset that we are using. Thank you. |
Yep. A .aggr function only takes one col as input. I would try to do the calculation outside of alasql if you need two. |
Hello. I am trying to implement CORR() using custom function logic provided here:
https://github.com/alasql/alasql/wiki/User-Defined-Functions
This is my code.
But, somehow this returned
{0 : {corr: 3}}
, which signifies the calculation is not being done properly, considering pearson correlation always lies between -1 and 1...Any guess or help to figure out what to do?
Thank you in advance.
The text was updated successfully, but these errors were encountered: