我想知道是否有办法将比较的逻辑矩阵转换为多重比较测试中使用的字母符号.如在multcomp::cld.
我的数据看起来像这样:
test_data <- data.frame(mean=c(1.48, 1.59, 1.81,1.94),CI_lower=c(1.29,1.38,1.54, 1.62),CI_upper=c(1.56,1.84, 2.3, 2.59))
mean CI_lower CI_upper
1 1.48 1.29 1.56
2 1.59 1.38 1.84
3 1.81 1.54 2.30
4 1.94 1.62 2.59
Run Code Online (Sandbox Code Playgroud)
我感兴趣的是一种符号,表示哪些条目具有重叠的CI,以获得如下所示的最终结果:
final <- data.frame(mean=c(1.48, 1.59, 1.81,1.94),CI_lower=c(1.29, 1.38,1.54, 1.62),CI_upper=c(1.56,1.84, 2.3, 2.59),letters = c("a","ab","ab","b"))
mean CI_lower CI_upper letters
1 1.48 1.29 1.56 a
2 1.59 1.38 1.84 ab
3 1.81 1.54 2.30 ab
4 1.94 1.62 2.59 b
Run Code Online (Sandbox Code Playgroud)
我做了一个可怜的尝试,就像这样:
same <- outer(test_data$CI_lower, test_data$CI_upper,"-")
same <- same<0
same <- lower.tri(same, diag = FALSE) & same
same_ind <- which(same,arr.ind = T)
groups <- as.list(as.numeric(rep(NA,nrow(test_data))))
for(i in 1:nrow(same_ind)){
group_pos <- as.numeric(same_ind[i,])
for(i2 in group_pos){
groups[[i2]] <- c(groups[[i2]],i)
}
}
letters_notation <- sapply(groups,function(x){
x <- x[!is.na(x)]
x <- letters[x]
x <- paste0(x,collapse="")
return(x)
}
)
Run Code Online (Sandbox Code Playgroud)
这将给出这样的:
mean CI_lower CI_upper letters
1 1.48 1.29 1.56 ab
2 1.59 1.38 1.84 acd
3 1.81 1.54 2.30 bce
4 1.94 1.62 2.59 de
Run Code Online (Sandbox Code Playgroud)
有关如何做到这一点的任何想法?
这是一个使用data.table非常有效的foverlaps功能的可能解决方案.这不完全是您想要的输出(因为我不完全理解它),但您可以从中识别出重叠点
library(data.table)
setkey(setDT(test_data), CI_lower, CI_upper)
Overlaps <- foverlaps(test_data, test_data, type = "any", which = TRUE) ## returns overlap indices
test_data[ , overlaps := Overlaps[, paste(letters[yid], collapse = ""), xid]$V1][]
# mean CI_lower CI_upper overlaps
# 1: 1.48 1.29 1.56 abc <~~ not overlapping with d
# 2: 1.59 1.38 1.84 abcd
# 3: 1.81 1.54 2.30 abcd
# 4: 1.94 1.62 2.59 bcd <~~ not overlapping with a
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
332 次 |
| 最近记录: |