From 3ec866dfe52883aaf211e28b6821ec1f2e6970ea Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 26 Aug 2023 08:42:41 +0200 Subject: [PATCH 01/10] changed logrank to likelihood in show_global_p --- R/default_forest_panels.R | 2 +- R/forest_model.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index b738066..4d7c7d0 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -70,7 +70,7 @@ default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, me forest_panel(width = 0.03, item = "vline", hjust = 0.5), forest_panel( width = 0.12, - display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference; Variable logrank:"), + display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference; Likelihood ratio:"), sprintf("%0.2f (%0.2f, %0.2f)", trans(estimate), trans(conf.low), trans(conf.high)) ), display_na = NA diff --git a/R/forest_model.R b/R/forest_model.R index a1be244..675bb75 100644 --- a/R/forest_model.R +++ b/R/forest_model.R @@ -286,7 +286,7 @@ forest_model <- function(model, if (show_global_p != "none") { if (inherits(model, "coxph")) { - p_val <- as.numeric(summary(model)$sctest[3]) + p_val <- as.numeric(summary(model)$logtest[3]) if (show_global_p == "bottom") { label <- paste("Global p", format.pval(p_val, digits = 1, eps = 1e-3)) forest_terms <- forest_terms %>% From 945eb645f49189df602a5a3829ebf40cd04980be Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 2 Sep 2023 16:04:02 +0200 Subject: [PATCH 02/10] minor change --- R/default_forest_panels.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index 4d7c7d0..341b210 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -70,7 +70,7 @@ default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, me forest_panel(width = 0.03, item = "vline", hjust = 0.5), forest_panel( width = 0.12, - display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference; Likelihood ratio:"), + display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference; Likelihood ratio test:"), sprintf("%0.2f (%0.2f, %0.2f)", trans(estimate), trans(conf.low), trans(conf.high)) ), display_na = NA From a0dbda9092344565e59f6b3ddef8b120c60b10e3 Mon Sep 17 00:00:00 2001 From: gernophil Date: Fri, 13 Oct 2023 11:50:44 +0200 Subject: [PATCH 03/10] changed global to Wald test --- R/default_forest_panels.R | 2 +- R/forest_model.R | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index 341b210..ad4dc3e 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -70,7 +70,7 @@ default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, me forest_panel(width = 0.03, item = "vline", hjust = 0.5), forest_panel( width = 0.12, - display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference; Likelihood ratio test:"), + display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference | Global p (Wald test):"), sprintf("%0.2f (%0.2f, %0.2f)", trans(estimate), trans(conf.low), trans(conf.high)) ), display_na = NA diff --git a/R/forest_model.R b/R/forest_model.R index 675bb75..dd3a250 100644 --- a/R/forest_model.R +++ b/R/forest_model.R @@ -286,9 +286,9 @@ forest_model <- function(model, if (show_global_p != "none") { if (inherits(model, "coxph")) { - p_val <- as.numeric(summary(model)$logtest[3]) + p_val <- as.numeric(summary(model)$waldtest[3]) if (show_global_p == "bottom") { - label <- paste("Global p", format.pval(p_val, digits = 1, eps = 1e-3)) + label <- paste(" Global p (Wald test):", format.pval(p_val, digits = 1, eps = 1e-3)) forest_terms <- forest_terms %>% dplyr::add_row(term_label = "Global p", variable = label) } else if (show_global_p == "aside") { From 829f11252f3d3fdb1e9505474ea538ca971e470a Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 18 Nov 2023 15:57:27 +0100 Subject: [PATCH 04/10] added selection for global p --- R/default_forest_panels.R | 15 +++++++++++++-- R/forest_model.R | 25 ++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index ad4dc3e..aaa5127 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -9,7 +9,18 @@ #' @return `list` ready to be passed to `forest_model` #' @export #' -default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, measure = NULL, trans_char = "I") { +default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, measure = NULL, trans_char = "I", which_global_p = "none") { + if (which_global_p != "none") { + if (which_global_p == "logtest") { + global_p_text <- "(Likelihood ratio test)" + } else if (which_global_p == "waldtest") { + global_p_text <- "(Wald test)" + } else if (which_global_p == "sctest") { + global_p_text <- "(Score (logrank))" + } + } else { + global_p_text <- "(Likelihood ratio test)" + } if (is.list(model) && inherits(model[[1]], "rma")) { model <- model[[1]] } @@ -70,7 +81,7 @@ default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, me forest_panel(width = 0.03, item = "vline", hjust = 0.5), forest_panel( width = 0.12, - display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference | Global p (Wald test):"), + display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference | Global p ", global_p_text, ":"), sprintf("%0.2f (%0.2f, %0.2f)", trans(estimate), trans(conf.low), trans(conf.high)) ), display_na = NA diff --git a/R/forest_model.R b/R/forest_model.R index dd3a250..a568889 100644 --- a/R/forest_model.R +++ b/R/forest_model.R @@ -127,7 +127,7 @@ #' #' print(forest_model(glm(outcome ~ ., binomial(), data_for_logistic))) forest_model <- function(model, - panels = default_forest_panels(model, factor_separate_line = factor_separate_line), + panels = default_forest_panels(model, factor_separate_line = factor_separate_line, which_global_p = which_global_p), covariates = NULL, exponentiate = NULL, funcs = NULL, factor_separate_line = FALSE, format_options = forest_model_format_options(), @@ -136,8 +136,10 @@ forest_model <- function(model, recalculate_width = TRUE, recalculate_height = TRUE, model_list = NULL, merge_models = FALSE, exclude_infinite_cis = TRUE, show_global_p = c("none", "bottom", "aside"), + which_global_p = c("logtest", "waldtest", "sctest"), n_logical_true_only = FALSE) { show_global_p <- match.arg(show_global_p) + which_global_p <- match.arg(which_global_p) mapping <- aes(estimate, xmin = conf.low, xmax = conf.high) if (!is.null(model_list)) { if (!is.list(model_list)) { @@ -286,9 +288,26 @@ forest_model <- function(model, if (show_global_p != "none") { if (inherits(model, "coxph")) { - p_val <- as.numeric(summary(model)$waldtest[3]) + p_val_logtest <- as.numeric(summary(model)$logtest[3]) + p_val_waldtest <- as.numeric(summary(model)$waldtest[3]) + p_val_sctest <- as.numeric(summary(model)$sctest[3]) + if (which_global_p != "none") { + if (which_global_p == "logtest") { + p_val <- p_val_logtest + global_p_text <- "(Likelihood ratio test)" + } else if (which_global_p == "waldtest") { + p_val <- p_val_waldtest + global_p_text <- "(Wald test)" + } else if (which_global_p == "sctest") { + p_val <- p_val_sctest + global_p_text <- "(Score (logrank))" + } + } else { + p_val <- p_val_logtest + global_p_text <- "(Likelihood ratio test)" + } if (show_global_p == "bottom") { - label <- paste(" Global p (Wald test):", format.pval(p_val, digits = 1, eps = 1e-3)) + label <- paste(" Global p ", global_p_text, ":", format.pval(p_val, digits = 1, eps = 1e-3)) forest_terms <- forest_terms %>% dplyr::add_row(term_label = "Global p", variable = label) } else if (show_global_p == "aside") { From 59906bf1753221733edc1de60f6f0440da440df7 Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 18 Nov 2023 16:04:41 +0100 Subject: [PATCH 05/10] added selection for global p --- R/default_forest_panels.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index aaa5127..80189c0 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -81,7 +81,7 @@ default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, me forest_panel(width = 0.03, item = "vline", hjust = 0.5), forest_panel( width = 0.12, - display = if_else(reference, if_else(is_na(p.value), "Reference", "Reference | Global p ", global_p_text, ":"), + display = if_else(reference, if_else(is_na(p.value), "Reference", paste0("Reference | Global p ", global_p_text, ":")), sprintf("%0.2f (%0.2f, %0.2f)", trans(estimate), trans(conf.low), trans(conf.high)) ), display_na = NA From 06af3ae765d8e23792d5e6c6e0b6e78494f97277 Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 18 Nov 2023 16:08:51 +0100 Subject: [PATCH 06/10] test --- R/default_forest_panels.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index 80189c0..bcc684c 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -19,7 +19,7 @@ default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, me global_p_text <- "(Score (logrank))" } } else { - global_p_text <- "(Likelihood ratio test)" + global_p_text <- "(I was none)" } if (is.list(model) && inherits(model[[1]], "rma")) { model <- model[[1]] From 6fda8fb06454e2b21c5702b8437e11d72c86c955 Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 18 Nov 2023 16:11:25 +0100 Subject: [PATCH 07/10] test --- R/default_forest_panels.R | 2 +- R/forest_model.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index bcc684c..80189c0 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -19,7 +19,7 @@ default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, me global_p_text <- "(Score (logrank))" } } else { - global_p_text <- "(I was none)" + global_p_text <- "(Likelihood ratio test)" } if (is.list(model) && inherits(model[[1]], "rma")) { model <- model[[1]] diff --git a/R/forest_model.R b/R/forest_model.R index a568889..aaee494 100644 --- a/R/forest_model.R +++ b/R/forest_model.R @@ -127,6 +127,7 @@ #' #' print(forest_model(glm(outcome ~ ., binomial(), data_for_logistic))) forest_model <- function(model, + which_global_p = c("logtest", "waldtest", "sctest"), panels = default_forest_panels(model, factor_separate_line = factor_separate_line, which_global_p = which_global_p), covariates = NULL, exponentiate = NULL, funcs = NULL, factor_separate_line = FALSE, @@ -136,7 +137,6 @@ forest_model <- function(model, recalculate_width = TRUE, recalculate_height = TRUE, model_list = NULL, merge_models = FALSE, exclude_infinite_cis = TRUE, show_global_p = c("none", "bottom", "aside"), - which_global_p = c("logtest", "waldtest", "sctest"), n_logical_true_only = FALSE) { show_global_p <- match.arg(show_global_p) which_global_p <- match.arg(which_global_p) From 4fcb411bb15b3d48b95746f99405ff28681c6171 Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 18 Nov 2023 16:14:42 +0100 Subject: [PATCH 08/10] test --- R/forest_model.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/forest_model.R b/R/forest_model.R index aaee494..9f6a4bb 100644 --- a/R/forest_model.R +++ b/R/forest_model.R @@ -127,7 +127,6 @@ #' #' print(forest_model(glm(outcome ~ ., binomial(), data_for_logistic))) forest_model <- function(model, - which_global_p = c("logtest", "waldtest", "sctest"), panels = default_forest_panels(model, factor_separate_line = factor_separate_line, which_global_p = which_global_p), covariates = NULL, exponentiate = NULL, funcs = NULL, factor_separate_line = FALSE, @@ -137,6 +136,7 @@ forest_model <- function(model, recalculate_width = TRUE, recalculate_height = TRUE, model_list = NULL, merge_models = FALSE, exclude_infinite_cis = TRUE, show_global_p = c("none", "bottom", "aside"), + which_global_p = c("logtest", "waldtest", "sctest"), n_logical_true_only = FALSE) { show_global_p <- match.arg(show_global_p) which_global_p <- match.arg(which_global_p) @@ -163,7 +163,7 @@ forest_model <- function(model, (inherits(model_list[[1]], "glm") && model_list[[1]]$family$link == "logit") } if (missing(panels)) { - panels <- default_forest_panels(model_list[[1]], factor_separate_line = factor_separate_line) + panels <- default_forest_panels(model_list[[1]], factor_separate_line = factor_separate_line, which_global_p = which_global_p) } } else { if (is.null(exponentiate)) { From d15699edfa74ad64851c28aeee495aefc9737344 Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 18 Nov 2023 16:24:27 +0100 Subject: [PATCH 09/10] simplified code --- R/default_forest_panels.R | 10 ++++------ R/forest_model.R | 7 ++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index 80189c0..b1c814c 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -9,17 +9,15 @@ #' @return `list` ready to be passed to `forest_model` #' @export #' -default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, measure = NULL, trans_char = "I", which_global_p = "none") { - if (which_global_p != "none") { - if (which_global_p == "logtest") { - global_p_text <- "(Likelihood ratio test)" - } else if (which_global_p == "waldtest") { +default_forest_panels <- function(model = NULL, factor_separate_line = FALSE, measure = NULL, trans_char = "I", which_global_p = "logtest") { + if (which_global_p != "logtest") { + if (which_global_p == "waldtest") { global_p_text <- "(Wald test)" } else if (which_global_p == "sctest") { global_p_text <- "(Score (logrank))" } } else { - global_p_text <- "(Likelihood ratio test)" + global_p_text <- "(Likelihood ratio test)" } if (is.list(model) && inherits(model[[1]], "rma")) { model <- model[[1]] diff --git a/R/forest_model.R b/R/forest_model.R index 9f6a4bb..50e4f46 100644 --- a/R/forest_model.R +++ b/R/forest_model.R @@ -291,11 +291,8 @@ forest_model <- function(model, p_val_logtest <- as.numeric(summary(model)$logtest[3]) p_val_waldtest <- as.numeric(summary(model)$waldtest[3]) p_val_sctest <- as.numeric(summary(model)$sctest[3]) - if (which_global_p != "none") { - if (which_global_p == "logtest") { - p_val <- p_val_logtest - global_p_text <- "(Likelihood ratio test)" - } else if (which_global_p == "waldtest") { + if (which_global_p != "logtest") { + if (which_global_p == "waldtest") { p_val <- p_val_waldtest global_p_text <- "(Wald test)" } else if (which_global_p == "sctest") { From 1299e14bc4496170ca4ac4aa7c36205680fddf40 Mon Sep 17 00:00:00 2001 From: gernophil Date: Sat, 18 Nov 2023 16:36:03 +0100 Subject: [PATCH 10/10] added some documentation --- R/forest_model.R | 4 ++++ man/forest_model.Rd | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/R/forest_model.R b/R/forest_model.R index 50e4f46..5c44d93 100644 --- a/R/forest_model.R +++ b/R/forest_model.R @@ -28,6 +28,10 @@ #' - 'none', don't show. #' - 'bottom', show global p value in the bottom. #' - 'aside', show global p value along with 'Reference', this is useful when you plot a list of models. +#' @param which_global_p Select which global p value to show, only works for Cox model. +#' - 'logtest', Likelihood ratio test. +#' - 'waldtest', Wald test. +#' - 'sctest', Score (logrank). #' @param n_logical_true_only whether to only count TRUE values in n for logical #' covariates #' diff --git a/man/forest_model.Rd b/man/forest_model.Rd index c828f3d..d61e067 100644 --- a/man/forest_model.Rd +++ b/man/forest_model.Rd @@ -22,6 +22,7 @@ forest_model( merge_models = FALSE, exclude_infinite_cis = TRUE, show_global_p = c("none", "bottom", "aside"), + which_global_p = c("logtest", "waldtest", "sctest"), n_logical_true_only = FALSE ) } @@ -71,6 +72,11 @@ displayed as text. Defaults to \code{TRUE}, since otherwise plot is malformed} - 'bottom', show global p value in the bottom. - 'aside', show global p value along with 'Reference', this is useful when you plot a list of models.} +\item{which_global_p}{Select which global p value to show, only works for Cox model. +- 'logtest', Likelihood ratio test. +- 'waldtest', Wald test. +- 'sctest', Score (logrank).} + \item{n_logical_true_only}{whether to only count TRUE values in n for logical covariates} }