[42] | 1 | <?php // content="text/plain; charset=utf-8"
|
---|
| 2 | require_once ('jpgraph/jpgraph.php');
|
---|
| 3 | require_once ('jpgraph/jpgraph_scatter.php');
|
---|
| 4 | require_once ('jpgraph/jpgraph_line.php');
|
---|
| 5 | require_once ('jpgraph/jpgraph_utils.inc.php');
|
---|
| 6 |
|
---|
| 7 | // Create some "fake" regression data
|
---|
| 8 | $datay = array();
|
---|
| 9 | $datax = array();
|
---|
| 10 | $a= 3.2;
|
---|
| 11 | $b= 2.5;
|
---|
| 12 | for($x=0; $x < 20; ++$x) {
|
---|
| 13 | $datax[$x] = $x;
|
---|
| 14 | $datay[$x] = $a + $b*$x + rand(-20,20);
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | $lr = new LinearRegression($datax, $datay);
|
---|
| 18 | list( $stderr, $corr ) = $lr->GetStat();
|
---|
| 19 | list( $xd, $yd ) = $lr->GetY(0,19);
|
---|
| 20 |
|
---|
| 21 | // Create the graph
|
---|
| 22 | $graph = new Graph(300,250);
|
---|
| 23 | $graph->SetScale('linlin');
|
---|
| 24 |
|
---|
| 25 | // Setup title
|
---|
| 26 | $graph->title->Set("Linear regression");
|
---|
| 27 | $graph->title->SetFont(FF_ARIAL,FS_BOLD,14);
|
---|
| 28 |
|
---|
| 29 | $graph->subtitle->Set('(stderr='.sprintf('%.2f',$stderr).', corr='.sprintf('%.2f',$corr).')');
|
---|
| 30 | $graph->subtitle->SetFont(FF_ARIAL,FS_NORMAL,12);
|
---|
| 31 |
|
---|
| 32 | // make sure that the X-axis is always at the
|
---|
| 33 | // bottom at the plot and not just at Y=0 which is
|
---|
| 34 | // the default position
|
---|
| 35 | $graph->xaxis->SetPos('min');
|
---|
| 36 |
|
---|
| 37 | // Create the scatter plot with some nice colors
|
---|
| 38 | $sp1 = new ScatterPlot($datay,$datax);
|
---|
| 39 | $sp1->mark->SetType(MARK_FILLEDCIRCLE);
|
---|
| 40 | $sp1->mark->SetFillColor("red");
|
---|
| 41 | $sp1->SetColor("blue");
|
---|
| 42 | $sp1->SetWeight(3);
|
---|
| 43 | $sp1->mark->SetWidth(4);
|
---|
| 44 |
|
---|
| 45 | // Create the regression line
|
---|
| 46 | $lplot = new LinePlot($yd);
|
---|
| 47 | $lplot->SetWeight(2);
|
---|
| 48 | $lplot->SetColor('navy');
|
---|
| 49 |
|
---|
| 50 | // Add the pltos to the line
|
---|
| 51 | $graph->Add($sp1);
|
---|
| 52 | $graph->Add($lplot);
|
---|
| 53 |
|
---|
| 54 | // ... and stroke
|
---|
| 55 | $graph->Stroke();
|
---|
| 56 |
|
---|
| 57 | ?>
|
---|
| 58 |
|
---|
| 59 |
|
---|