PHP Classes
elePHPant
Icontem

File: examples/TestFit3.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jose Gomez  >  Linear Fit  >  examples/TestFit3.php  >  Download  
File: examples/TestFit3.php
Role: Example script
Content type: text/plain
Description: Multivariate linear fitting example
Class: Linear Fit
Perform linear regression on a set of data values
Author: By
Last change:
Date: 1 year ago
Size: 1,240 bytes
 

Contents

Class file image Download
<?
   
require('../LinearFit.php');

   
$oFit=new LinearFit;
   
$oFit->SetData(array(array(0,1), array(1,3), array(2,-1), array(3,2)), array(array(0,3),array(0,4),array(6,-1),array(5,1)));
   
$oFit->AddData(array(4,0), array(9,-2));
   
$aCoeffs=$oFit->Fit();
   
$aConf=$oFit->ConfInterval(0.95);
   
$aRes=$oFit->GetValues(array(array(2,1), array(0,0), array(2,7)));
    if (
php_sapi_name()=='cli')
    {
        for (
$iEq=0; $iEq<count($aCoeffs[0]);$iEq++)
        {
        echo
"Equation $iEq: y=a0";
        for (
$i=1; $i<count($aCoeffs); $i++)
        {
            echo
"+a$i*x$i";
        }
        echo
"\n";
        for (
$i=0; $i<count($aCoeffs); $i++)
        {
            echo
"a$i=" . $aCoeffs[$i][$iEq] . "±" . $aConf[$i][$iEq]. "\n";
        }
        echo
"r2=" . $oFit->R2()[$iEq] . "\n";
        echo
"x1=2 x2=2 -> y=" . $aRes[$iEq][0] . "\n";
        }
    }
    else
    {
        for (
$iEq=0; $iEq<count($aCoeffs[0]);$iEq++)
        {
        echo
"Equation $iEq: y=a<sub>0</sub>";
        for (
$i=1; $i<count($aCoeffs); $i++)
        {
            echo
"+a<sub>$i</sub>*x<sub>$i</sub>";
        }
        echo
"<br>";
        for (
$i=0; $i<count($aCoeffs); $i++)
        {
            echo
"a<sub>$i</sub>=" . $aCoeffs[$i][$iEq] . "&#177;" . $aConf[$i][$iEq] . "</br>";
        }
        echo
"r<sup>2</sup>=" . $oFit->R2()[$iEq] . "</br>";
        echo
"x<sub>1</sub>=2 x<sub>2</sub>=2 -> y=" . $aRes[$iEq][0] . "</br>";
        }
    }
?>