Developer Forums | About Us | Site Map


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


  // 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'); 

  // Setup title   


  // 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   

  // Create the scatter plot with some nice colors 
  $sp1 = new ScatterPlot($slr->Y, $slr->X); 

  // Create the regression line 
  $lplot = new LinePlot($slr->PredictedY, $slr->X); 

  // Add the pltos to the line 

  // ... and stroke 
  $graph_name = "temp/test.png"; 
  <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-2017 All rights reserved.
Article copyright and all rights retained by the author.