{"id":42,"date":"2005-11-21T19:19:00","date_gmt":"2005-11-21T19:19:00","guid":{"rendered":"http:\/\/blog.trungson.com\/?p=42"},"modified":"2005-11-21T19:19:00","modified_gmt":"2005-11-21T19:19:00","slug":"linear-regression-php-class","status":"publish","type":"post","link":"http:\/\/blog.trungson.com\/?p=42","title":{"rendered":"Linear Regression PHP Class"},"content":{"rendered":"<p>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)<\/p>\n<p><textarea name=\"code\" class=\"php\"><br \/>\n<?php\n\/** perform regression analysis on the input data, make the trend line y=ax+b\n* @author Son Nguyen\n* @since 11\/18\/2005\n* @package Framework\n* @subpackage Math\n*\/\nclass CRegressionLinear {\n  private $mDatas; \/\/ input data, array of (x1,y1);(x2,y2);... pairs, or could just be a time-series (x1,x2,x3,...)\n  \/** constructor *\/\n  function __construct($pDatas) {\n    $this->mDatas = $pDatas;<br \/>\n  }<\/p>\n<p>  \/** compute the coeff, equation source: http:\/\/people.hofstra.edu\/faculty\/Stefan_Waner\/RealWorld\/calctopic1\/regression.html *\/<br \/>\n  function calculate() {<br \/>\n    $n = count($this->mDatas);<br \/>\n    $vSumXX = $vSumXY = $vSumX = $vSumY = 0;<br \/>\n    \/\/var_dump($this->mDatas);<br \/>\n    $vCnt = 0; \/\/ for time-series, start at t=0<br \/>\n    foreach ($this->mDatas AS $vOne) {<br \/>\n      if (is_array($vOne)) { \/\/ x,y pair<br \/>\n        list($x,$y) = $vOne;<br \/>\n      } else { \/\/ time-series<br \/>\n        $x = $vCnt; $y = $vOne;<br \/>\n      } \/\/ fi<\/p>\n<p>      $vSumXY += $x*$y;<br \/>\n      $vSumXX += $x*$x;<br \/>\n      $vSumX += $x;<br \/>\n      $vSumY += $y;<br \/>\n      $vCnt++;<br \/>\n    } \/\/ rof<br \/>\n    $vTop = ($n*$vSumXY &#8211; $vSumX*$vSumY);<br \/>\n    $vBottom = ($n*$vSumXX &#8211; $vSumX*$vSumX);<br \/>\n    $a = $vBottom!=0?$vTop\/$vBottom:0;<br \/>\n    $b = ($vSumY &#8211; $a*$vSumX)\/$n;<br \/>\n    \/\/var_dump($a,$b);<br \/>\n    return array($a,$b);<br \/>\n  }<\/p>\n<p>  \/** given x, return the prediction y *\/<br \/>\n  function predict($x) {<br \/>\n    list($a,$b) = $this->calculate();<br \/>\n    $y = $a*$x+$b;<br \/>\n    return $y;<br \/>\n  }<br \/>\n}<br \/>\n?><br \/>\n<\/textarea><\/p>\n<h4>Sample Usage<\/h4>\n<p><textarea name=\"code\" class=\"php\"><\/p>\n<p>\/\/ sales data for the last 30 quarters<br \/>\n$vSales = array(<br \/>\n 637381,700986,641305,660285,604474,565316,598734,688690,723406,697358,<br \/>\n 669910,605636,526655,555165,625800,579405,588317,634433,628443,570597,<br \/>\n 662584,763516,742150,703209,669883,586504,489240,648875,692212,586509<br \/>\n);<\/p>\n<p>$vRegression = new CRegressionLinear($vSales);<br \/>\n$vNextQuarter = $vRegression->predict(); \/\/ return the forecast for next period<br \/>\n<\/textarea><\/p>\n<p>In this example, we assume it&#8217;s a very simple case. For a better forecasting method, there are many, including this <a href=\"http:\/\/www.adspeed.org\/2008\/10\/time-series-forecasting-with-php.html\">multiplicative forecasting method<\/a> that takes into account seasonality, irregularity and also the growth trend.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \/\/ sales data for the last 30 quarters $vSales = array( 637381,700986,641305,660285,604474,565316,598734,688690,723406,697358, 669910,605636,526655,555165,625800,579405,588317,634433,628443,570597, 662584,763516,742150,703209,669883,586504,489240,648875,692212,586509 ); $vRegression = new CRegressionLinear($vSales); $vNextQuarter = $vRegression->predict(); \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,8],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/42"}],"collection":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=42"}],"version-history":[{"count":0,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}