Beispiel #1
0
 public function testComplexCase()
 {
     $r = new LinearRegression(Dataset::fromArray([0.5, 1, 4, -1]));
     $this->assertSame(-0.15, $r->slope());
     $this->assertSame(1.35, $r->intercept());
     $this->assertSame(0.0, $r(9));
 }
Beispiel #2
0
// content="text/plain; charset=utf-8"
require_once '../../vendor/autoload.php';
require_once 'jpgraph/jpgraph_scatter.php';
use Amenadiel\JpGraph\Graph;
use Amenadiel\JpGraph\Plot;
require_once 'jpgraph/jpgraph_utils.inc.php';
// Create some "fake" regression data
$datay = array();
$datax = array();
$a = 3.2;
$b = 2.5;
for ($x = 0; $x < 20; ++$x) {
    $datax[$x] = $x;
    $datay[$x] = $a + $b * $x + rand(-20, 20);
}
$lr = new LinearRegression($datax, $datay);
list($stderr, $corr) = $lr->GetStat();
list($xd, $yd) = $lr->GetY(0, 19);
// Create the graph
$graph = new Graph\Graph(300, 250);
$graph->SetScale('linlin');
// Setup title
$graph->title->Set("Linear regression");
$graph->title->SetFont(FF_ARIAL, FS_BOLD, 14);
$graph->subtitle->Set('(stderr=' . sprintf('%.2f', $stderr) . ', corr=' . sprintf('%.2f', $corr) . ')');
$graph->subtitle->SetFont(FF_ARIAL, FS_NORMAL, 12);
// make sure that the X-axis is always at the
// bottom at the plot and not just at Y=0 which is
// the default position
$graph->xaxis->SetPos('min');
// Create the scatter plot with some nice colors
 public function init($title, $xdata, $ydata, $width = 350, $height = 320)
 {
     // 1. Setup some member variables
     //$graph_width=$width;
     //$graph_height=$height;
     //$graph_title=$title;
     // 2. We'll need a graph object
     $this->graph = new \Graph($width, $height);
     // 3. Set some random values
     $this->graph->SetScale("textlin");
     $this->graph->title->Set($title);
     // 4. Build the linear regression class
     $linreg = new \LinearRegression($xdata, $ydata);
     // 4.1 Get the basic linear regression statistics
     list($stderr, $corr) = $linreg->GetStat();
     list($xd, $yd) = $linreg->GetY(1, count($xdata));
     list($b, $m) = $linreg->GetAB();
     // 4.2 Create the regression line
     $lplot = new \LinePlot($yd);
     $lplot->SetWeight(3);
     $lplot->SetColor('navy');
     // 5. Calculate regression and goal statistics
     //$mp = CakeNumber::precision($m, 0);
     //$bp = CakeNumber::precision($b, 0);
     // 5.1 y=mx+b
     //$s = "y = ".CakeNumber::precision($m,$p)."x + $bp";
     $s = "y = {$m} x + {$b}";
     $txt = new \Text($s);
     $txt->SetPos($this->left_margin, $this->stats_base_y);
     // Set color and font for the text
     $txt->SetColor('red');
     $txt->SetFont(FF_FONT2, FS_BOLD);
     $this->graph->AddText($txt);
     // 4.2 present amount
     //$present_amt = end($this->ydata);
     //App::uses("CakeNumber", "Utility");
     //$txt = new Text("p=" . CakeNumber::precision($present_amt,0));
     //$txt->SetPos($this->left_margin, $this->stats_base_y+$this->stats_line_ht);
     //$this->graph->AddText($txt);
     // 4.3 the goal
     //$delta = $this->goal - $present_amt;
     //$deltaP = $delta/$m;
     //$deltaP = CakeNumber::precision($deltaP, 1);
     //$s = "g = $this->goal";
     //$txt = new Text($s);
     //$txt->SetPos($this->left_margin, $this->stats_base_y+$this->stats_line_ht*2);
     //$this->graph->AddText($txt);
     //$s = "t = $deltaP";
     //$txt = new Text($s);
     //$txt->SetPos($this->left_margin, $this->stats_base_y+$this->stats_line_ht*3);
     //$this->graph->AddText($txt);
     $this->graph->Add($lplot);
     //$this->graph->legend->SetPos(0.5,0.98,'center','bottom');
     //$this->graph->legend->SetAbsPos(0,0);
     //$this->graph->yaxis->SetColor('red');// this works here, but not earlier
     //$this->graph->xaxis->SetColor('blue');
     //$this->graph->SetAxisLabelBackground(1,'red');
     //$this->graph->SetAxisLabelBackground(2,'green');
     //$this->graph->SetBackgroundCFlag(2,BGIMG_FILLPLOT,100); // no discernable effect
     //$this->graph->SetBackgroundGradient('navy','silver',2,BGRAD_FRAME); // no discernable effect
     //$this->graph->SetBox(true, array(80,80,80), 10);
     //$this->graph->SetBox();
     //$this->graph->SetFrame(true,'darkblue',20);
     //$this->graph->SetFrameBevel(20,true,'black');
     //$this->graph->SetMarginColor('silver');
     //$this->graph->SetMargin($this->left_margin,50,50,50); // left, right, top, bottom
     //$this->graph->SetMargin(50,50,50,0); // left, right, top, bottom
 }