2

I created a loop to calculate the icc between two raters. For each rater (R1, R2) I have a data frame of the 75 variables in columns and 125 observations.

library(irr)
for (i in 1:75) {
 icc <- icc(cbind.data.frame(R1[,i],R2[,i]), model="twoway", type="agreement",
     unit="single")
 print(icc)
}

icc returns as a list of results icc for each variable. I tried to integrate in the loop a function that will generate a data frame for the objects of icc that interest me (value, lower and upper bounder of the 95% confident interval) but it returns in different ways separate tables:

With this first attempt it returns 75 data frames of only one line each one, even if I used an rbind command

for (i in 1:75) {
  icc <- icc(cbind.data.frame(R1[,i],R2[,i]), model="twoway", type="agreement",
      unit="single")

  print(rbind.data.frame(cbind.data.frame(icc$value,icc$lbound,icc$ubound)))
  }

in the second case it returns 75 different data frames filled each one of the icc'objects of one variable.

for (i in 1:75) {
  icc <- icc(cbind.data.frame(R1[,i],R2[,i]), model="twoway", type="agreement",
      unit="single")

name_lines_are_variables <- names(L1)
name_columns <- c("ICC","Low CI 95%","Up CI 95%)
tab <- matrix(c(icc$value,icc$conf.level),nrow=38,ncol=2)
dimnames(tab) <- list(name_lines_are_variables,name_columns)
print(tab)

I appreciate your help

Miricy
  • 35
  • 6

2 Answers2

1

If I've understood your post correctly, then the problem with your code is that it the results from the icc() function are not being accumulated.

You can solve this problem by declaring an empty data.frame before the for loop, and then using rbind() to append the latest results to the existing results in this data.frame.

Please refer to the code below for an implementation (refer to the comments for clarifications):

rm(list = ls())

#Packages
library(irr)

#Dummy data
R1 <- data.frame(matrix(sample(1:100, 75*125, replace = TRUE), nrow = 75, ncol = 125))
R2 <- data.frame(matrix(sample(1:100, 75*125, replace = TRUE), nrow = 75, ncol = 125))


#Data frame that will accumulate the ICC results
#Initialized with zero rows (but has named columns)
my_icc <- data.frame(R1_col = character(), R2_col = character(), 
                     icc_val = double(), icc_lb = double(), 
                     icc_ub = double(), icc_conflvl = double(), 
                     icc_pval = double(), 
                     stringsAsFactors = FALSE)


#For loop
#Iterates through each COLUMN in R1 and R2
#And calculates ICC values with these as inputs
#Each R1[, i]-R2[, j] combination's results are stored
#as a row each in the my_icc data frame initialized above
for (i in 1:ncol(R1)){
  for (j in 1:ncol(R2)){

    #tmpdat is just a temporary variable to hold the current calculation's data
    tmpdat <- irr::icc(cbind.data.frame(R1[, i], R2[, j]), model = "twoway", type = "agreement", unit = "single")

    #Results from current cauculation being appended to the my_icc data frame
    my_icc <- rbind(my_icc, 
                    data.frame(R1_col = colnames(R1)[i], R2_col = colnames(R2)[j], 
                               icc_val = tmpdat$value, icc_lb = tmpdat$lbound, 
                               icc_ub = tmpdat$ubound, icc_conflvl = tmpdat$conf.level, 
                               icc_pval = tmpdat$p.value, 
                               stringsAsFactors = FALSE))


  } 
}

head(my_icc)
#   R1_col R2_col     icc_val      icc_lb    icc_ub icc_conflvl  icc_pval
# 1     X1     X1  0.14109954 -0.09028373 0.3570681        0.95 0.1147396
# 2     X1     X2  0.07171398 -0.15100798 0.2893685        0.95 0.2646890
# 3     X1     X3 -0.02357068 -0.25117399 0.2052619        0.95 0.5791774
# 4     X1     X4  0.07881817 -0.15179084 0.3004977        0.95 0.2511141
# 5     X1     X5 -0.12332146 -0.34387645 0.1083129        0.95 0.8521741
# 6     X1     X6 -0.17319598 -0.38833452 0.0578834        0.95 0.9297514
Dunois
  • 1,813
  • 9
  • 22
  • Thank you a lot for your help. I just had to chan – Miricy May 18 '20 at 09:04
  • You didn't even have to change that really unless you needed to save on computing time. You could just subset the final data.frame like so `my_icc[my_icc$R1_col == my_icc$R2_col, ]`. I'm happy I could help! – Dunois May 18 '20 at 10:23
1

Thank you a lot for your help @Dunois. I just had to keep the same variable in the for() loop, because I have to compare the same variables columns for each rater, so the final code :

library(irr)

R1 <- data.frame(matrix(sample(1:100, 75*125, replace = TRUE), nrow = 75, ncol = 125))
R2 <- data.frame(matrix(sample(1:100, 75*125, replace = TRUE), nrow = 75, ncol = 125))

my_icc <- data.frame(R1_col = character(), R2_col = character(), 
                     icc_val = double(), icc_lb = double(), 
                     icc_ub = double(), icc_conflvl = double(), 
                     icc_pval = double(), 
                     stringsAsFactors = FALSE)

for (i in 1:ncol(R1)){

    tmpdat <- irr::icc(cbind.data.frame(R1[, i], R2[, i]), model = "twoway", type = "agreement", unit = "single")

    my_icc <- rbind(my_icc, 
                    data.frame(R1_col = colnames(R1)[i], R2_col = colnames(R2)[i], 
                               icc_val = tmpdat$value, icc_lb = tmpdat$lbound, 
                               icc_ub = tmpdat$ubound, icc_conflvl = tmpdat$conf.level, 
                               icc_pval = tmpdat$p.value, 
                               stringsAsFactors = FALSE))

}

head(my_icc)
#R1_col R2_col      icc_val     icc_lb     icc_ub icc_conflvl  icc_pval
#1     X1     X1  0.116928667 -0.1147526 0.33551788        0.95 0.1601141
#2     X2     X2  0.006627921 -0.2200660 0.23238172        0.95 0.4773967
#3     X3     X3 -0.184898902 -0.3980084 0.04542289        0.95 0.9427605
#4     X4     X4  0.066504226 -0.1646006 0.28963006        0.95 0.2862440
#5     X5     X5 -0.035662755 -0.2603757 0.19227801        0.95 0.6196883
#6     X6     X6 -0.055329309 -0.2808315 0.17466685        0.95 0.6805675

Miricy
  • 35
  • 6