Skip to content

Commit f95fe3a

Browse files
committed
impl , not finish
1 parent 323009a commit f95fe3a

5 files changed

Lines changed: 137 additions & 10 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Imports:
2121
broom.helpers,
2222
cli,
2323
dplyr,
24+
forestploter,
2425
glue,
2526
insight,
2627
lifecycle,

R/01-class.R

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,33 @@ method(print, breg) <- function(x, ..., raw = FALSE) {
7373
if (raw) {
7474
print(utils::str(x))
7575
} else {
76-
cli_text("A object of {.cls breg} class\n")
76+
cli_text("A object of {.cls breg} class with slots:\n")
7777

78+
qty_x <- qty(x@n_x)
79+
qty_x2 <- qty(x@n_x2)
80+
if (qty_x == 0) qty_x <- qty_x + 1
81+
if (qty_x2 == 0) qty_x2 <- qty_x2 + 1
82+
83+
# TODO: add variable?
7884
cli_ul()
79-
cli_li("{.field Y}: {.emph {x@y}}")
80-
cli_li("{.field X}:")
81-
ulid <- cli_ul()
85+
cli_li("{.field y} ({col_blue('response')} variable): {.emph {x@y}}")
86+
# cli_li("{.field x}:")
87+
# ulid <- cli_ul()
8288
# https://cli.r-lib.org/reference/pluralization.html#choosing-the-right-quantity
83-
cli_li("{col_blue('focal')}{qty(x@n_x)} variable{?s}: {.emph {x@x}}")
84-
cli_li("{col_blue('control')}{qty(x@n_x2)} variable{?s}: {.emph {x@x2}}")
85-
cli_end(ulid)
89+
cli_li("{.field x} ({col_blue('focal')}{qty_x} term{?s}): {.emph {x@x}}")
90+
cli_li("{.field x2} ({col_blue('control')}{qty_x2} term{?s}): {.emph {x@x2}}")
91+
# cli_end(ulid)
8692
cli_li("{.field group_by}: {.emph {x@group_by}}")
8793
cli_li("{.field data}: {.emph {rlang::expr_deparse(x@data)}}")
8894
cli_li("{.field config}: {.emph {x@config}}")
8995
cli_li("{.field models}: {.emph {rlang::expr_deparse(x@models)}}")
9096
cli_li("{.field results}: {.emph {rlang::expr_deparse(x@results)}}")
97+
cli_li("{.field results_tidy}: {.emph {rlang::expr_deparse(x@results_tidy)}}")
9198
cli_end()
9299

93100
cli_text()
94-
cli_text(col_grey("Focal variables (or primary variables) are injected into the model one by one, while covariates (or control variables) remain constant across all models in the batch."))
101+
cli_text(col_grey("Focal term(s) are injected into the model one by one,"))
102+
cli_text(col_grey("while control term(s) remain constant across all models in the batch."))
95103
}
96104

97105
invisible(x)

R/02-pipeline.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#' - `br_run()` for passing other configurations for obtaining modeling results with [tidy_plus_plus()].
3333
#' e.g., The default value for `exponentiate` is `FALSE` (coefficients are not exponentiated).
3434
#' For logistic, multinomial, and Cox-PH regressions models, `exponentiate` is typically set to `TRUE`.
35-
#' To generate a reseanable results, some arguments have been reset, include:
35+
#' To generate a resonable results, some arguments have been reset, include:
3636
#' - `intercept`: `TRUE` (default) (instead of `FALSE` in default [tidy_plus_plus()]).
3737
#' @param model_args A list of arguments passed to `br_set_model()`.
3838
#' @param run_args A list of arguments passed to `br_run()`.
@@ -282,6 +282,7 @@ runner <- function(ms, data, dots, x, run_parallel) {
282282
# the number of observations will be multiplied
283283
# see: https://github.com/larmarange/broom.helpers/blob/210cc945bd6a462148a358f8d4851e0d16d208e3/R/model_get_n.R#L96
284284

285+
# TODO: disbale intercept option
285286
# Change default values of some arguments in tidy_plus_plus
286287
if (!"intercept" %in% names(dots)) {
287288
dots[["intercept"]] <- TRUE

R/04-show.R

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,120 @@
88
#' @family br_show
99
br_show_forest <- function(breg, ...) {
1010
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+
)
11125
}
12126

13127
#' Show forest with `ggstats` interface
@@ -120,3 +234,6 @@ br_show_table <- function(breg, ..., args_table_format = list(), export = FALSE,
120234
if (export) tbl <- do.call(insight::export_table, vctrs::vec_c(list(tbl), args_table_export))
121235
tbl
122236
}
237+
238+
# TODO: show table with gtsummary
239+
# https://github.com/WangLabCSU/bregr/issues/16

man/pipeline.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)