r - 使用循环在 r 中创建具有 ICC 结果的表
问题描述
我创建了一个循环来计算两个评分者之间的 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()
函数的结果没有被累积。
data.frame
您可以通过在 之前声明一个空来解决此问题for loop
,然后使用rbind()
将最新结果附加到 this 中的现有结果中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
推荐阅读
- java - 如何使用列表过滤多值映射
- css - 使用 bootstrap 5 时,如何摆脱此列下方的额外空间?
- polygon - 我可以在 R 中的多边形周围生成规则间隔的点吗?
- javascript - 下载的 html 上的 unicode 字符串被破坏
- python - 执行Python多处理时的图像路径问题
- html - 如何在角材料表中显示选定的值?
- react-native - flex:1 让我的视图在 react-native 中消失
- java - Resteasy微配置客户端隐藏响应错误
- macos - macOS Core Location desiredAccuracy = kCLLocationAccuracyReduced,这行得通吗?
- java - 当我将 JPA(hibernate) 与本机查询一起使用时,表 'database_name.CTE' 不存在