From d423fe302d9271a7e7cad3851d03dbcf82f4ccf3 Mon Sep 17 00:00:00 2001 From: Jacob Elnaggar Date: Sun, 3 Sep 2023 20:03:48 -0500 Subject: [PATCH 1/7] Update geom_signif.R added FDR function --- R/geom_signif.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/geom_signif.R b/R/geom_signif.R index 1910e0a..945665c 100644 --- a/R/geom_signif.R +++ b/R/geom_signif.R @@ -39,6 +39,7 @@ #' annotations per facet. #' @param na.rm If `FALSE` (the default), removes missing values with #' a warning. If `TRUE` silently removes missing values. +#' @param FDR logical. Should the p value be corrected for multiple testing? TRUE by default. #' @param orientation The orientation of the layer. The default (‘NA’) #' automatically determines the orientation from the aesthetic mapping. #' In the rare event that this fails it can be given explicitly by setting @@ -104,6 +105,7 @@ stat_signif <- function(mapping = NULL, vjust = 0, parse = FALSE, manual = FALSE, + FDR=FALSE, orientation = NA, ...) { if (manual) { @@ -137,6 +139,7 @@ stat_signif <- function(mapping = NULL, parse = parse, manual = manual, na.rm = na.rm, + FDR=FDR, orientation = orientation, ... ) @@ -271,6 +274,7 @@ geom_signif <- function(mapping = NULL, vjust = 0, parse = FALSE, manual = FALSE, + FDR=FALSE, orientation = NA, ...) { params <- list(na.rm = na.rm, ...) @@ -327,6 +331,7 @@ geom_signif <- function(mapping = NULL, vjust = vjust, parse = parse, manual = manual, + FDR=FDR, orientation = orientation ) ) @@ -427,6 +432,7 @@ StatSignif <- ggplot2::ggproto( step_increase, tip_length, manual, + FDR, flipped_aes = FALSE) { data <- ggplot2::flip_data(data, flipped_aes) scales <- ggplot2::flip_data(scales, flipped_aes) @@ -467,6 +473,9 @@ StatSignif <- ggplot2::ggproto( group_2 <- complete_data$y[complete_data$x == scales$x$map(comp[2]) & complete_data$PANEL == data$PANEL[1]] p_value <- do.call(test, c(list(group_1, group_2), test.args))$p.value + if(FDR==TRUE){ + p_value = p.adjust(p_value, method="fdr", n=length(comparisons)) + } if (is.numeric(map_signif_level)) { temp_value <- names(which.min(map_signif_level[which(map_signif_level > p_value)])) if (is.null(temp_value)) { From c1ea713125faecd103a847e5aab93a4e380c5cd1 Mon Sep 17 00:00:00 2001 From: Jacob Elnaggar Date: Sun, 3 Sep 2023 20:59:42 -0500 Subject: [PATCH 2/7] FDR function description updated Fixed discrepancy with FDR function description --- R/geom_signif.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/geom_signif.R b/R/geom_signif.R index 945665c..2c582df 100644 --- a/R/geom_signif.R +++ b/R/geom_signif.R @@ -39,7 +39,7 @@ #' annotations per facet. #' @param na.rm If `FALSE` (the default), removes missing values with #' a warning. If `TRUE` silently removes missing values. -#' @param FDR logical. Should the p value be corrected for multiple testing? TRUE by default. +#' @param FDR logical. Should the p value be corrected for multiple testing by false discovery rate ? FALSE by default. #' @param orientation The orientation of the layer. The default (‘NA’) #' automatically determines the orientation from the aesthetic mapping. #' In the rare event that this fails it can be given explicitly by setting From 57804be2cfeef6d90a7d4a1abefe8e962e9932fd Mon Sep 17 00:00:00 2001 From: Jacob Elnaggar Date: Tue, 5 Sep 2023 00:17:56 -0500 Subject: [PATCH 3/7] Testing p value correction with facets --- R/geom_signif.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/geom_signif.R b/R/geom_signif.R index 2c582df..6db3833 100644 --- a/R/geom_signif.R +++ b/R/geom_signif.R @@ -473,8 +473,10 @@ StatSignif <- ggplot2::ggproto( group_2 <- complete_data$y[complete_data$x == scales$x$map(comp[2]) & complete_data$PANEL == data$PANEL[1]] p_value <- do.call(test, c(list(group_1, group_2), test.args))$p.value + print(p_value) + print(comparisons) if(FDR==TRUE){ - p_value = p.adjust(p_value, method="fdr", n=length(comparisons)) + p_value = p.adjust(p_value, method="fdr") #, n=length(comparisons)) } if (is.numeric(map_signif_level)) { temp_value <- names(which.min(map_signif_level[which(map_signif_level > p_value)])) From 26150c49ab56ddd305056147579c8c4fcb4ccdbc Mon Sep 17 00:00:00 2001 From: Jacob Elnaggar Date: Tue, 5 Sep 2023 00:26:44 -0500 Subject: [PATCH 4/7] debugging2 --- R/geom_signif.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/geom_signif.R b/R/geom_signif.R index 6db3833..02e931c 100644 --- a/R/geom_signif.R +++ b/R/geom_signif.R @@ -473,10 +473,13 @@ StatSignif <- ggplot2::ggproto( group_2 <- complete_data$y[complete_data$x == scales$x$map(comp[2]) & complete_data$PANEL == data$PANEL[1]] p_value <- do.call(test, c(list(group_1, group_2), test.args))$p.value + print("old") print(p_value) print(comparisons) if(FDR==TRUE){ p_value = p.adjust(p_value, method="fdr") #, n=length(comparisons)) + print("new") + print(p_value) } if (is.numeric(map_signif_level)) { temp_value <- names(which.min(map_signif_level[which(map_signif_level > p_value)])) From edd23c55cbd099ca2dfe51d32f2946dfc0fd0c09 Mon Sep 17 00:00:00 2001 From: Jacob Elnaggar Date: Tue, 5 Sep 2023 00:34:14 -0500 Subject: [PATCH 5/7] debugging 2 --- R/geom_signif.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/geom_signif.R b/R/geom_signif.R index 02e931c..b2d83ee 100644 --- a/R/geom_signif.R +++ b/R/geom_signif.R @@ -477,7 +477,7 @@ StatSignif <- ggplot2::ggproto( print(p_value) print(comparisons) if(FDR==TRUE){ - p_value = p.adjust(p_value, method="fdr") #, n=length(comparisons)) + p_value = p.adjust(p_value, method="fdr", n=length(comparisons)) print("new") print(p_value) } From e7e428b84d046a9fad717f386b0c94389c35561b Mon Sep 17 00:00:00 2001 From: Jacob Elnaggar Date: Tue, 5 Sep 2023 00:42:59 -0500 Subject: [PATCH 6/7] finished debuging --- R/geom_signif.R | 5 ----- 1 file changed, 5 deletions(-) diff --git a/R/geom_signif.R b/R/geom_signif.R index b2d83ee..2c582df 100644 --- a/R/geom_signif.R +++ b/R/geom_signif.R @@ -473,13 +473,8 @@ StatSignif <- ggplot2::ggproto( group_2 <- complete_data$y[complete_data$x == scales$x$map(comp[2]) & complete_data$PANEL == data$PANEL[1]] p_value <- do.call(test, c(list(group_1, group_2), test.args))$p.value - print("old") - print(p_value) - print(comparisons) if(FDR==TRUE){ p_value = p.adjust(p_value, method="fdr", n=length(comparisons)) - print("new") - print(p_value) } if (is.numeric(map_signif_level)) { temp_value <- names(which.min(map_signif_level[which(map_signif_level > p_value)])) From ad56df9ec1284ec6f238fe7c511062c5e201561f Mon Sep 17 00:00:00 2001 From: Jacob Elnaggar Date: Tue, 5 Sep 2023 13:35:41 -0500 Subject: [PATCH 7/7] Changed FDR to correction IndrajeetPatil: Instead of creating a new argument for every p-value adjustment method, a better option here is to instead introduce argument method = "none". The default would make this addition backward-compatible, and this adjustment becomes relative only when method is not equal to "none". --- R/geom_signif.R | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/R/geom_signif.R b/R/geom_signif.R index 2c582df..61e8f64 100644 --- a/R/geom_signif.R +++ b/R/geom_signif.R @@ -39,7 +39,8 @@ #' annotations per facet. #' @param na.rm If `FALSE` (the default), removes missing values with #' a warning. If `TRUE` silently removes missing values. -#' @param FDR logical. Should the p value be corrected for multiple testing by false discovery rate ? FALSE by default. +#' @param correction Should the p value be corrected for multiple testing by false discovery rate ? +#' "none" by default. p.adjust.methods: c("holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none") #' @param orientation The orientation of the layer. The default (‘NA’) #' automatically determines the orientation from the aesthetic mapping. #' In the rare event that this fails it can be given explicitly by setting @@ -105,7 +106,7 @@ stat_signif <- function(mapping = NULL, vjust = 0, parse = FALSE, manual = FALSE, - FDR=FALSE, + correction="none", orientation = NA, ...) { if (manual) { @@ -139,7 +140,7 @@ stat_signif <- function(mapping = NULL, parse = parse, manual = manual, na.rm = na.rm, - FDR=FDR, + correction=correction, orientation = orientation, ... ) @@ -274,7 +275,7 @@ geom_signif <- function(mapping = NULL, vjust = 0, parse = FALSE, manual = FALSE, - FDR=FALSE, + correction="none", orientation = NA, ...) { params <- list(na.rm = na.rm, ...) @@ -331,7 +332,7 @@ geom_signif <- function(mapping = NULL, vjust = vjust, parse = parse, manual = manual, - FDR=FDR, + correction=correction, orientation = orientation ) ) @@ -432,7 +433,7 @@ StatSignif <- ggplot2::ggproto( step_increase, tip_length, manual, - FDR, + correction, flipped_aes = FALSE) { data <- ggplot2::flip_data(data, flipped_aes) scales <- ggplot2::flip_data(scales, flipped_aes) @@ -473,9 +474,7 @@ StatSignif <- ggplot2::ggproto( group_2 <- complete_data$y[complete_data$x == scales$x$map(comp[2]) & complete_data$PANEL == data$PANEL[1]] p_value <- do.call(test, c(list(group_1, group_2), test.args))$p.value - if(FDR==TRUE){ - p_value = p.adjust(p_value, method="fdr", n=length(comparisons)) - } + p_value = p.adjust(p_value, method=correction, n=length(comparisons)) if (is.numeric(map_signif_level)) { temp_value <- names(which.min(map_signif_level[which(map_signif_level > p_value)])) if (is.null(temp_value)) {