I am attempting to write an algorithm in $c++$ to calculate a linear trendline. The issue is when I am doing examples by hand, I get a slope that is $10^4$ whereas I believe it should be $10^3$. The results I get are also not the same as excel or any online calculators give me.
So I followed the equations given in the selected answer of this post. So this my working:
+-----+----------+----------+-------+
| x | y | x*y | x^2 |
+-----+----------+----------+-------+
| 155 | 0.116142 | 18.00201 | 13225 |
| 127 | 0.077622 | 9.857994 | 16129 |
| 113 | 0.058267 | 6.584171 | 12769 |
+-----+----------+----------+-------+
$$\begin{align} \sum x &= 395\\ \sum y &= 0.252031\\ \sum xy &= 34.444175\\ \sum x^2 &= 42123\\ \end{align}$$ Now this is exactly what I am calculating using the slope equation linked above:
$$\frac {\left(3 \times \sum xy\right) - \left(\sum x \times \sum y\right)}{\left(3 \times \sum x^2\right) - \left(\sum x\right)^2}$$
which is $$\frac{103.332525 - 99.552245}{126369 - 156025}\\
= -1.274710008 \times 10^{-4}\\
= -0.000127471$$
Now for the offset equation shown in the link I do:
$$\sum y - \text{slope}\left(\sum x\right) / 3$$
which is $$(0.252031 - -0.000127471(395)) / 3\\
=0.100794015$$
This gives me the result of $$y = -0.000127471x + 0.100794015$$
In excel and the online calculators I get $$y = 0.0013776399x - 0.0973791029$$
My values look close, but are still not correct, with the first having an extra zero (being $10^{-4}$ not $10^{-3}$) and the signs being reversed.
I must be missing something somewhere but I can't figure out what I am doing wrong, can anybody see my error?