使用循环创建带有r中ICC结果的表

问题描述 投票:1回答:2

我创建了一个循环来计算两个评估者之间的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%置信区间的上下限),但它以不同的方式返回单独的表:

通过第一次尝试,即使我使用了rbind命令,它也只返回一行的75个数据帧

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

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

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
2个回答
0
投票

如果我正确理解了您的帖子,那么您的代码存在的问题是icc()函数的结果没有被[[累加。

您可以通过在data.frame之前声明一个空的for loop,然后使用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


0
投票
非常感谢@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") #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)[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

© www.soinside.com 2019 - 2024. All rights reserved.