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.


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



(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.


$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.)


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



//Adding the values to mid_sum_top og mid_sum_bottom




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


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



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.