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 DATA2<p><textarea name="firstsample" rows="35" cols="5"></textarea> <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: '.$tvalue; echo '<br />'; echo 'And the critical t-value is: '.$criticaltvalue; echo '<br />'; echo 'The computed degrees of freedom is: '.$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: '.$tvalue; echo '<br />'; echo 'And the critical t-value is: '.$criticaltvalue; echo '<br />'; echo 'The computed degrees of freedom is: '.$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.
blog comments powered by Disqus |
|
|
|
|
|
|
|