Jump to content

Recommended Posts

Posted

Using the linear regression method outlined in the following https://www.easycalculation.com/statistics/learn-regression.php I wrote some javascript to find the outliers of a given set. However I would like the method to work with curves and in 3d. Not sure of the forumlae in 3d.

 

<script>
function findLineByLeastSquares(values_x, values_y) {
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_xx = 0;
var count = 0;
var x = 0;
var y = 0;
var values_length = values_x.length;
if (values_length != values_y.length) {
throw new Error('The parameters values_x and values_y need to have same size!');
}
if (values_length === 0) {
return [ [], [] ];
}

for (var v = 0; v < values_length; v++) {
x = values_x[v];
y = values_y[v];
sum_x += x;
sum_y += y;
sum_xx += x*x;
sum_xy += x*y;
count++;
}
/*
* Calculate m and b for the formula:
* y = x * m + b
*/
var m = (count*sum_xy - sum_x*sum_y) / (count*sum_xx - sum_x*sum_x);
var b = (sum_y/count) - (m*sum_x)/count;
return [m, b];
}

function outliers(values_x,values_y,m,b,outlierdistance){
var outlier = [];
for (var v = 0; v < values_x.length; v++) {
x = values_x[v];
y = values_y[v];
distance = (m*x - y + b)/(Math.sqrt(Math.pow(x,2)+Math.pow(y,2)));
if(distance<0){
distance = distance * -1;
}
if(distance>outlierdistance){
outlier.push(x,y);
}

}
return outlier;
}
values_x = [10,2330,9,44,20,30,40,50];
values_y = [10,20,900,44,20,30,40,50];
slope = findLineByLeastSquares(values_x, values_y);
document.write(outliers(values_x,values_y,slope[0],slope[1],1));
</script>

 

Posted

Outlier detection is as much art as it is science.

 

Can't help with the code but this paper gives the formulas for several types of outlier detection in multivariate regression.

 

But when you say curves you mean you want to use non-linear regression?

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.