|
8 | 8 | #' @family br_show |
9 | 9 | br_show_forest <- function(breg, ...) { |
10 | 10 | assert_breg_obj_with_results(breg) |
| 11 | + |
| 12 | + # TODO: grouped (compared) forestplot for group_by??? |
| 13 | + |
| 14 | + dt <- br_get_results(breg) |
| 15 | + |
| 16 | + # if (global_p) { |
| 17 | + # if (inherits(model, "coxph")) { |
| 18 | + # p_val <- as.numeric(summary(model)$sctest[3]) |
| 19 | + # label <- paste("Global p ", format.pval(p_val, digits = 2, eps = 1e-3)) |
| 20 | + # forest_terms <- forest_terms |> |
| 21 | + # dplyr::add_row(term_label = "Global p", variable = label) |
| 22 | + # } else { |
| 23 | + # message("No global p value availabe for non-Cox model") |
| 24 | + # } |
| 25 | + # } |
| 26 | + |
| 27 | + # ref_line = NULL, xlim = NULL, vars = NULL, p = NULL, |
| 28 | + # ref_line = 1, xlim = c(0, 2) |
| 29 | + |
| 30 | + # if (!is.null(vars)) |
| 31 | + # data <- data[data$focal_term %in% vars] |
| 32 | + # if (!is.null(p)) { |
| 33 | + # minps <- sapply(split(data, data$focal_term), function(x) min(x$p, |
| 34 | + # na.rm = TRUE)) |
| 35 | + # vars2 <- names(minps[minps < p]) |
| 36 | + # data <- data[data$focal_term %in% vars2] |
| 37 | + # } |
| 38 | + |
| 39 | + ref_line <- if (inherits(model, "coxph") || (inherits( |
| 40 | + model, |
| 41 | + "glm" |
| 42 | + ) && model$family$link == "logit")) { |
| 43 | + 1L |
| 44 | + } else { |
| 45 | + 0L |
| 46 | + } |
| 47 | + xlim <- c(floor(min(data$CI_low, na.rm = TRUE)), ceiling(max(data$CI_high, |
| 48 | + na.rm = TRUE |
| 49 | + ))) |
| 50 | + if (is.infinite(xlim[1])) { |
| 51 | + warning("\ninfinite CI detected, set a minimal value -100", |
| 52 | + immediate. = TRUE |
| 53 | + ) |
| 54 | + xlim[1] <- -100 |
| 55 | + } |
| 56 | + if (is.infinite(xlim[2])) { |
| 57 | + warning("\ninfinite CI detected, set a maximal value 100", |
| 58 | + immediate. = TRUE |
| 59 | + ) |
| 60 | + xlim[2] <- 100 |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + c( |
| 65 | + "Group_variable", "Focal_variable", "reference_row", "label", "n_obs", |
| 66 | + "estimate", "std.error", "p.value", "conf.low", "conf.high" |
| 67 | + ) |
| 68 | + |
| 69 | + has_group <- !is.null(br_get_group_by(breg)) |
| 70 | + dt <- dt |> |
| 71 | + dplyr::mutate( |
| 72 | + ` ` = paste(rep(" ", 20), collapse = " "), |
| 73 | + `Estimate (95% CI)` = dplyr::case_when( |
| 74 | + dt$reference_row ~ "Reference", |
| 75 | + is.na(dt$std.error) ~ "", |
| 76 | + TRUE ~ sprintf( |
| 77 | + "%.2f (%.2f to %.2f)", |
| 78 | + estimate, |
| 79 | + conf.low, |
| 80 | + conf.high |
| 81 | + ) |
| 82 | + ), |
| 83 | + P = if_else(is.na(p.value), "", format.pval(p.value, |
| 84 | + digits = 2, |
| 85 | + eps = 0.001 |
| 86 | + )), |
| 87 | + conf.low = if_else(is.na(conf.low), estimate, conf.low), |
| 88 | + conf.high = if_else(is.na(conf.high), estimate, conf.high) |
| 89 | + ) #|> dplyr::mutate_all(~dplyr::if_else(is.na(.), "", as.character(.))) |
| 90 | + |
| 91 | + sel_cols <- c( |
| 92 | + if (has_group) "Group_variable" else NULL, |
| 93 | + "Focal_variable", "variable", "label", "n_obs", " ", "Estimate (95% CI)", |
| 94 | + "P", "estimate", "conf.low", "conf.high" |
| 95 | + ) |
| 96 | + dt2 <- dt |> |
| 97 | + dplyr::select(dplyr::all_of(sel_cols), dplyr::everything()) |> |
| 98 | + rename(c( |
| 99 | + "Group_variable" = "Group", |
| 100 | + "Focal_variable" = "Focal", |
| 101 | + "variable" = "Variable", |
| 102 | + "label" = "Level", |
| 103 | + "n_obs" = "N" |
| 104 | + )) |
| 105 | + |
| 106 | + idx_end <- 7L |
| 107 | + idx_ci <- 5L |
| 108 | + if (has_group) { |
| 109 | + idx_end <- 8L |
| 110 | + idx_end <- 6L |
| 111 | + } |
| 112 | + forestploter::forest(dt2[, 1:idx_end], |
| 113 | + est = dt$estimate, |
| 114 | + lower = dt$conf.low, |
| 115 | + upper = dt$conf.high, |
| 116 | + ci_column = idx_ci |
| 117 | + ) |
| 118 | + forestploter::forest(dt2[, 1:idx_end], |
| 119 | + est = dt$estimate, |
| 120 | + lower = dt$conf.low, |
| 121 | + upper = dt$conf.high, |
| 122 | + ci_column = idx_ci, |
| 123 | + ref_line = ref_line, xlim = xlim, ... |
| 124 | + ) |
11 | 125 | } |
12 | 126 |
|
13 | 127 | #' Show forest with `ggstats` interface |
@@ -120,3 +234,6 @@ br_show_table <- function(breg, ..., args_table_format = list(), export = FALSE, |
120 | 234 | if (export) tbl <- do.call(insight::export_table, vctrs::vec_c(list(tbl), args_table_export)) |
121 | 235 | tbl |
122 | 236 | } |
| 237 | + |
| 238 | +# TODO: show table with gtsummary |
| 239 | +# https://github.com/WangLabCSU/bregr/issues/16 |
0 commit comments