rdataframefor-loopiccreliability

Using a loop to create table with results of ICC in r


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


Solution

  • 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