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

Dealing with chains of different length #334

Open
n-kall opened this issue Jan 10, 2024 · 1 comment
Open

Dealing with chains of different length #334

n-kall opened this issue Jan 10, 2024 · 1 comment

Comments

@n-kall
Copy link
Collaborator

n-kall commented Jan 10, 2024

I suppose there is an implicit expectation that all chains are the same length, but different lengths can occur when doing filtering with e.g. dplyr. I think the recommended workflow should likely be to merge chains before doing this kind of filtering, but I guess that can't be enforced.

Currently, different draws formats handle chains of varying length differently in how they calculate niterations (even after repairing). And draws_array does not allow different length chains at all -> I think the error message could point to merge_chains as a solution.

Consider filtering a draws_df based on the value of some variable (below dplyr is used to do so, but it could be done in base R syntax). This can end up with a draws_df that has different length chains.

library(posterior)
library(dplyr)
fdraws <- example_draws() |>
as_draws_df() |>
dplyr::filter(`mu` > 0) |>
repair_draws()

niterations(fdraws)
## [1] 94

niterations(as_draws_matrix(fdraws))
## [1] 89.5

niterations(as_draws_list(fdraws))
## [1] 90

as_draws_array(fdraws)
## Error in abind::abind(x, along = 3L) : 
##   arg 'X2' has dims=86, 10, 1; but need dims=90, 10, X

Should there be a message/warning given when a draws object has different length chains that suggests merge_chains?

@mjskay
Copy link
Collaborator

mjskay commented Jan 10, 2024

Hmm yeah, to be consistent with other cases where this comes up, conversion functions from draws_df (which I believe is the only format that supports unequal chain lengths) to other formats should probably check for this. When it happens, it should either raise an error, or merge chains and warn with warn_merge_chains("index").

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

No branches or pull requests

2 participants