Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > SIMPLE LINEAR REGRESSION WITH PHP: PART 2


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Simple linear regression with PHP: Part 2
By Paul Meagher - 2004-05-21 Page:  1 2 3 4 5 6 7 8 9

Graphical output

The output methods you have implemented so far display the summary values in HTML format. It would also be desirable to display scatter plots and line plots of this data in GIF, JPEG, or PNG format.

Rather than writing the code for generating line and scatter plots myself, I thought it would be best to use the PHP-based graphics library called JpGraph. JpGraph is under active development by Johan Persson and is described as follows on the project Web site:

JpGraph makes it easy to draw both "quick and dirty" graphs with a minimum of code and complex professional graphs, which requires a very fine grain control. JpGraph is equally well suited for both scientific and business types of graphs.

The JpGraph distribution contains a large number of example scripts that can be customized for your particular needs. Using JpGraph for the data-exploration tool was a simple matter of finding an example script that did something similiar to what I wanted and adapting it to my particular requirements.

The script in Listing 3 is extracted from the sample data-exploration tool (explore.php) and demonstrates how the library is invoked and how the data from the SimpleLinearRegression analysis is fed into Line and Scatter classes. The comments in this code are by Johan Persson (the JPGraph codebase is well documented).

Listing 3. Detailed functions from the sample data-exploration tool explore.php


<?php 

  // Snippet extracted from explore.php script 

  include ("jpgraph/jpgraph.php"); 
  include ("jpgraph/jpgraph_scatter.php"); 
  include ("jpgraph/jpgraph_line.php"); 

  // Create the graph 
  $graph = new Graph(300,200,'auto'); 
  $graph->SetScale("linlin"); 

  // Setup title   
  $graph->title->Set("$title"); 
  $graph->img->SetMargin(50,20,20,40);    
  $graph->xaxis->SetTitle("$x_name","center"); 
  $graph->yaxis->SetTitleMargin(30);      
  $graph->yaxis->title->Set("$y_name");  

  $graph->title->SetFont(FF_FONT1,FS_BOLD); 

  // 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 
  $sp1 = new ScatterPlot($slr->Y, $slr->X); 
  $sp1->mark->SetType(MARK_FILLEDCIRCLE); 
  $sp1->mark->SetFillColor("red"); 
  $sp1->SetColor("blue"); 
  $sp1->SetWeight(3); 
  $sp1->mark->SetWidth(4); 

  // Create the regression line 
  $lplot = new LinePlot($slr->PredictedY, $slr->X); 
  $lplot->SetWeight(2); 
  $lplot->SetColor('navy'); 

  // Add the pltos to the line 
  $graph->Add($sp1); 
  $graph->Add($lplot); 

  // ... and stroke 
  $graph_name = "temp/test.png"; 
  $graph->Stroke($graph_name); 
  ?> 
  <img src='<?php echo $graph_name ?>' vspace='15'> 

  ?>



View Simple linear regression with PHP: Part 2 Discussion

Page:  1 2 3 4 5 6 7 8 9 Next Page: Data-exploration script

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.