Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions R/default_forest_panels.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
}
Expand Down Expand Up @@ -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
Expand Down
28 changes: 24 additions & 4 deletions R/forest_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down Expand Up @@ -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(),
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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") {
Expand Down
6 changes: 6 additions & 0 deletions man/forest_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.