如果没有统计差异,则覆盖 cldList 停止
Override cldList stop if no statistical differences
我正在尝试将 cld 添加到我的数据中,但 cldList 在遇到统计差异时停止工作。我的数据包含有统计差异和无统计差异的组,因此我需要一种方法来解决此问题。 cldList 是 rcompanion
包的一部分,写法如下:
cldList = function(formula = NULL,
data = NULL,
comparison = NULL,
p.value = NULL,
threshold = 0.05,
print.comp = FALSE,
remove.space = TRUE,
remove.equal = TRUE,
remove.zero = TRUE,
swap.colon = TRUE,
swap.vs = FALSE,
...)
{
if(!is.null(formula)){
p.value = eval(parse(text=paste0("data","$",all.vars(formula[[2]])[1])))
comparison = eval(parse(text=paste0("data","$",all.vars(formula[[3]])[1])))
}
Comparison = (as.numeric(p.value) <= threshold)
if (sum(Comparison) == 0){stop("No significant differences.", call.=FALSE)} # THIS LINE HERE #
if(remove.space == TRUE) {comparison = gsub(" ", "", comparison)}
if(remove.equal == TRUE) {comparison = gsub("=", "", comparison)}
if(remove.zero == TRUE) {comparison = gsub("0", "", comparison)}
if(swap.colon == TRUE) {comparison = gsub(":", "-", comparison)}
if(swap.vs == TRUE) {comparison = gsub("vs", "-", comparison)}
names(Comparison) = comparison
if(print.comp == TRUE)
{Y = data.frame(Comparisons = names(Comparison),
p.value = p.value, Value=Comparison,
Threshold=threshold)
cat("\n", "\n")
print(Y)
cat("\n", "\n")}
MCL = multcompLetters(Comparison, ...)
Group = names(MCL$Letters)
Letter = as.character(MCL$Letters)
MonoLetter = as.character(MCL$monospacedLetters)
Z = data.frame(Group, Letter, MonoLetter)
return(Z)
}
这是我的一部分数据(从另一个程序输出):
dummy_df <-data.frame(target = c("A1","A1","A1","A1","A1","A1","A2","A2","A2","A2","A2","A2","A3","A3","A3","A3","A3","A3","A4","A4","A4","A4","A4","A4"),
comparison = c("a - b","a - c","a - d","b - c","b - d","c - d","a - b","a - c","a - d","b - c","b - d","c - d","e - c","e - d","e - f","c - d","c - f","d - f","e - c","e - d","e - f","c - d","c - f","d - f"),
significant = c("Yes","No","Yes","Yes","Yes","Yes","Yes","No","Yes","Yes","Yes","Yes","Yes","Yes","Yes","No","Yes","Yes","No","No","No","No","No","No"),
p.val = c( 0.04,0.06,0.04,0.04,0.04,0.04,0.04,0.06,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.06,0.04,0.04,0.06,0.06,0.06,0.06,0.06,0.06))
我的代码是这样的
i <- 1
targets <- data.frame(Genes = unique(dummy_df$target))
df <- data.frame()
df1 <- data.frame(Group = "No", Letter ="significant", MonoLetter = "differences")
while (i<=nrow(targets)) {
print(i)
df2 <-cldList(p.val~comparison, data = subset.data.frame(dummy_df, dummy_df$target==targets[i,]), threshold = 0.05)
print(df2)
df2$target <- targets[i,]
#print(df2)
df <- rbind(df, df2)
i <- i+1
}
我尝试在 cldList
步骤之后添加 if (nrow(df2)==0) {df2 <- df1}
以至少了解哪些目标在统计上没有差异,但没有成功。
是否有使循环继续遍历所有目标的解决方法?理想情况下,所有组的 cldList
输出具有相同的字母将是目标。
我是 cldList()
函数的作者。
很遗憾,您提供的循环代码对我不起作用。
但是,如果没有显着差异,我可以解决 cldList()
的行为。问题是函数所依赖的 multcompLetters
在没有显着差异时具有不同的输出。
编辑: rcompanion 包中的函数已更新为 return 具有相同字母的数据框当没有显着差异时,所有组。 (rcompanion v. 2.4.13, CRAN.R-project.org/package=rcompanion
#### Examples
library(rcompanion)
Comparison = c("A-B", "A-C", "B-C")
Pvalue = c(0.04, 1, 1)
DataFrame = data.frame(Comparison, Pvalue)
cldList(Pvalue ~ Comparison, data=DataFrame)
### Group Letter MonoLetter
### 1 A a a
### 2 B b b
### 3 C ab ab
Comparison2 = c("A-B", "A-C", "B-C")
Pvalue2 = c(1, 1, 1)
DataFrame2 = data.frame(Comparison2, Pvalue2)
cldList(Pvalue2 ~ Comparison2, data=DataFrame2)
### Group Letter MonoLetter
### 1 A a a
### 2 B a a
### 3 C a a
我正在尝试将 cld 添加到我的数据中,但 cldList 在遇到统计差异时停止工作。我的数据包含有统计差异和无统计差异的组,因此我需要一种方法来解决此问题。 cldList 是 rcompanion
包的一部分,写法如下:
cldList = function(formula = NULL,
data = NULL,
comparison = NULL,
p.value = NULL,
threshold = 0.05,
print.comp = FALSE,
remove.space = TRUE,
remove.equal = TRUE,
remove.zero = TRUE,
swap.colon = TRUE,
swap.vs = FALSE,
...)
{
if(!is.null(formula)){
p.value = eval(parse(text=paste0("data","$",all.vars(formula[[2]])[1])))
comparison = eval(parse(text=paste0("data","$",all.vars(formula[[3]])[1])))
}
Comparison = (as.numeric(p.value) <= threshold)
if (sum(Comparison) == 0){stop("No significant differences.", call.=FALSE)} # THIS LINE HERE #
if(remove.space == TRUE) {comparison = gsub(" ", "", comparison)}
if(remove.equal == TRUE) {comparison = gsub("=", "", comparison)}
if(remove.zero == TRUE) {comparison = gsub("0", "", comparison)}
if(swap.colon == TRUE) {comparison = gsub(":", "-", comparison)}
if(swap.vs == TRUE) {comparison = gsub("vs", "-", comparison)}
names(Comparison) = comparison
if(print.comp == TRUE)
{Y = data.frame(Comparisons = names(Comparison),
p.value = p.value, Value=Comparison,
Threshold=threshold)
cat("\n", "\n")
print(Y)
cat("\n", "\n")}
MCL = multcompLetters(Comparison, ...)
Group = names(MCL$Letters)
Letter = as.character(MCL$Letters)
MonoLetter = as.character(MCL$monospacedLetters)
Z = data.frame(Group, Letter, MonoLetter)
return(Z)
}
这是我的一部分数据(从另一个程序输出):
dummy_df <-data.frame(target = c("A1","A1","A1","A1","A1","A1","A2","A2","A2","A2","A2","A2","A3","A3","A3","A3","A3","A3","A4","A4","A4","A4","A4","A4"),
comparison = c("a - b","a - c","a - d","b - c","b - d","c - d","a - b","a - c","a - d","b - c","b - d","c - d","e - c","e - d","e - f","c - d","c - f","d - f","e - c","e - d","e - f","c - d","c - f","d - f"),
significant = c("Yes","No","Yes","Yes","Yes","Yes","Yes","No","Yes","Yes","Yes","Yes","Yes","Yes","Yes","No","Yes","Yes","No","No","No","No","No","No"),
p.val = c( 0.04,0.06,0.04,0.04,0.04,0.04,0.04,0.06,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.06,0.04,0.04,0.06,0.06,0.06,0.06,0.06,0.06))
我的代码是这样的
i <- 1
targets <- data.frame(Genes = unique(dummy_df$target))
df <- data.frame()
df1 <- data.frame(Group = "No", Letter ="significant", MonoLetter = "differences")
while (i<=nrow(targets)) {
print(i)
df2 <-cldList(p.val~comparison, data = subset.data.frame(dummy_df, dummy_df$target==targets[i,]), threshold = 0.05)
print(df2)
df2$target <- targets[i,]
#print(df2)
df <- rbind(df, df2)
i <- i+1
}
我尝试在 cldList
步骤之后添加 if (nrow(df2)==0) {df2 <- df1}
以至少了解哪些目标在统计上没有差异,但没有成功。
是否有使循环继续遍历所有目标的解决方法?理想情况下,所有组的 cldList
输出具有相同的字母将是目标。
我是 cldList()
函数的作者。
很遗憾,您提供的循环代码对我不起作用。
但是,如果没有显着差异,我可以解决 cldList()
的行为。问题是函数所依赖的 multcompLetters
在没有显着差异时具有不同的输出。
编辑: rcompanion 包中的函数已更新为 return 具有相同字母的数据框当没有显着差异时,所有组。 (rcompanion v. 2.4.13, CRAN.R-project.org/package=rcompanion
#### Examples
library(rcompanion)
Comparison = c("A-B", "A-C", "B-C")
Pvalue = c(0.04, 1, 1)
DataFrame = data.frame(Comparison, Pvalue)
cldList(Pvalue ~ Comparison, data=DataFrame)
### Group Letter MonoLetter
### 1 A a a
### 2 B b b
### 3 C ab ab
Comparison2 = c("A-B", "A-C", "B-C")
Pvalue2 = c(1, 1, 1)
DataFrame2 = data.frame(Comparison2, Pvalue2)
cldList(Pvalue2 ~ Comparison2, data=DataFrame2)
### Group Letter MonoLetter
### 1 A a a
### 2 B a a
### 3 C a a