| 
<?php  
 $sep = "\t"; $nl  = "\n";
 
 $content = file_get_contents('data.txt');
 
 $records = explode($nl, $content);
 $header  = explode($sep, trim(array_shift($records)));
 $data    = array_fill_keys($header, array());
 
 foreach ($records as $id=>$record) {
 $record = trim($record);
 if ($record == '') continue;
 
 $fields = explode($sep, $record);
 $titles = $header;
 
 foreach ($fields as $field) {
 $title = array_shift($titles);
 $data[$title][] = $field;
 }
 }
 
 $x = $data['wt'];
 $y = $data['mpg'];
 
 require('kashi.php');
 
 // $kashi = new Kashi($dbname, $dbuser, $dbpass, $dbhost);
 $kashi = new Kashi('test', 'root', '', 'localhost');
 
 /**
 * Summary Statistics:
 */
 
 // $x is an array of values
 echo 'Arithmetic Mean: ' . $kashi->mean($x) . '<br />';
 echo 'Aeometric Mean: '  . $kashi->mean($x, "geometric") . '<br />';
 echo 'Harmonic Mean: '   . $kashi->mean($x, "harmonic")  . '<br />';
 
 echo 'Mode: '; print_r($kashi->mode($x)); echo '<br />';
 echo 'Median: '   . $kashi->median($x)   . '<br />';
 echo 'Variance: ' . $kashi->variance($x) . '<br />';
 echo 'SD: '       . $kashi->sd($x)       . '<br />';
 echo '%CV: '      . $kashi->cv($x)       . '<br />';
 
 echo 'Skewness: ' . $kashi->skew($x)     . '<br />';
 echo 'Is it significant (i.e. test it against 0)? ';
 var_dump($kashi->isSkew($x));
 
 echo 'Kurtosis: ' . $kashi->kurt($x)     . '<br />';
 echo 'Is it significant (i.e. test it against 0)? ';
 var_dump($kashi->isKurt($x));
 
 echo 'Rank: ';
 echo implode(', ', $kashi->rank($x)) . '<br />';
 
 echo '<hr />';
 
 /**
 * Statistical Graphics
 */
 echo 'Boxplot: <br /><pre>';
 print_r($kashi->boxplot($x));
 echo '</pre><br />';
 
 echo 'Histogram: <br /><pre>';
 print_r($kashi->hist($x, 8));
 echo '</pre><br />';
 
 echo 'Normal Q-Q Plot: <br />';
 $qq = $kashi->qqnorm($x);
 echo 'x = ' . implode(', ', $qq['x']) . '<br />';
 echo 'y = ' . implode(', ', $qq['y']) . '<br />';
 
 echo '<hr />';
 
 /**
 * Correlation, Regression, and t-Test:
 */
 echo 'Covariance: '  . $kashi->cov($x, $y) . '<br />';
 echo 'Correlation: ' . $kashi->cor($x, $y) . '<br />';
 
 $r = $kashi->cor($x, $y);
 $n = count($x);
 echo 'Significant of Correlation: ' . $kashi->corTest($r, $n) . '<br />';
 
 echo 'Regression: <pre>' . print_r($kashi->lm($x, $y), true) . '</pre><br />';
 
 echo 't-Test unpaired: ' . $kashi->tTest($x, $y, false) . '<br />';
 echo 'Test (assumed equal variances): ' . $kashi->tDist($kashi->tTest($x, $y, false), $kashi->tTestDf($x, $y, true, false)) . '<br />';
 echo 'Test (assumed unequal variances): ' . $kashi->tDist($kashi->tTest($x, $y, false), $kashi->tTestDf($x, $y, false, false)) . '<br />';
 
 echo 't-Test paired: ' . $kashi->tTest($x, $y, true) . '<br />';
 echo 'Test: ' . $kashi->tDist($kashi->tTest($x, $y, true), $kashi->tTestDf($x, $y, false, true)) . '<br />';
 
 echo '<hr />';
 
 /**
 * Distributions:
 */
 echo 'Normal distribution (x=0.5, mean=0, sd=1): '  . $kashi->norm(0.5, 0, 1) . '<br />';
 
 echo 'Probability for the Student t-distribution (t=3, n=10) one-tailed: ';
 echo $kashi->tDist(3, 10, 1) . '<br />';
 
 echo 'Probability for the Student t-distribution (t=3, n=10) two-tailed: ';
 echo $kashi->tDist(3, 10, 2) . '<br />';
 
 echo 'F probability distribution (f=2, df1=12, df2=15): '  . $kashi->fDist(2, 12, 15) . '<br />';
 
 echo 'Inverse of the standard normal cumulative distribution (p=0.95): ';
 echo $kashi->inverseNormCDF(0.95) . '<br />';
 
 echo 't-value of the Student\'s t-distribution (p=0.05, n=29): ';
 echo $kashi->inverseTCDF(0.05, 29) . '<br />';
 
 echo 'Standardize (x) (i.e. mean=0 & variance=1): <br />';
 echo implode(', ', $kashi->standardize($x)) . '<br />';
 
 echo '<hr />';
 
 /**
 * Chi-square test or Contingency tables (A/B testing):
 */
 $table['Automatic'] = array('4 Cylinders' => 3, '6 Cylinders' => 4, '8 Cylinders' => 12);
 $table['Manual']    = array('4 Cylinders' => 8, '6 Cylinders' => 3, '8 Cylinders' => 2);
 
 $result = $kashi->chiTest($table);
 
 $probability = $kashi->chiDist($result['chi'], $result['df']);
 echo 'Chi-square test probability: ' . $probability . '<br />';
 
 echo '<hr />';
 
 /**
 * Diversity index:
 */
 $gear = array('3' => 15, '4' => 12, '5' => 5);
 $cyl  = array('4' => 11, '6' => 7, '8' => 14);
 
 echo 'Shannon index for gear: ' . $kashi->diversity($gear) . '<br />';
 echo 'Shannon index for cyl: ' . $kashi->diversity($cyl) . '<br />';
 
 echo '<hr />';
 
 /**
 * ANOVA:
 */
 require('kashi_anova.php');
 
 // $obj = new KashiANOVA($dbname, $dbuser, $dbpass, $dbhost);
 $obj = new KashiANOVA('test', 'root', '', 'localhost');
 
 $str = file_get_contents('anova_data.txt');
 $obj->loadString($str);
 
 // mpg ~ cyl
 $result = $obj->anova('cyl', 'mpg');
 echo 'Analysis of variance (ANOVA): mpg ~ cyl<pre>';
 print_r($result);
 echo '</pre>';
 
 echo '<hr />';
 
 /**
 * Cluster Analysis
 */
 require('kashi_cluster.php');
 $obj = new KashiCluster();
 $obj->dataLoad($data);
 $result = $obj->kMean(2);
 
 echo 'K-Means Clustering:<pre>';
 print_r($result);
 echo '</pre>';
 
 // Heretical tree output has no header, and consists of four columns. For each row, the first column is the
 // identifier of the node, the second and third columns are child nodes identifier, and the fourth column used
 // to determine the height of the node when rendering a tree.
 $tree = $obj->hClust();
 echo "<hr />Hierarchical Clustering:<pre>$tree</pre>";
 
 echo '<hr />';
 
 /**
 * Time Series Analysis
 */
 echo 'Moving Average: <br />' . implode(', ', $kashi->movingAvg($x, 5)) . '<br />';
 
 echo '<hr />';
 
 /**
 * Matrix functions
 */
 echo 'Matrix Functions: <br />';
 
 $A[1][1] = 1;
 $A[1][2] = 2;
 $A[2][1] = 3;
 $A[2][2] = 4;
 
 $B[1][1] = 5;
 $B[1][2] = 7;
 $B[2][1] = 6;
 $B[2][2] = 8;
 
 echo 'A + B = ';
 print_r($kashi->mAddition($A, $B));
 
 echo '<br />B - A = ';
 print_r($kashi->mSubtraction($B, $A));
 
 echo '<br />A * 2 = ';
 print_r($kashi->mMultiplication($A, 2));
 
 echo '<br />A * B = ';
 print_r($kashi->mMultiplication($A, $B));
 
 echo '<br />Transpose of B, t(B) = ';
 print_r($kashi->mTranspose($B));
 
 echo '<br />Determinat of A, |A| = ';
 print_r($kashi->mDeterminant($A));
 
 echo '<br />Cofactor of A = ';
 print_r($kashi->mCofactor($A));
 
 echo '<br />Adjoint of A = ';
 print_r($kashi->mAdjoint($A));
 
 echo '<br />Inverse of A = ';
 print_r($kashi->mInverse($A));
 
 echo '<hr />';
 
 /*
 * Path Analysis
 */
 echo 'Path Analysis: <br />';
 
 print_r($kashi->path($data['mpg'], array(1=>$data['wt'], $data['hp'], $data['qsec'])));
 
 echo '<hr />';
 
 /*
 * Solve Equations
 */
 echo 'Solve System of Linear Equations: <br />';
 
 $X[1] = array(1=>0, 2, 1);
 $X[2] = array(1=>1, 4, -1);
 $X[3] = array(1=>-1, 1, 1);
 
 $Y = array(1=>9, 23, 2);
 
 try{
 print_r($kashi->solve($X, $Y));
 } catch (KashiException $e) {
 echo 'Caught exception: ',  $e->getMessage(), '<hr/>';
 }
 
 echo '<hr />';
 
 |