【问题标题】:Using a loop to create table with results of ICC in r使用循环在 r 中创建具有 ICC 结果的表
【发布时间】:2020-05-17 18:55:31
【问题描述】:

我创建了一个循环来计算两个评分者之间的 icc。 对于每个评估者(R1,R2),我有一个包含 75 个列变量和 125 个观察值的数据框。

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 返回每个变量的结果 icc 列表。 我试图在循环中集成一个函数,该函数将为我感兴趣的 icc 对象(值、95% 置信区间的下限和上限)生成数据框,但它以不同的方式返回单独的表:

第一次尝试返回 75 个数据帧,每帧只有一行,即使我使用了 rbind 命令

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)))
  }

在第二种情况下,它返回 75 个不同的数据帧,填充一个变量的每个 icc'对象。

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)

感谢您的帮助

【问题讨论】:

    标签: r dataframe for-loop icc reliability


    【解决方案1】:

    如果我正确理解了您的帖子,那么您的代码的问题在于 icc() 函数的结果没有累积

    您可以通过在for loop 之前声明一个空的data.frame,然后使用rbind() 将最新结果附加到此data.frame 中的现有结果中来解决此问题。

    请参阅下面的代码以获取实现(请参阅 cmets 以获得说明):

    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
    

    【讨论】:

    • 非常感谢您的帮助。我只需要chan
    • 除非您需要节省计算时间,否则您甚至不必真正更改它。您可以像my_icc[my_icc$R1_col == my_icc$R2_col, ] 那样对最终的data.frame 进行子集化。我很高兴能帮上忙!
    【解决方案2】:

    非常感谢@Dunois 的帮助。我只需要在for() 循环中保留相同的变量,因为我必须为每个评估者比较相同的变量列,所以最终代码:

    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
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多