Consider the following y-values ($(0,y_0),(1,y_1),...$):
$$580,382,854,193,128,901,283,294,854,490$$
Plotting the linear regression gives the following formula:
$$y = 4.5x + 475.8$$
However, switching around the order of the y-values, like so:
$$580,382,854,854,128,901,283,294,193,490$$
Gives the following line of best fit:
$$y = -35.6x + 656.1$$
Why does the order matter when it comes to a line of best fit? The elements are the same and the algorithm I am using has no interaction between x- and y-variables:
sx = 0; sy = 0; stt = 0; sts = 0;
yArray = {}; //ten numbers from above
for (i = 0; i < 10; ++i) {
sx = sx + i;
sy = sy + yArray[i];
}
for (i = 0; i < 10; ++i) {
t = i - (sx / 10);
stt = stt + (t * t);
sts = sts + (t * yArray[i]);
}
slope = sts / stt;
intercept = (sy - (sx * slope)) / 10;
There's nothing like sx + sy
or sx * sy
, etc. I just don't see where the order matters here.