Monday, November 21, 2005

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.

Labels: ,

6 Comments:

At 1:11 PM, Anonymous Andras said...

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 :)

 
At 2:55 PM, Blogger trungson said...

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.

 
At 8:46 AM, Anonymous Anonymous said...

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

 
At 8:10 AM, Anonymous Anonymous said...

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.

 
At 2:42 PM, Anonymous Anonymous said...

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

 
At 12:41 AM, Anonymous Amy Ramesh said...

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

 

Post a Comment

<< Home