O'Reilly Databases

oreilly.comSafari Books Online.Conferences.

We've expanded our coverage and improved our search! Search for all things Database across O'Reilly!

Search Search Tips

advertisement
AddThis Social Bookmark Button

Print Subscribe to Databases Subscribe to Newsletters

ANOVA Statistical Programming with PHP
Pages: 1, 2, 3, 4

Step 3: Show Box Plot

Early in your analysis you should graph your data so that you are being guided by proper overall intuitions about your data. A commonly recommended way to visualize ANOVA data is to use side-by-side box plots for each treatment level. To generate these box plots we need to compute a five-number summary for each treatment level, consisting of the minimum value, first quartile, median, third quartile, and maximum value. The showBoxPlot($params) method computes these summary values and uses them to generate the treatment-level box plots.

Test Score by Anxiety Level
Figure 2 — box plot of test anxiety data

The showBoxPlot($params) method is actually a wrapper around JpGraph library methods. The $params argument allows us to supply parameter values needed to fine tune JpGraph's output as you can see if you examine the method source code:

<?php
/**
* The showBoxPlot method is a wrapper for JPGRAPH methods.  
* The JpGraph constant defining to the root of the JpGraph
* library is specified in the config.php file.  
*
* I only do very basic $params array handling in this method.  There
* is a lot of room for improvement in making parameter handling more
* extensive (i.e., so you have more control over aspects of your plot)
* and more intelligent.
*/
function showBoxPlot($params=array()) {
  include_once JPGRAPH . "/src/JpGraph.php";
  include_once JPGRAPH . "/src/jpgraph_stock.php";

  $summary = $this->getFiveNumberSummary();
  $yData   = array();

  foreach($this->levels AS $level) {
    // Data must be in the format : q1, q3, min, max, median.
    $plot_parts = array("q1","q3","min","max","median");

    foreach($plot_parts AS $part) {
      $yData[] = $summary[$level][$part];
    }
  }

  $figureTitle = $params["figureTitle"];
  $xTitle      = $params["xTitle"];
  $yTitle      = $params["yTitle"];

  if(!isset($figureTitle))
    $figureTitle = "Figure 1";

  if(!isset($xTitle))
    $xTitle = "x-level";    

  if(!isset($yTitle)) 
    $yTitle = "y-level";        

  $plotWidth  = 400;
  $plotHeight = 300;
  $yMin       = $params["yMin"];
  $yMax       = $params["yMax"];
  $yTicks     = $params["yTicks"];
  $yMargin    = 35;
  $xMargin    = 15;
  $xLabels    = $this->levels;

  $graph = new Graph($plotWidth, $plotHeight);

  $graph->SetFrame(false);
  $graph->SetMarginColor('white');
  $graph->title->Set($figureTitle);
  $graph->SetScale("textlin", $yMin, $yMax);
  $graph->yscale->ticks->Set($yTicks);
  $graph->xaxis->SetTickLabels($xLabels);
  $graph->xaxis->SetTitle($xTitle,"center");
  $graph->xaxis->SetTitleMargin($xMargin);
  $graph->yaxis->SetTitle($yTitle, "center");
  $graph->yaxis->SetTitleMargin($yMargin);

  // Create a new box plot.
  $bp = new BoxPlot($yData);

  // Indent bars so they don’t start and end at the edge of the plot area.
  $bp->SetCenter();

  // Width of the bars in pixels.
  $bp->SetWidth(25);

  // Set bar colors
  $bp->SetColor('black','white','black','white');

  // Set median colors
  $bp->SetMedianColor('black','black');
  $graph->Add($bp);

  if (isset($params["outputFile"])) {
    $outputFile = $params["outputFile"];
    $graph_name = "temp/$outputfile";      
  } else {
    $graph_name = "temp/boxplot.png";
  }

  $graph->Stroke($graph_name);

  echo "<img src='$graph_name' vspace='15' alt='$figureTitle'>";      
}
?>

Step 4: Show Descriptive Statistics

Another report that we will want to see early in our analysis is a descriptive statistics report. The descriptive statistics table comes directly from the showDescriptiveStatistics() method.

Table 2. Show Descriptive Statistics

Anxiety Levels
lowmoderatehigh
mean48.4048.0057.50
stdev12.6411.639.29
n101010

Pages: 1, 2, 3, 4

Next Pagearrow




Tagged Articles

Be the first to post this article to del.icio.us

Related to this Article

Data Jujitsu: The Art of Turning Data into Product Data Jujitsu: The Art of Turning Data into Product
November 2012
$0.00 USD

Designing Great Data Products Designing Great Data Products
March 2012
$0.00 USD

Sponsored Resources

  • Inside Lightroom
Advertisement
O'reilly

© 2013, O’Reilly Media, Inc.

(707) 827-7019 (800) 889-8969

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.

About O'Reilly

  • Academic Solutions
  • Jobs
  • Contacts
  • Corporate Information
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly

Community

  • Authors
  • Community & Featured Users
  • Forums
  • Membership
  • Newsletters
  • O'Reilly Answers
  • RSS Feeds
  • User Groups

Partner Sites

  • makezine.com
  • makerfaire.com
  • craftzine.com
  • igniteshow.com
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com

Shop O'Reilly

  • Customer Service
  • Contact Us
  • Shipping Information
  • Ordering & Payment
  • The O'Reilly Guarantee