Home arrow PHP arrow Page 2 - Performing Descriptive Statistical Analysis with PHP

Computing descriptive statistics using PHP - PHP

Have you ever thought of doing statistical analysis using PHP? Well, as simple as this technology can be, it enables an analyst to do server-side scripting commands which accept data from a web form, and then analyze the data in the server using PHP.

TABLE OF CONTENTS:
  1. Performing Descriptive Statistical Analysis with PHP
  2. Computing descriptive statistics using PHP
  3. Detailed explanation of the scripts
  4. Performing calculations with PHP
By: Codex-M
Rating: starstarstarstarstar / 2
October 06, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

"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:&nbsp;<b> '.round(average($data),4).'</b>';

echo '<br />';

echo 'The standard deviation of the sample is:&nbsp;<b> '.round(stdev($data),4).'</b>';

echo '<br />';

echo 'The %coefficient of variation is (data in percent):&nbsp;<b> '.round($CV,4).'</b>';

echo '<br />';

echo 'The median of the sample is: &nbsp;<b>'.median($data).'</b>';

echo '<br />';

echo 'The maximum sample is:&nbsp;<b>'.round(max($data),4).'</b>';

echo '<br />';

echo 'The minimum sample is:&nbsp;<b>'.round(min($data),4).'</b>';

echo '<br />';

echo 'The statistical range of the sample is:&nbsp;<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>



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: