【问题标题】:Compact letter display from logical matrix来自逻辑矩阵的紧凑型字母显示
【发布时间】:2014-12-04 12:09:36
【问题描述】:

我想知道是否有一种方法可以将比较的逻辑矩阵转换为多重比较测试中使用的字母符号。如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

我感兴趣的是一个表示哪些条目具有重叠 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

我做了一个可怜的尝试,结果是这样的:

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

这将给出:

  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

关于如何做到这一点的任何想法?

【问题讨论】:

  • 不确定我是否理解所需的输出
  • 我想要什么或它意味着什么?我需要的是 c("a","ab","ab","b")。这意味着前 3 个条目都有重叠的 CI。最后 3 个有重叠的 CI。虽然第一个和最后一个条目不重叠。
  • 我有一个可能非常有效的解决方案,但不是您想要的输出,但是您将能够确定重叠路径,不确定是否发布它
  • 那么为什么第一个条目是“a”而不是“ab”或“ac”?为什么您将双字母分配给一些重叠的项目而不是其他项目?
  • 我觉得你需要看看igraph

标签: r


【解决方案1】:

这是使用data.tables 非常有效的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

【讨论】:

  • 谢谢!这确实非常接近。您是否也对如何消除冗余有一个好的/有效的想法?基本上我想如果我删除所有条目中存在的字母然后重新编号剩余的字母以从 a、b、c 等开始...
【解决方案2】:

根据 David Arenburg 的建议和这篇 http://menugget.blogspot.it/2014/05/automated-determination-of-distribution.html 不错的文章,我找到了解决方案。

library(igraph)

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

n <- nrow(test_data)


g <- outer(test_data$CI_lower, test_data$CI_upper,"-")
g <- !(g<0)
g <- g + t(g) # not necessary, but make matrix symmetric
g <- g!=1
rownames(g) <- 1:n # change row names
colnames(g) <- 1:n # change column names


# Re-arrange data into an "edge list" for use in igraph (i.e. which groups are "connected") - Solution from "David Eisenstat" ()
same <- which(g==1)
g2 <- data.frame(N1=((same-1) %% n) + 1, N2=((same-1) %/% n) + 1)
g2 <- g2[order(g2[[1]]),] # Get rid of loops and ensure right naming of vertices
g3 <- simplify(graph.data.frame(g2,directed = FALSE))



# Calcuate the maximal cliques - these are groupings where every node is connected to all others
cliq <- maximal.cliques(g3) # Solution from "majom" ()
cliq2 <- lapply(cliq, as.numeric)

# Reorder by level order - Solution from "MrFlick" ()
ml<-max(sapply(cliq, length))
reord <- do.call(order, data.frame(
  do.call(rbind, 
          lapply(cliq2, function(x) c(sort(x), rep.int(0, ml-length(x))))
  )
))
cliq <- cliq[reord]
cliq

# Generate labels to  factor levels
lab.txt <- vector(mode="list", n) # empty list
lab <- letters[seq(cliq)] # clique labels
for(i in seq(cliq)){ # loop to concatenate clique labels
  for(j in cliq[[i]]){
    lab.txt[[j]] <- paste0(lab.txt[[j]], lab[i])
  }
}

 

unlist(lab.txt)
[1] "a"  "ab" "ab" "b" 

【讨论】:

  • 使用当前 igraph 版本 reord &lt;- ... 似乎不起作用:parse_op_args 中的错误(...,what = "a vertex",is_fun = is_igraph_vs,:不是顶点序列
  • 不知道是不是好办法,不过可以用cliq2 &lt;- lapply(cliq, as.numeric)reord &lt;- do.call(... cliq2 ... )修复
猜你喜欢
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2020-01-04
  • 2022-01-13
  • 2010-10-22
  • 1970-01-01
相关资源
最近更新 更多