Weighted kappa example in PHP and Java
I was looking for a weighted kappa code, but could not find one. Here is the solution. First in PHP then Java.
<?php
$kappa[0][0]=44;
$kappa[0][1]=5;
$kappa[0][2]=1;
$kappa[1][0]=7;
$kappa[1][1]=20;
$kappa[1][2]=3;
$kappa[2][0]=9;
$kappa[2][1]=5;
$kappa[2][2]=6;
findKappa($kappa);
function findKappa($kappa){
#we have the counts stored in an nxn matrix
#convert "observed matrix" from counts to frequencies
$sum = sumOfAllElements($kappa);
foreach($kappa as $i=>$r){
foreach($r as $j=>$c){
$kappa[$i][$j] = $kappa[$i][$j]/$sum;
}
}
#echo "observed matrix:".json_encode($matrix)."</p>";
#now, let's find the "chance matrix"
$chance=array();
foreach($kappa as $i=>$r){
foreach($r as $j=>$c){
$chance[$i][$j] =
array_sum(getRow($kappa, $i))*array_sum(getColumn($kappa, $j));
}
}
#echo "chance matrix:".json_encode($chance)."</p>";
#we will use a weight matrix
#there are two main ways to calculate the weight matrix;
#linear or quadratic
#we will use the linear one
$weight = array();
$rowCount = count($kappa);
#echo "each dimension:".$rowCount;
#$rowCount = $columnCount, because the observation matrix is an nxn matrix.
for($i=0;$i<$rowCount;$i++){
for($j=0;$j<$rowCount;$j++){
$weight[$i][$j]=1-(abs($i-$j)/($rowCount-1));
#this would be the quadratic one:
#$weight[$i][$j]=1-pow((abs($i-$j)/$rowCount),2);
}
}
#echo "weight matrix:".json_encode($weight)."</p>";
#now, 1)multiply each element in the observed matrix
#by corresponding weight element and sum it all
#2)do the same thing with chance matrix
$sumOfObserved = 0;
$sumOfChance = 0;
for($i=0;$i<$rowCount;$i++){
for($j=0;$j<$rowCount;$j++){
$sumOfObserved += $kappa[$i][$j]*$weight[$i][$j];
$sumOfChance += $chance[$i][$j]*$weight[$i][$j];
}
}
#the formula for kappa is this:
$kappaValue = ($sumOfObserved-$sumOfChance)/(1-$sumOfChance);
echo "<h3>here is your kappa value:".$kappaValue."</h3>";
}
function getArrayFirstIndex($arr){
foreach ($arr as $key => $value)
return $key;
}
function sumOfAllElements($matrix){
$sum = 0;
foreach($matrix as $row){
$sum+=array_sum($row);
}
return $sum;
}
function getRow($matrix,$row){
return $matrix[$row];
}
function getColumn($matrix,$column){
$col = array();
foreach($matrix as $row){
#add each $row[$column] to the $column array
$col[]=$row[$column];
}
return $col;
}
?>

public class Test {
public static void main(String args[]){
double kappa[][] = new double[3][3];
kappa[0][0]=44;
kappa[0][1]=5;
kappa[0][2]=1;
kappa[1][0]=7;
kappa[1][1]=20;
kappa[1][2]=3;
kappa[2][0]=9;
kappa[2][1]=5;
kappa[2][2]=6;
findKappa(kappa);
}
static void findKappa(double [][]kappa){
//we have the counts stored in an nxn matrix
//convert "observed matrix" from counts to frequencies
double sum = sumOfAllElements(kappa);
for (int k = 0; k < kappa.length; k++) {
for (int k2 = 0; k2 < kappa.length; k2++) {
kappa[k][k2] = kappa[k][k2]/sum;
}
}
//echo "observed matrix:".json_encode(matrix)."</p>";
//now, let's find the "chance matrix"
double chance[][] = new double[3][3];
for (int k = 0; k < kappa.length; k++) {
for (int k2 = 0; k2 < kappa.length; k2++) {
chance[k][k2] =
array_sum(getRow(kappa, k))*array_sum(getColumn(kappa, k2));
}
}
//echo "chance matrix:".json_encode(chance)."</p>";
//we will use a weight matrix
//there are two main ways to calculate the weight matrix;
//linear or quadratic
//we will use the linear one
double weight[][] = new double[3][3];
double rowCount = kappa.length;
//echo "each dimension:".rowCount;
//rowCount = columnCount, because the observation matrix is an nxn matrix.
for(int i=0;i<rowCount;i++){
for(int j=0;j<rowCount;j++){
weight[i][j]=1-(Math.abs(i-j)/(rowCount-1));
//this would be the quadratic one:
//weight[i][j]=1-pow((abs(i-j)/rowCount),2);
}
}
//echo "weight matrix:".json_encode(weight)."</p>";
//now, 1)multiply each element in the observed matrix
//by corresponding weight element and sum it all
//2)do the same thing with chance matrix
double sumOfObserved = 0;
double sumOfChance = 0;
for(int i=0;i<rowCount;i++){
for(int j=0;j<rowCount;j++){
sumOfObserved += kappa[i][j]*weight[i][j];
sumOfChance += chance[i][j]*weight[i][j];
}
}
//the formula for kappa is this:
double kappaValue = (sumOfObserved-sumOfChance)/(1-sumOfChance);
System.out.println("here is your kappa value:"+kappaValue);
}
static double array_sum(double arr[]){
double sum=0;
for (int i=0; i < arr.length; i++)
sum+=arr[i];
return sum;
}
static double sumOfAllElements(double[][] matrix){
double sum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
sum+=matrix[i][j];
}
}
return sum;
}
static double[] getRow(double matrix[][],int row){
return matrix[row];
}
static double[] getColumn(double matrix[][],int column){
double col[]=new double[3];
for (int i = 0; i < matrix.length; i++) {
col[i]=matrix[i][column];
}
return col;
}
}
Leave a Comment
