Is there a PHP Fleiss' Kappa implementation?
I'm looking for a PHP implementation of Fleiss' Kappa that is publicly available. So far, I found some classes written in Java, Perl, Ruby and Python, but nothing in PHP.
Do you have any idea or suggestion?
Thanks开发者_如何学编程!
Because I needed it too, I wrote this function:
function fleissKappa($table){
/*
* author Tom Aizenberg
*
* $table is an n x m array containing the classification counts
*
* adapted from the example in en.wikipedia.org/wiki/Fleiss'_kappa
*
*/
$subjects = count($table);
$classes = count($table[0]);
$raters = array_sum($table[0]);
for($q = 1; $q < count($table); $q++){
if(count($table[$q])!=$classes){
print("no equal number of classes.");
exit(0);
}
if(array_sum($table[$q])!=$raters){
print("no equal number of raters.");
exit(0);
}
}
$pj = array();
$pi = array();
for($j = 0; $j < $subjects; $j++){
$pi[$j] =0;
}
for($i = 0; $i < $classes; $i++){
$tpj = 0;
for($j = 0; $j < $subjects; $j++){
$tpj += $table[$j][$i];
$pi[$j] += $table[$j][$i]*$table[$j][$i];
}
$pj[$i] = $tpj/($raters*$subjects);
}
for($j = 0; $j < $subjects; $j++){
$pi[$j] = $pi[$j]-$raters;
$pi[$j] = $pi[$j]*(1/($raters*($raters-1)));
}
$pcarret = array_sum($pi)/$subjects;
$pecarret = 0;
for($i = 0; $i < count($pj);$i++){
$pecarret += $pj[$i]*$pj[$i];
}
$kappa = ($pcarret-$pecarret)/(1-$pecarret);
return $kappa;
}
Test it:
$test = array(
array(0,0,0,0,14),
array(0,2,6,4,2),
array(0,0,3,5,6),
array(0,3,9,2,0),
array(2,2,8,1,1),
array(7,7,0,0,0),
array(3,2,6,3,0),
array(2,5,3,2,2),
array(6,5,2,1,0),
array(0,2,2,3,7));
print(fleissKappa($test));
精彩评论