Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rvar conversion drops dimensions #265

Closed
wds15 opened this issue Dec 16, 2022 · 2 comments · Fixed by #268
Closed

rvar conversion drops dimensions #265

wds15 opened this issue Dec 16, 2022 · 2 comments · Fixed by #268

Comments

@wds15
Copy link

wds15 commented Dec 16, 2022

The rvar creation utilities do drop dimensions from the input variables whenever just 1 entry lives in the last index as it seems. Here is a minimal example:

> library(posterior)
+ mu <- rdo(matrix(rnorm(1, mean = 1, sd = 1), 1, 1), ndraws=10)
+ mu
+ mur1 <- as_draws_rvars(as_draws_array(mu))
rvar<10>[1,1] mean ± sd:
     [,1]     
[1,] 0.53 ± 1 
> > as_draws_rvars(as_draws_array(mur1))
# A draws_rvars: 10 iterations, 1 chains, and 1 variables
$x: rvar<10>[1] mean ± sd:
[1] 0.53 ± 1 

You see that mu is a matrix of dimension 1x1, which I can create with rdo. Converting it to an array and then back to an rvar gets rid of the last dimension. This is with posterior 1.3.0.

The offending code in posterior seems to be here:
https://github.com/stan-dev/posterior/blob/master/R/as_draws_rvars.R#L78-L82

This misbehaviour triggers a problem in OncoBayes2 in a special case:

library(OncoBayes2)

groups <- "one_group"
combo_data <- tibble(
  group_id = factor(groups, groups),
  drug_A = 1,
  drug_B = 1,
  num_patients = 3,
  num_toxicities = 1
)

num_groups <- 1

fit <- blrm_exnex(
  cbind(num_toxicities, num_patients - num_toxicities) ~
    1 + I(log(drug_A)) |
    1 + I(log(drug_B)) |
    0 + I(2 * drug_A * drug_B / (1 + drug_A * drug_B)) |
    group_id,
  data = combo_data,
  prior_EX_mu_mean_comp = matrix(
    c(log(1/4), 0,
      log(1/4), 0),
    nrow = 2,
    ncol = 2,
    byrow = TRUE
  ),
  prior_EX_mu_sd_comp = matrix(
    c(1, 0.7,
      1, 0.7),
    nrow = 2,
    ncol = 2,
    byrow = TRUE
  ),
  prior_EX_mu_mean_inter = 0,
  prior_EX_mu_sd_inter = 1,
  prior_EX_tau_mean_comp = matrix(
    c(0, 0),
    nrow = 2,
    ncol = 2
  ),
  prior_EX_tau_sd_comp = matrix(
    c(1, 1),
    nrow = 2,
    ncol = 2
  ),
  prior_EX_tau_mean_inter = matrix(0),
  prior_EX_tau_sd_inter = matrix(1),
  prior_is_EXNEX_comp = c(FALSE, FALSE),
  prior_is_EXNEX_inter = FALSE,
  prior_EX_prob_comp = matrix(1, nrow = num_groups, ncol = 2),
  prior_EX_prob_inter = matrix(1, nrow = num_groups, ncol = 1),
  prior_tau_dist = 0,
  prior_PD = FALSE
)

dd <- posterior_predict(fit)

## Error in aeta[, g, , drop = FALSE] : incorrect number of dimensions

Tagging @mjskay here... seems to be another corner case to catch?

@wds15
Copy link
Author

wds15 commented Jan 11, 2023

@mjskay would be great if you could have a look at this.... or may I also attempt to fix this issue myself and provide a PR?

@mjskay
Copy link
Collaborator

mjskay commented Jan 11, 2023

Happy to look, though the absolute earliest would likely be next week or the week after. Alternatively if you wanted to work on a PR that sounds good to me! 😊

mjskay added a commit that referenced this issue Jan 25, 2023
ensure as_draws_rvars preserves dims of length-1 arrays, closes #265
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants