Home arrow PHP arrow Page 4 - Performing Inferential Statistical Analysis with PHP

The PHP Code - PHP

Statistics aren't just for Excel spreadsheets anymore. If you've been wondering how to use PHP to help you do a basic statistical analysis, keep reading. In this article you'll learn how to use PHP to help you compare data sets.

TABLE OF CONTENTS:
  1. Performing Inferential Statistical Analysis with PHP
  2. Overview of Statistical Comparison
  3. Programming Strategy in PHP
  4. The PHP Code
By: Codex-M
Rating: starstarstarstarstar / 2
October 07, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Below is the complete PHP script required to implement the above strategy.

<html>

<head>

<title>Statistical T-Test Online 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">

Compare data using Statistical T-test below at 95% confidence level.<br />

Copy and paste numerical data for analysis in each of the text area below (one number per line):<br /><br />

DATA1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATA2<p><textarea name="firstsample" rows="35" cols="5"></textarea>&nbsp;&nbsp;<textarea name="secondsample" rows="35" cols="5"></textarea></p>

<br />

<input type="submit" name="submit" value="Compare if the two data sets are statistically different">

</form>

<a href="/ttest.php">Click here to reset or clear this form</a>

<?php

}

else

{

//form submitted, grab the data from POST

//Connect to MySQL database after sanitizing the data

$username = "root";

$password = "xxx";

$hostname = "localhost";

$database = "ttest";

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

//select a database to work with

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");

$firstsample =trim($_POST['firstsample']);

$secondsample =trim($_POST['secondsample']);

//Escape variables for use in MySQL

//test if it contains some data.

if ((!isset($firstsample) || trim($firstsample) == "") || (!isset($secondsample) || trim($secondsample) == ""))

{

//feedback to user that it contains no data

die ('ERROR: Incomplete Form. <a href="/ttest.php">Click here to proceed with the analysis</a>');

}

else

{

//explode data and assign it to an array

$data1 = explode("n", $firstsample);

$data2 = explode("n", $secondsample);

////////////////////////////////

//ANALYSIS FOR 1ST DATA SETS///

///////////////////////////////

//function to compute statistical mean

function average1($data1) {

return array_sum($data1)/count($data1);

}

$dataaverage1 = average1($data1);

//function to compute standard deviation

function stdev1($data1){

$average1 = average1($data1);

foreach ($data1 as $value1) {

$variance1[] = pow($value1-$average1,2);

}

$standarddeviation1 = sqrt((array_sum($variance1))/((count($data1))-1));

return $standarddeviation1;

}

$datastandarddeviation1 = stdev1($data1);

//variance of data set 1

$datavariance1 = $datastandarddeviation1 * $datastandarddeviation1;

//number of data for 1st data set

$count1 =count($data1);

//variance over number of data

$sterror1 = $datavariance1/$count1;

////////////////////////////////

//ANALYSIS FOR 2nd DATA SETS///

///////////////////////////////

//function to compute statistical mean

function average2($data2) {

return array_sum($data2)/count($data2);

}

$dataaverage2 = average2($data2);

//function to compute standard deviation

function stdev2($data2){

$average2 = average2($data2);

foreach ($data2 as $value2) {

$variance2[] = pow($value2-$average2,2);

}

$standarddeviation2 = sqrt((array_sum($variance2))/((count($data2))-1));

return $standarddeviation2;

}

$datastandarddeviation2 = stdev2($data2);

//variance of data set 2

$datavariance2 = $datastandarddeviation2 * $datastandarddeviation2;

//number of data for 2nd data set

$count2 =count($data2);

//variance over number of data

$sterror2 = $datavariance2/$count2;

////////////////////////////

//COMPUTE STANDARD ERROR///

//////////////////////////

$sumerror=$sterror1+ $sterror2;

$standarderror=sqrt($sumerror);

///////////////////////////////////

//COMPUTE DIFFERENCE OF TWO MEANS//

///////////////////////////////////

$difference=$dataaverage1-$dataaverage2;

$meandifference=abs($difference);

////////////////////

//COMPUTE T-VALUE///

////////////////////

$tvalue=$meandifference/$standarderror;

///////////////////////////////

//COMPUTE DEGREES OF FREEDOM///

///////////////////////////////

$df=$count1 + $count2 -2;

///////////////////////////////////////////////

//EXTRACT CRITICAL T VALUE FROM THE DATABASE///

///////////////////////////////////////////////

$df = mysql_real_escape_string(stripslashes($df));

$result = mysql_query("SELECT `critical` FROM `ttest` WHERE `degrees`='$df'")

or die(mysql_error());

// store the record of the "example" table into $row

$row = mysql_fetch_array($result)

or die("Invalid query: " . mysql_error());

// Print out the contents of the entry

$criticaltvalue = $row['critical'];

//////////////////////////////////////////

//COMPARE CRITICAL AND COMPUTED T VALUE///

//////////////////////////////////////////

if ($tvalue > $criticaltvalue)

//they are statistical different

{

echo '<h2>T-Test Results of the Analyzed Samples:</h2>';

echo '<br />';

echo 'The two data sets are <b>statistical DIFFERENT at 95% confidence level.It says that the two samples are NOT the same.</b><br />';

echo 'The computed t-value is:&nbsp;'.$tvalue;

echo '<br />';

echo 'And the critical t-value is:&nbsp;'.$criticaltvalue;

echo '<br />';

echo 'The computed degrees of freedom is:&nbsp;'.$df;

echo '<br />';

echo '<a href="/ttest.php">Click here to do another analysis</a>';

}

else

//they are not statistically different

{

echo '<h2>T-Test Results of the Analyzed Samples:</h2>';

echo '<br />';

echo 'The two data sets are <b>NOT statistical different at 95% confidence level.It says that the two samples are the same.</b><br />';

echo 'The computed t-value is:&nbsp;'.$tvalue;

echo '<br />';

echo 'And the critical t-value is:&nbsp;'.$criticaltvalue;

echo '<br />';

echo 'The computed degrees of freedom is:&nbsp;'.$df;

echo '<br />';

echo '<a href="/ttest.php">Click here to do another analysis</a>';

}

echo '<br></br>';

echo 'Below is the submitted/analyzed data for your reference';

echo '<br></br>';

$display1 = implode("n <br />", $data1);

$display2 = implode("n <br />", $data2);

echo '<table border="1">';

echo '<tr>';

echo '<th>Data</th>';

echo '</tr>';

echo '<tr><td>'.$display1.'</td></tr>';

echo '<tr><td>'.$display2.'</td></tr>';

echo '</table>';

}

}

?>

</body>

</html> 

You can test a full, working version of this script to see it in action. You can also download (copy and paste) the script.



 
 
>>> 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: