Reputation: 928
I am using PHP version 5.2.13 with Kohana framework v2.3.4 and I want to calculate the standard deviation.
I found a function in the PHP manual: stats_standard_deviation
The problem is that when I try I get this error:
Fatal error: Call to undefined function stats_standard_deviation() in /folder/test.php on line 1799
This is the code I am using:
function std_dev ($attr, $test1,$test2,$test3,$test4,$test5,$test6) {
$items[] = array();
if (isset($test1) && $test1->$attr != 9 && $test1->$attr != 0) {
$items[] = $test1->$attr;
}
if (isset($test2) && $test2->$attr != 9 && $test2->$attr != 0) {
$items[] = $test2->$attr;
}
if (isset($test3) && $test3->$attr != 9 && $test3->$attr != 0) {
$items[] = $test3->$attr;
}
if (isset($test4) && $test4->$attr != 9 && $test4->$attr != 0) {
$items[] = $test4->$attr;
}
if (isset($test5) && $test5->$attr != 9 && $test5->$attr != 0) {
$items[] = $test5->$attr;
}
if (isset($test6) && $test6->$attr != 9 && $test6->$attr != 0) {
$items[] = $test6->$attr;
}
$standard_deviation = stats_standard_deviation($items);
return round($standard_deviation,2);
}
All help will be appreciated.
Thanks!
Upvotes: 3
Views: 3240
Reputation: 518
As the comments said, the PECL package is not installed in your system, look here to install.
But, if you can't install, or you don't want to install it, you can use this function
function std_deviation($arr){
$arr_size=count($arr);
$mu=array_sum($arr)/$arr_size;
$ans=0;
foreach($arr as $elem){
$ans+=pow(($elem-$mu),2);
}
return sqrt($ans/$arr_size);
}
That follows the standard deviation formula.
Upvotes: 4