Skip to content

Commit 85bed6f

Browse files
yoshidk6claude
andcommitted
Replace tidybayes dependency with ggdist and rstantools
tidybayes depends on arrayhelpers which is scheduled for CRAN archival. Replace tidybayes::add_*_draws() with direct posterior_epred/linpred/predict via rstantools generics + a new .pp_matrix_to_draws_tbl() reshape helper. Replace tidybayes::median_qi() with ggdist::median_qi(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 42952a7 commit 85bed6f

26 files changed

Lines changed: 103 additions & 46 deletions

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: BayesERtools
22
Type: Package
33
Title: Bayesian Exposure-Response Analysis Tools
4-
Version: 0.2.5
4+
Version: 0.2.5.9000
55
Authors@R:
66
c(person("Kenta", "Yoshida", , "yoshida.kenta.6@gmail.com", role = c("aut", "cre"),
77
comment = c(ORCID = "0000-0003-4967-3831")),
@@ -27,7 +27,7 @@ Depends:
2727
URL: https://genentech.github.io/BayesERtools/, https://genentech.github.io/BayesERbook/
2828
Config/testthat/edition: 3
2929
Roxygen: list(markdown = TRUE)
30-
RoxygenNote: 7.3.2
30+
RoxygenNote: 7.3.3
3131
Imports:
3232
dplyr,
3333
tidyr,
@@ -38,8 +38,9 @@ Imports:
3838
rlang,
3939
rstanarm,
4040
rstanemax (>= 0.1.9),
41+
rstantools,
4142
loo,
42-
tidybayes,
43+
ggdist,
4344
posterior
4445
Suggests:
4546
testthat (>= 3.0.0),

NEWS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# BayesERtools 0.2.5.9000
2+
3+
## Major changes
4+
5+
* Replaced `tidybayes` dependency with `ggdist` and `rstantools` to avoid
6+
CRAN archival risk from `tidybayes`'s dependency on `arrayhelpers`.
7+
`tidybayes::add_*_draws()` calls replaced with direct `posterior_epred()` /
8+
`posterior_linpred()` / `posterior_predict()` via `rstantools` generics.
9+
`tidybayes::median_qi()` replaced with `ggdist::median_qi()`.
10+
111
# BayesERtools 0.2.5
212

313
## Major changes

R/coveff.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ sim_coveff <- function(
6565
df_for_sim <- spec_coveff_to_df_sim(spec_coveff)
6666

6767
linpred_draws <-
68-
tidybayes::add_linpred_draws(df_for_sim, extract_mod(ermod))
68+
.pp_matrix_to_draws_tbl(
69+
rstantools::posterior_linpred(extract_mod(ermod), newdata = df_for_sim),
70+
df_for_sim, ".linpred"
71+
)
6972

7073
linpred_draws_ref <-
7174
linpred_draws |>
@@ -102,7 +105,11 @@ sim_coveff <- function(
102105

103106
linpred_med_qi <-
104107
linpred_draws_3 |>
105-
tidybayes::median_qi(.width = qi_width) |>
108+
dplyr::group_by(dplyr::across(
109+
!dplyr::any_of(c(".chain", ".iteration", ".draw",
110+
".odds_ratio", ".response_diff"))
111+
)) |>
112+
ggdist::median_qi(.width = qi_width) |>
106113
dplyr::arrange(var_order, value_order)
107114

108115
# Determine which columns to select based on model type

R/ersim-methods.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,17 @@ extract_var_cov.ersim_med_qi <- function(x) attr(x, "var_cov")
7272
#'
7373
calc_ersim_med_qi <- function(x, qi_width = 0.95) {
7474
var_exposure_sym <- rlang::sym(extract_var_exposure(x))
75-
x_grouped <- x
76-
7775
if (inherits(x, "ersim_marg")) {
7876
x_grouped <- dplyr::group_by(x, .id_exposure, !!var_exposure_sym)
77+
} else {
78+
x_grouped <- dplyr::group_by(x, dplyr::across(
79+
!c(.chain, .iteration, .draw, .epred, .linpred, .prediction)
80+
))
7981
}
8082

8183
simdata_med_qi <-
8284
x_grouped |>
83-
tidybayes::median_qi(.width = qi_width) |>
85+
ggdist::median_qi(.width = qi_width) |>
8486
dplyr::as_tibble()
8587

8688
# Create dummy ermod to feed to ersim class constructor

R/sim_ermod.R

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#' `.epred` is on the probability scale. `.prediction` is the predicted
2626
#' response with residual variability (or in case of binary endpoint,
2727
#' the predicted yes (1) or no (0) for event occurrence).
28-
#' See [tidybayes::add_epred_draws()] for more details.
28+
#' See [rstantools::posterior_epred()] for more details.
2929
#'
3030
#' In case of `output_type = "median_qi"`, it returns `ersim_med_qi` object.
3131
#'
@@ -87,23 +87,27 @@ sim_er <- function(
8787
n_draws_sim <- chech_ndraws(mod, n_draws_sim)
8888

8989
simdata_epred <-
90-
tidybayes::add_epred_draws(newdata, mod,
91-
ndraws = n_draws_sim,
92-
seed = seed_sample_draws
90+
.pp_matrix_to_draws_tbl(
91+
.posterior_draws(rstantools::posterior_epred, mod, newdata,
92+
n_draws_sim, seed_sample_draws
93+
),
94+
newdata, ".epred"
9395
)
9496
simdata_linpred <-
95-
tidybayes::add_linpred_draws(newdata, mod,
96-
ndraws = n_draws_sim,
97-
seed = seed_sample_draws
97+
.pp_matrix_to_draws_tbl(
98+
.posterior_draws(rstantools::posterior_linpred, mod, newdata,
99+
n_draws_sim, seed_sample_draws
100+
),
101+
newdata, ".linpred"
98102
) |>
99-
dplyr::ungroup() |>
100103
dplyr::select(.draw, .row, .linpred)
101104
simdata_predicted <-
102-
tidybayes::add_predicted_draws(newdata, mod,
103-
ndraws = n_draws_sim,
104-
seed = seed_sample_draws
105+
.pp_matrix_to_draws_tbl(
106+
.posterior_draws(rstantools::posterior_predict, mod, newdata,
107+
n_draws_sim, seed_sample_draws
108+
),
109+
newdata, ".prediction"
105110
) |>
106-
dplyr::ungroup() |>
107111
dplyr::select(.draw, .row, .prediction)
108112

109113
simdata <-
@@ -121,7 +125,10 @@ sim_er <- function(
121125

122126
simdata_med_qi <-
123127
simdata |>
124-
tidybayes::median_qi(.width = qi_width) |>
128+
dplyr::group_by(dplyr::across(
129+
!c(.chain, .iteration, .draw, .epred, .linpred, .prediction)
130+
)) |>
131+
ggdist::median_qi(.width = qi_width) |>
125132
dplyr::as_tibble()
126133

127134
if (inherits(ermod, "ermod_bin")) {
@@ -406,7 +413,7 @@ sim_er_new_exp_marg <- function(
406413
simdata_med_qi <-
407414
simdata |>
408415
dplyr::group_by(.id_exposure, !!var_exposure_sym) |>
409-
tidybayes::median_qi(.width = qi_width) |>
416+
ggdist::median_qi(.width = qi_width) |>
410417
dplyr::as_tibble()
411418

412419
return(new_ersim_marg_med_qi(simdata_med_qi, ermod,

R/utils-draws.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.posterior_draws <- function(fn, mod, newdata, n_draws, seed) {
2+
set.seed(seed)
3+
if (inherits(mod, c("stanemax", "stanemaxbin"))) {
4+
fn(mod, newdata = newdata, ndraws = n_draws)
5+
} else {
6+
fn(mod, newdata = newdata, draws = n_draws)
7+
}
8+
}
9+
10+
.pp_matrix_to_draws_tbl <- function(mat, newdata, col_name) {
11+
n_draws <- nrow(mat)
12+
n_obs <- nrow(newdata)
13+
draw_rows <- expand.grid(
14+
.draw = seq_len(n_draws),
15+
.row = seq_len(n_obs)
16+
)
17+
newdata |>
18+
dplyr::mutate(.row = dplyr::row_number()) |>
19+
dplyr::left_join(
20+
dplyr::tibble(
21+
.row = draw_rows$.row,
22+
.chain = NA_integer_,
23+
.iteration = NA_integer_,
24+
.draw = draw_rows$.draw,
25+
!!col_name := as.vector(mat)
26+
),
27+
by = ".row"
28+
)
29+
}

R/yyy.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if (getRversion() >= "2.15.1") {
55
# General
66
utils::globalVariables(c(
77
".", "n", ":=", ".epred", ".linpred", ".prediction", ".draw", ".response",
8-
".id_exposure"
8+
".id_exposure", ".chain", ".iteration", ".row"
99
))
1010

1111
# ggplot

man/BayesERtools-package.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.

man/build_spec_coveff.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.

man/dev_ermod_bin_cov_sel.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)