2011-08-04 44 views

回答

0

因爲我需要它太,我寫了這個功能:

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 = 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));