Linear Regression PHP Class

This PHP 5 class accepts an array of data as input, performs linear regression analysis and returns the trend line. The result is the same as in Excel (Tools/Data Analysis/Regression)

Sample Usage

In this example, we assume it’s a very simple case. For a better forecasting method, there are many, including this multiplicative forecasting method that takes into account seasonality, irregularity and also the growth trend.


Posted

in

,

by

Tags:

Comments

12 responses to “Linear Regression PHP Class”

  1. Andras Avatar
    Andras

    Hey, Your code rocks except for Line 21, where it says $vSumXY += $x*$x; instead of $vSumXX += $x*$x; . Please correct it, it took me quite a lot of time to figure out, why the results were wrong.

    Thanks anyway, I really appreciated it 🙂

  2. trungson Avatar
    trungson

    Correction has been made. It’s weird how I have it correct in the codebase but somehow pasted it incorrectly to the blog. 🙂 Thanks Andras.

  3. Anonymous Avatar
    Anonymous

    So where does the data go? Wheer and how do you enter data streams in to this code to run it?

  4. Anonymous Avatar
    Anonymous

    For those who are interested, I have compared the values of slope and Y-intercept given by this Class with those given by Excel. They produce exactly equivalent responses, at least out to 10 decimal places. Nice work.

  5. Anonymous Avatar
    Anonymous

    I would love to see an example of how this class works…

  6. Amy Ramesh Avatar
    Amy Ramesh

    Thank you very much for the code. But I would like to know how the functions will be called. It would be great if you provide some example.

    Thank you
    ^Amy

  7. Anonymous Avatar
    Anonymous

    How would you create a list of points for trendline for the historical dataset? this just predicts forward set

    Thanks

  8. Anonymous Avatar
    Anonymous

    Many thanks!!
    Also, congrats for providing an easy read and ready to use code.

  9. Anonymous Avatar
    Anonymous

    this script is great and it works without any issues on http://www.pacifichost.com good host

  10. Anonymous Avatar
    Anonymous

    Hi – thanks for the great script. It's been a long time since I've seen y=mx+b! Slope worked fine for me as time series, but the y-intercept wasn't working. (The reverse occurred when using pairs). I modified the script so it just takes key => value pairs and does both correctly. Predict needs the next X-value as an argument to work. Here's the code:

    class CRegressionLinear {
    /** perform regression analysis on the input data, make the trend line y=ax+b
    * http://blog.trungson.com/2005/11/linear-regression-php-class.html
    */
    private $mDatas; // input data, array of (x1,y1);(x2,y2);… pairs, or could just be a time-series (x1,x2,x3,…)
    /** constructor */
    function __construct($pDatas) {
    $this->mDatas = $pDatas;
    }

    /** compute the coeff, equation source: http://people.hofstra.edu/faculty/Stefan_Waner/RealWorld/calctopic1/regression.html */
    function calculate() {
    $n = count($this->mDatas);
    $vSumXX = $vSumXY = $vSumX = $vSumY = 0;
    foreach ($this->mDatas AS $vCnt => $vOne) {
    $x = $vCnt; $y = $vOne;
    $vSumXY += $x*$y;
    $vSumXX += $x*$x;
    $vSumX += $x;
    $vSumY += $y;
    $vCnt++;
    } // rof
    $vTop = ($n*$vSumXY – $vSumX*$vSumY);
    $vBottom = ($n*$vSumXX – $vSumX*$vSumX);
    $a = $vBottom!=0?$vTop/$vBottom:0;
    $b = ($vSumY – $a*$vSumX)/$n;
    return array($a,$b);
    }

    /** given x, return the prediction y */
    function predict($x) {
    list($a,$b) = $this->calculate();
    $y = ($a*$x)+$b;
    return $y;
    }
    } // function CRegressionLinear

    //Thanks again

  11. Santhosh Avatar
    Santhosh

    simple codings, can easy to learn crispy.

  12. Alfredo Covaleda Vélez Avatar

    Son, thanks for your code. I added a few lines to calculate the coefficient of correlation and the coefficient of determination inside your class. I’d like to use this class as other of the classes included as part of my thesis work, of course with the additional code I have written. Regards.

Leave a Reply

Your email address will not be published. Required fields are marked *