Tom-Alexander / regression-js

Curve Fitting in JavaScript.
http://tom-alexander.github.io/regression-js/
MIT License
940 stars 131 forks source link

Exponential Regression Doens't Accept Negative Y Values #117

Open JimLynchCodes opened 3 years ago

JimLynchCodes commented 3 years ago

Hi, I am trying to fit an exponential curve to some points that have negative initial values.

For example, these points:

const exponentialPoints = [[0.1, -0.25], [1, 1.1], [2, 5], [4, 30]] 

When I run it through regression.exponential(exponentialPoints) it outputs a regression model where all the y values are "NaN"... 🤔

{
  points: [ [ 0.1, NaN ], [ 1, NaN ], [ 2, NaN ], [ 4, NaN ] ],
  predict: [Function: predict],
  equation: [ NaN, NaN ],
  string: 'y = NaNe^(NaNx)',
  r2: NaN
}

I am wondering why this is happening. I would think it's perfectly fine for an exponential curve to have negative y values.

For example, consider this line: y = 2 * e^(3x) - 7

(and see it plotted here: https://www.wolframalpha.com/input/?i=plot+y%3D+2+*+e%5E%283x%29+-+7)

You can also run the code here: https://repl.it/@JimLynch/exponential-reg-negative-y#index.js

Thanks!

Jezternz commented 3 years ago

I had this problem also by even just having the first value as [0, 0]. Checking out another repo mentioned on another issue: https://github.com/mljs/ml#regression.

Jezternz commented 3 years ago

I had this problem also by even just having the first value as [0, 0]. Checking out another repo mentioned on another issue: https://github.com/mljs/ml#regression.

That one doesn't seem to be much better, struggles to handle similar problems. I am shocked at how hard it seems to be to find a reasonable library to do this.

josephrocca commented 3 years ago

It also NaNs on non-negative examples:

eval(await fetch("https://cdnjs.cloudflare.com/ajax/libs/regression/2.0.1/regression.min.js").then(r => r.text()));
let data = [[0,0],[1,0],[2,0],[3,1],[4,2],[5,0],[6,10],[7,20],[8,30],[9,10],[10,50],[11,70],[12,100],[13,250],[14,100],[15,300],[16,700]];
regression.exponential(data);

image