javascriptnode.jsgraphtrendbest-fit

Calculating value from where it should sit on a graph (line of best fit)


I have the following sample dataset -

|     a     |    f    |
|-----------|---------|
| £75.00    |  43,200 |
| £500.00   |  36,700 |
| £450.00   |  53,400 |
| £450.00   |  25,700 |
| £250.00   |  12,900 |
| £1,600.00 | 136,000 |
| £600.00   |  72,900 |
| £500.00   |  13,000 |
| £500.00   |  49,600 |
| £500.00   |  43,600 |
| £1,000.00 | 104,000 |

And I've used google graphs to create a line of best fit for it

enter image description here

It has analysed the 'trend line' as 0.762.

I simply want to create a function that I can use to calculate A, when given F. I just want a number returned.

const calc = f => what_A_should_be_given_F

What might be a good approach for this?

Thanks for any help, Ollie


EDIT: The slope is 83.81246554, and the y-intercept is 633.9917215.


Solution

  • You can do it like this.

    If you have y = mx + b and need to get x you need to modify your equation to x = (y - b) / m

    const arr = [43200, 36700, 53400, 25700, 12900, 136000, 72900, 13000, 49600, 43600, 104000];
    
    const m = 83.81246554;
    const b = 633.991172215;
    const calc = f => (f - b) / m;
    
    for (let i = 0; i < arr.length; i++) {
      console.log(`${arr[i]} => ${calc(arr[i])}`);
    }