r - 使用循环在 r 中创建包含 ICC 结果的表

标签 r dataframe for-loop icc reliability

我创建了一个循环来计算两个评估者之间的 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)

感谢您的帮助

最佳答案

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

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

请引用下面的代码进行实现(请参阅注释以进行说明):

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

关于r - 使用循环在 r 中创建包含 ICC 结果的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61857136/

相关文章:

r - 将一个目录中的大量文件合并到R中的数据框中

r - 在 map 上绘制绕纬度/经度的时间半径

r - 通过带有嵌入式传单 svg 等的 RSelenium 提取底层数据

r - 如何从数据框中的字符串中提取数字并将其放入新列中?

c - C语言嵌套for循环

r - 将带有时间单位(ms、s、us)的字符转换为数字

scala - 如何将 DataFrame 模式写入 Scala 中的文件

python - 如果列中存在匹配值,则使用另一个数据帧注释一个数据帧

c++ - { } 和等号变量之间的区别

PHP for 循环与带范围的 foreach