Fredrik Nylén
Fredrik Nylén

Reputation: 577

Kappa applied to all pairwise combinations of raters, and store the result

For a visualisation, I need to compute the weighted kappa of all combinations / pairs of seven raters. So, if I use some sample data with seven columns:

ex <- structure(list(`1` = c(1, 1, 2, 1, 2, 1, 1, 1, 2, 3, 1, 1, 1, 
1, 1, 1, 2, 1, 2, 2), `2` = c(1, 1, 1, 1, 2, 1, 1, 1, 2, 3, 1, 
2, 1, 1, 2, 2, 1, 2, 2, 2), `3` = c(1, 1, 2, 1, 2, 1, 1, 1, 2, 
3, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2), `4` = c(1, 1, 2, 2, 2, 1, 2, 
2, 2, 3, 2, 1, 1, 2, 2, 2, 1, 1, 2, 2), pa = c(1, 2, 1, 2, 3, 
1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 1, 3, 2), ta = c(2, 2, 2, 
1, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 3), ka = c(1, 
1, 2, 1, 2, 1, 1, 1, 2, 3, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))

I would like to get a structure that captures the output of irr::kappa2 on all combinations of columns. A structure like:

out <- data.frame("1_2"=irr::kappa2(ex[c(1,2)]),
"1_3"=irr::kappa2(ex[c(1,3)]),"1_4"=irr::kappa2(ex[c(1,3)]),....

(for all unique combinations of columns).

Any ideas?

Upvotes: 0

Views: 230

Answers (1)

Ric S
Ric S

Reputation: 9247

A solution would be to store the whole output structure of function kappa2 into an element of a list, and to have an element for each possible combination of columns:

# initialization
out_list <- list()
column <- 1

# cycle for storing kappa2's output structure
for (i in 1:(ncol(ex)-1)){
    for (j in (i+1):ncol(ex)){
        out_list[[column]] <- irr::kappa2(ex[,c(i,j)])
        # renaming the elements
        names(out_list)[column] <- paste0(i, "_", j)
        column <- column + 1
    }
}

In case you just want the Kappa value for each pair of columns, like you said in the comments, you can use the following (very similar to before) code:

# initialization
# the number of columns of "out" is from mathematics
out <- as.data.frame(matrix(0, nrow = 1, ncol = ncol(ex) * (ncol(ex)-1) / 2))
column <- 1

# cycle for calculation kappa
for (i in 1:(ncol(ex)-1)){
    for (j in (i+1):ncol(ex)){
        out[1,column] <- irr::kappa2(ex[,c(i,j)])$value
        colnames(out)[column] <- paste0(i, "_", j)
        column <- column + 1
    }
}

Upvotes: 2

Related Questions