stan-dev / stan-dev/posterior

Dealing with chains of different length

Open
#334 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
171
Forks
26
Avg merge
2d 18h
Merged PRs (30d)
3

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing niterations and the conversion entry points for draws_df, draws_matrix, draws_list, and draws_array after repair_draws() creates unequal chain lengths. Review merge_chains as the suggested remedy and determine where an informative warning or error should be produced. Done means unequal chains are handled consistently and draws_array points users toward merge_chains when needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.