Linear regression

Linear regression is often used to predict the further development of random values.
I wished to be able to se the development of an x-y scatter plot and to display it in a graphical view.
I also wanted to be able to see the most likely development in to the future of this scatter plot.
I used this formula to find the slope of the straight line, which will tell me how the development will be.

Slope

I need two array’s. One for X and one for Y.

example:

$x=array(1,2,3,4);
$y=array(1,2,3,4);

(They need to contain the same amount of plots)
Looking at the formula, I need the mean for X and the mean for Y.
I also need to get the X value and the Y value for any given position (Xi and Yi).

Since you need the two arrays to contain the same number of plots, you can create a check for this.

example:

$n = count($x);
if ($n != count($y)) {
echo «The arrays does not contain the same amount of plots!»;
}

I first get the mean of each array:
(This is X and Y with a line over it.)

$x_snitt=(array_sum($x)/$n);
$y_snitt=(array_sum($y)/$n);

I then create a for-loop, which will parse the X-array and the Y-array and calculate the value over and under the equation.

for($i = 0; $i < $n; $i++) {

//calculate the value for the position in the arrays

$line_over=(($x[$i]-$x_snitt)*($y[$i]-$y_snitt));

$line_under=(($x[$i]-$x_snitt)*($x[$i]-$x_snitt));

//Adding the values to mid_sum_top og mid_sum_bottom

$mid_sum_top+=$line_over;

$mid_sum_bottom+=$line_under;

}

I’ve now got the values over and under the equation.
I now need to calculate the slope for the regression line.

$b=($mid_sum_top/$mid_sum_bottom);

I now want to find out where the regression line, cross Y.
I use this formula :

intercept

$intercept=$y_snitt-($b*$x_snitt);

You now have the values to creat a formula/array wich can show you the linear regression of the scatterplot.

(Linear regression line, is supposed to cross the median point of the scatterplot and the intercept point.)

Linear regression

You can put the code in a function which return the results e.g. in a array.

In this demo, a random scatterplot is created and the simple linear regression is calculated and shown:

You can press [F5] to create a new picture.