PHP Classes
elePHPant
Icontem

File: weightedexample.php

Recommend this page to a friend!
  Classes of Greg Neyman  >  Weighted PHP Statistics  >  weightedexample.php  >  Download  
File: weightedexample.php
Role: Example script
Content type: text/plain
Description: Example
Class: Weighted PHP Statistics
Calculate statistics on weighted data sets
Author: By
Last change:
Date: 6 months ago
Size: 1,994 bytes
 

 

Contents

Class file image Download
<?php
include 'weightedstats.php';

//First argument is the variable of interest, in array form
$inputs = array(222,190,49,106,148,464,221,134,109,90,224,63,262,182,87,166,75,291,205,155,432,226,259,257,297,94,93,120,138,224,77,276,323,78,98,45,48,63,118,410,116,1091,449,1344,201,23,346,271,214,184,25,92,185,41,225,284,303,248,35,409,125,1279,153,184,226,59,160,88,304,408,33,337,615,20,97,208,334,560,306,142,133,19,274,56,167,104,206,366,56,4476,392,222,272,119,255,362,23,239,292,357);

//Second argument is the sample weight, where index 0 is the weight for $inputs[0], etc
$weights = array(795,948,1665,8134,2140,9884,14275,7242,7601,4160,14228,12147,2082,7071,20988,7889,6480,795,10062,2740,5415,8391,14312,14228,7791,2184,5522,14275,12649,3866,4346,4774,6083,15805,10875,5112,1444,20988,8224,14037,14312,7071,2328,10321,963,948,9547,8883,2870,1352,7403,7451,3181,6652,6439,3268,18789,3064,800,3913,7062,2991,10321,9343,9874,4550,4782,2991,5415,9547,2328,3622,3181,1444,6652,2740,4071,2450,4335,1351,2930,948,3622,7062,35714,1931,11584,7442,7986,9206,2450,18132,4346,1369,1448,5272,4931,9327,4445,21477);

//The two arrays must match lengths, otherwise a fatal error is thrown
$ws = new weightedstats($inputs, $weights);

print
"<html><body><h1>Weighted Statistics Class Example</h1><dl>";
print
"<dt>Arithmetic Mean</dt>";
print
"<dd>The following is the <b>incorrect</b> mean for a weighted data set: ";
print
array_sum($inputs)/count($inputs);
print
"</dd><dt>Weighted Mean</dt>";
print
"<dd>This is the correct way to perform the mean when the data provided has statistical weights: ";
print
$ws->average();
print
"</dd><dt>Standard Deviation</dt><dd>";
print
$ws->stdev();
print
"</dd><dt>Quartile 1</dt><dd>";
print
$ws->percentile(25);
print
"</dd><dt>Median</dt><dd>";
print
$ws->percentile(50);
print
"</dd><dt>Quartile 3</dt><dd>";
print
$ws->percentile(75);
print
"</dd><dt>Skew</dt><dd>";
print
$ws->skew();
print
"</dd></dl></body></html>";
?>