easy conversion to draws from rstantools format
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 171
- Forks
- 26
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 3
Description
I am struggling with converting posteriors I get from rstantools things like posterior_linpred to a draws object correctly. The problem is that the chain information gets dropped. Here is an example illustrating what I'd like to have:
library(posterior)
#> Warning: package 'posterior' was built under R version 4.1.2
#> This is posterior version 1.2.2
#>
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#>
#> mad, sd, var
samp <- as_draws_matrix(example_draws())
## posterior_* functions from stan tools return matrices like
rstantools_samp <- matrix(as.matrix(samp), niterations(samp)*nchains(samp), nvariables(samp))
colnames(rstantools_samp) <- variables(samp)
head(rstantools_samp)
#> mu tau theta[1] theta[2] theta[3] theta[4] theta[5]
#> [1,] 2.005831 2.767367 3.9617520 0.27123540 -0.7431706 2.104805 0.92348879
#> [2,] 1.458316 6.979976 0.1237101 -0.06901539 0.9518270 7.281225 -0.06195211
#> [3,] 5.814947 9.677075 21.2510465 14.93055775 1.8290945 1.381443 0.53106337
#> [4,] 6.849586 4.788366 14.6996540 8.58618604 2.6749150 4.393232 4.75807198
#> [5,] 1.805168 2.848165 5.9600546 1.15573721 3.1088628 1.994890 0.76885094
#> [6,] 3.841243 4.083357 5.7601096 9.90920447 -0.9956266 5.328625 5.88894271
#> theta[6] theta[7] theta[8]
#> [1,] 1.650237 3.320019 4.848542
#> [2,] 11.257502 9.621128 -8.640446
#> [3,] 7.155371 14.802013 -1.736363
#> [4,] 8.101547 9.491277 5.281551
#> [5,] 4.656270 1.208251 -4.540236
#> [6,] -1.701463 2.780403 7.075855
dim(rstantools_samp)
#> [1] 400 10
## things are order by chain, so we have
all(rstantools_samp[1:100,1] == subset_draws(samp, variable="mu", chain=1))
#> [1] TRUE
all(rstantools_samp[101:200,1] == subset_draws(samp, variable="mu", chain=2))
#> [1] TRUE
## now we should have a posterior function which lets me create from
## rstantools_samp a posterior draws thing which knows the number of
## chains. This does not work:
as_draws_matrix(rstantools_samp, .nchains=4)
#> # A draws_matrix: 400 iterations, 1 chains, and 10 variables
#> variable
#> draw mu tau theta[1] theta[2] theta[3] theta[4] theta[5] theta[6]
#> 1 2.01 2.8 3.96 0.271 -0.74 2.1 0.923 1.7
#> 2 1.46 7.0 0.12 -0.069 0.95 7.3 -0.062 11.3
#> 3 5.81 9.7 21.25 14.931 1.83 1.4 0.531 7.2
#> 4 6.85 4.8 14.70 8.586 2.67 4.4 4.758 8.1
#> 5 1.81 2.8 5.96 1.156 3.11 2.0 0.769 4.7
#> 6 3.84 4.1 5.76 9.909 -1.00 5.3 5.889 -1.7
#> 7 5.47 4.0 4.03 4.151 10.15 6.6 3.741 -2.2
#> 8 1.20 1.5 -0.28 1.846 0.47 4.3 1.467 3.3
#> 9 0.15 3.9 1.81 0.661 0.86 4.5 -1.025 1.1
#> 10 7.17 1.8 6.08 8.102 7.68 5.6 7.106 8.5
#> # ... with 390 more draws, and 2 more variables
Created on 2022-08-04 by the reprex package (v2.0.1)
What I can do is to crudely set the nchains attribute to the number of chains. So I think the above should just work and give me a draws thing with 4 chains... this obviously requires documented formatting of the input samples to be column major sorted...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the as_draws_matrix conversion shown in the issue and review how matrix dimensions and chain metadata are handled. Use the rstantools_samp example as the reproduction case; done means converting the chain-ordered matrix with .nchains=4 produces draws with four chains and preserves the expected values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100