diff --git a/R/default_forest_panels.R b/R/default_forest_panels.R index b738066..b1c814c 100644 --- a/R/default_forest_panels.R +++ b/R/default_forest_panels.R @@ -9,7 +9,16 @@ #' @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 = "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)" + } if (is.list(model) && inherits(model[[1]], "rma")) { model <- model[[1]] } @@ -70,7 +79,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", 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 diff --git a/R/forest_model.R b/R/forest_model.R index a1be244..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 #' @@ -127,7 +131,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 +140,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)) { @@ -161,7 +167,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)) { @@ -286,9 +292,23 @@ forest_model <- function(model, if (show_global_p != "none") { if (inherits(model, "coxph")) { - p_val <- as.numeric(summary(model)$sctest[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 != "logtest") { + 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", 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") { 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} }