"Descriptive statistics," as the name suggests, gives a numerical description of your sample. These descriptions can be the location of the mean (average) and the variability of the sample (measured in standard deviation or % CV). In PHP, we can execute these calculations using functions. A function is a programming block which is aimed at attaining an objective (such as calculating an average, standard deviation or % coefficient of variation). In general, below is an example web application form written in PHP that can accept numerical data for descriptive statistics analysis: <html> <head> <title>Compute Descriptive Statistics of Numerical Data Using PHP by www.php-developer.org</title> </head> <body> <?php //Check if the form is submitted if (!$_POST['submit']) { //form not submitted, display form ?> <form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post"> Compute descriptive statistics such as mean, standard deviation and % CV for the following form.<br /> Copy and paste numerical data for analysis below (one data per line):<br /> <textarea name="figures" rows="50" cols="20"></textarea> <br /> <input type="submit" name="submit" value="Give me descriptive statistics of this sample numerical data"> </form> <a href="/descriptivestats.php">Click here to reset or clear this form</a> <?php } else { //form submitted, grab the data from POST $figures =trim($_POST['figures']); //test if it contains some data. if (!isset($figures) || trim($figures) == "") { //feedback to user that it contains no data die ('ERROR: Enter figures. <a href="/descriptivestats.php">Click here to proceed with the analysis</a>'); } else { //explode data and assign it to an array $data = explode("n", $figures); //function to compute statistical mean function average($data) { return array_sum($data)/count($data); } //function to compute standard deviation function stdev($data){ $average = average($data); foreach ($data as $value) { $variance[] = pow($value-$average,2); } $standarddeviation = sqrt((array_sum($variance))/((count($data))-1)); return $standarddeviation; } //compute % coefficient of variation $CV = ((stdev($data))/(average($data))) * 100; //function to compute median of the datasets function median($data) { sort($data); $arrangements = count($data); if (($arrangements % 2) == 0) { $i = $arrangements / 2; return (($data[$i - 1] + $data[$i]) / 2); } else { $i = ($arrangements - 1) / 2; return $data[$i]; } } //function to compute the range function statisticalrange($data) { return (max($data) - min($data)); } //display results to browser echo '<h2>Descriptive Statistics of the Analyzed Sample Data:</h2>'; echo '<br />'; echo 'The mean of the sample is: <b> '.round(average($data),4).'</b>'; echo '<br />'; echo 'The standard deviation of the sample is: <b> '.round(stdev($data),4).'</b>'; echo '<br />'; echo 'The %coefficient of variation is (data in percent): <b> '.round($CV,4).'</b>'; echo '<br />'; echo 'The median of the sample is: <b>'.median($data).'</b>'; echo '<br />'; echo 'The maximum sample is: <b>'.round(max($data),4).'</b>'; echo '<br />'; echo 'The minimum sample is: <b>'.round(min($data),4).'</b>'; echo '<br />'; echo 'The statistical range of the sample is: <b> '.round(statisticalrange($data),4).'</b>'; echo '<br></br>'; echo 'Below is the submitted/analyzed data for your reference'; echo '<br></br>'; $display = implode("n <br />", $data); echo $display; echo '<br></br>'; echo '<a href="/descriptivestats.php">Click here to do another analysis</a>'; } } ?> </body> </html>
blog comments powered by Disqus |
|
|
|
|
|
|
|