. // Comparing Partitions website is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // //The following functions are used to construct a contigency table array ($cont_table) // from a 2 array ($ar1,$ar2) input each with a classification of a dataset //input (full_cont_table): $ar1,$ar2 //output:$cont_table function full_cont_table($ar1,$ar2){ $table=cont_table($ar1,$ar2); $table_headers=cont_table_headers($table); list($sumrow,$sumcol,$total)=cont_table_totals($table,$table_headers); $cont_table[values]=$table; $cont_table[headers]=$table_headers; $cont_table[totals][rows]=$sumrow; $cont_table[totals][columns]=$sumcol; $cont_table[Total]=$total; return($cont_table); } function cont_table($ar1,$ar2){ $n_ele=count($ar1);# TODO verify if size a1==size a2 for ($i=0; $i<=$n_ele-1;$i++){ $cont["$ar1[$i]"]["$ar2[$i]"]++; } return($cont); } function cont_table_headers($cont){ $xnames=array_keys($cont); foreach($xnames as $value ) { $piece=array_keys($cont[$value]); foreach($piece as $value2){ $ynames[]=$value2; } } $ynames=array_unique($ynames); $ct_headers[x]=$xnames; $ct_headers[y]=$ynames; return($ct_headers); } function cont_table_totals($cont,$ct_headers){ foreach($ct_headers[y] as $value){ $sumrow[$value]=0; foreach($ct_headers[x] as $value2){ if($cont["$value2"]["$value"]!=0) {$CV=$cont["$value2"]["$value"];} else {$CV=0;} $sumrow["$value"]=$sumrow["$value"]+$cont["$value2"]["$value"]; $sumcol["$value2"]=$sumcol["$value2"]+$cont["$value2"]["$value"]; } } #TOTALS ROW foreach($ct_headers[x] as $value3){ $total=$total+$sumcol[$value3]; } return(array($sumrow,$sumcol,$total)); } //This function calculates the mismatch matrix that are needed for the //partition agreement coeficients calculations //input: $cont_table (output from full_cont_table) //output:$a,$b,$c,$d,$N function mismatch_matrix($full_table_cont){ #Defining $a foreach($full_table_cont[headers][x] as $value){ foreach($full_table_cont[headers][y] as $value2){ $a=$a+($full_table_cont[values]["$value"]["$value2"]*($full_table_cont[values][$value][$value2]-1))/2; } } #Defining $A1 foreach($full_table_cont[totals][columns] as $value){ $A1=$A1+($value*($value-1))/2; } #Defining $b $b=$A1-$a; #Defining $A2 foreach($full_table_cont[totals][rows] as $value){ $A2=$A2+($value*($value-1))/2; } ##Defining $c $c=$A2-$a; ##defining d $N=$full_table_cont[Total]; $d=($N*($N-1)/2)-$A1-$c; return(array($a,$b,$c,$d,$N)); } // RAND, Adjusted RAnd and Wallace coefficient functions function randcoef($a,$b,$c,$d){ $Rand = ($a + $d)/($a + $b + $c + $d); return($Rand); } function adjust_rand($a,$b,$c,$d,$N){ //$Rand = ($a + $d)/($a + $b + $c + $d); $ARand = (($N*($N-1)/2)*($a+$d)-(($a+$b)*($a+$c)+($c+$d)*($b+$d)))/(pow($N*($N-1)/2,2)-(($a+$b)*($a+$c)+($c+$d)*($b+$d))); //return(array($Rand,$ARand)); return($ARand); } function wallace($a,$b,$c){ if($a+$b>0){ $Wallace1=$a/($a+$b); } else $Wallace1=0; if($a+$c>0){ $Wallace2=$a/($a+$c); }else $Wallace2=0; return(array($Wallace1,$Wallace2)); } //Simpson's index of diversity and 95% confidence interval calculation //input is an array of calssification //input: $ar1 //output: $SID (array...see function for details) function Simpsons_Index_Diversity($ar1){ $N=count($ar1); $unique_entries=array_unique($ar1); $ngroups=count($unique_entries); foreach($unique_entries as $values){ $freq[$values]=count(array_keys($ar1,$values)); } foreach($freq as $values){ $sumtot=$sumtot+(($values)*(($values)-1)); $sumFc2=$sumFc2+pow(($values/$N),2); $sumFc3=$sumFc3+pow(($values/$N),3); } if (($N*($N-1))>0){ $SID[value]=1-($sumtot/($N*($N-1))); } else $SID[value]=1; $sqrsumFc2=pow($sumFc2,2); $s2=(4/$N)*($sumFc3-$sqrsumFc2); $SID[low]=$SID[value]-2*sqrt($s2); $SID[high]=$SID[value]+2*sqrt($s2); $SID[ngroups]=count($freq); return($SID); } ?>