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.

Comments (12)

  1. 9:11 pm, January 22, 2006Andras  / Reply

    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. 10:55 pm, January 22, 2006trungson  / Reply

    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. 3:46 pm, July 7, 2006Anonymous  / Reply

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

  4. 3:10 pm, August 22, 2007Anonymous  / Reply

    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. 9:42 pm, June 25, 2008Anonymous  / Reply

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

  6. 7:41 am, September 12, 2008Amy Ramesh  / Reply

    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. 9:07 pm, July 21, 2009Anonymous  / Reply

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

    Thanks

  8. 12:16 pm, August 8, 2009Anonymous  / Reply

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

  9. 2:22 am, January 15, 2010Anonymous  / Reply

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

  10. 6:21 am, February 1, 2010Anonymous  / Reply

    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. 2:09 pm, March 8, 2010Santhosh  / Reply

    simple codings, can easy to learn crispy.

  12. 5:40 am, December 30, 2011Alfredo Covaleda Vélez  / Reply

    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

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.