Take Web data analysis to the next level with PHP
By Paul Meagher2004-06-15
The Constructor: Backbone of the Chi Square test
Listing 4 looks at the Chi Square constructor code which forms the backbone of the Chi Square test.
Listing 4. The Chi Square constructor
<?php
class ChiSquare1D {
function ChiSquare1D($ObsFreq, $Alpha=0.05, $ExpProb=FALSE) {
$this->ObsFreq = $ObsFreq;
$this->ExpProb = $ExpProb;
$this->Alpha = $Alpha;
$this->NumCells = count($this->ObsFreq);
$this->DF = $this->NumCells - 1;
$this->Total = $this->getTotal();
$this->ExpFreq = $this->getExpFreq();
$this->ChiSqObt = $this->getChiSqObt();
$this->ChiSqCrit = $this->getChiSqCrit();
$this->ChiSqProb = $this->getChiSqProb();
return true;
}
}
?>
Four noteworthy aspects of the constructor method are:
1. The constructor accepts an array of observed frequencies, an alpha probability cutoff score, and an optional array of expected probabilities.
2. The first six lines involve relatively simple assignments and computed values that are recorded so a complete result object is available to calling scripts.
3. The final four lines do the bulk of the work in obtaining the Chi Square statistics you are most interested in.
4. The class implements only the Chi Square test logic. No output methods are associated with this class.
You can examine the class methods included in the code download for this article to find out more about how each result object value is computed (see Resources).
Tutorial Pages:
» Take Web data analysis to the next level with PHP
» Relate Web data to experimental design
» Examples of measurement scales
» Start with the sampling
» Test the hypothesis
» Model the null hypothesis: The Chi Square statistic
» Look at the Chi Square sampling distribution
» Chi Square instance variables
» The Constructor: Backbone of the Chi Square test
» Handle output issues
» Repoll
» Apply the knowledge
» Resources
First published by IBM developerWorks
