有没有PHP Fleiss的Kappa实现?

时间:2011-08-04 17:20:48

标签: php statistics classification

我正在寻找可公开发布的Fleiss' Kappa的PHP实现。到目前为止,我发现了一些用Java, Perl, Ruby and Python编写的类,但在PHP中没有。

您有任何想法或建议吗?

谢谢!

1 个答案:

答案 0 :(得分: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));