Open wds15 opened 2 years ago
One option is to ingest posterior_...()
function output via rvar()
, as the internal format of rvar()
is (by design) the same as the output of those functions. rvar()
lets you set the number of chains:
library(posterior)
library(rstanarm)
mtcars_subset = mtcars[, c("hp", "cyl", "mpg")]
m = stan_glm(mpg ~ hp*cyl, data = mtcars_subset, chains = 4)
epred = rvar(posterior_epred(m), nchains = 4)
epred
#> rvar<1000,4>[32] mean ± sd:
#>
#> Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive
#> 20 ± 0.79 20 ± 0.79 26 ± 0.93 20 ± 0.79
#> Hornet Sportabout Valiant Duster 360 Merc 240D
#> 16 ± 0.88 21 ± 0.80 15 ± 1.01 28 ± 1.18
#> Merc 230 Merc 280 Merc 280C Merc 450SE
#> 26 ± 0.94 20 ± 0.81 20 ± 0.81 15 ± 0.85
#> Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental
#> 15 ± 0.85 15 ± 0.85 15 ± 0.79 15 ± 0.81
#> Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla
#> 15 ± 0.89 28 ± 1.10 29 ± 1.41 28 ± 1.12
#> Toyota Corona Dodge Challenger AMC Javelin Camaro Z28
#> 26 ± 0.97 16 ± 1.09 16 ± 1.09 15 ± 1.01
#> Pontiac Firebird Fiat X1-9 Porsche 914-2 Lotus Europa
#> 16 ± 0.88 28 ± 1.10 26 ± 0.91 24 ± 1.25
#> Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E
#> 14 ± 1.21 18 ± 1.44 13 ± 2.11 25 ± 1.17
This can be especially useful for the posterior_...()
functions since you can put the resulting rvar
s in data frame alongside the data used to make the predictions:
cbind(mtcars_subset, epred = epred)
#> hp cyl mpg epred
#> Mazda RX4 110 6 21.0 20.48962 ± 0.7908150
#> Mazda RX4 Wag 110 6 21.0 20.48962 ± 0.7908150
#> Datsun 710 93 4 22.8 25.80881 ± 0.9256434
#> Hornet 4 Drive 110 6 21.4 20.48962 ± 0.7908150
#> Hornet Sportabout 175 8 18.7 15.51820 ± 0.8760158
#> Valiant 105 6 18.1 20.70694 ± 0.8036316
#> Duster 360 245 8 14.3 14.55358 ± 1.0112446
#> Merc 240D 62 4 24.4 28.07635 ± 1.1825719
#> Merc 230 95 4 22.8 25.66252 ± 0.9437801
#> Merc 280 123 6 19.2 19.92459 ± 0.8124373
#> Merc 280C 123 6 17.8 19.92459 ± 0.8124373
#> Merc 450SE 180 8 16.4 15.44930 ± 0.8459401
#> Merc 450SL 180 8 17.3 15.44930 ± 0.8459401
#> Merc 450SLC 180 8 15.2 15.44930 ± 0.8459401
#> Cadillac Fleetwood 205 8 10.4 15.10479 ± 0.7862891
#> Lincoln Continental 215 8 10.4 14.96699 ± 0.8091412
#> Chrysler Imperial 230 8 14.7 14.76028 ± 0.8889254
#> Fiat 128 66 4 32.4 27.78376 ± 1.1026873
#> Honda Civic 52 4 30.4 28.80781 ± 1.4145373
#> Toyota Corolla 65 4 33.9 27.85691 ± 1.1217972
#> Toyota Corona 97 4 21.5 25.51622 ± 0.9659045
#> Dodge Challenger 150 8 15.5 15.86271 ± 1.0899219
#> AMC Javelin 150 8 15.2 15.86271 ± 1.0899219
#> Camaro Z28 245 8 13.3 14.55358 ± 1.0112446
#> Pontiac Firebird 175 8 19.2 15.51820 ± 0.8760158
#> Fiat X1-9 66 4 27.3 27.78376 ± 1.1026873
#> Porsche 914-2 91 4 26.0 25.95510 ± 0.9117325
#> Lotus Europa 113 4 30.4 24.34588 ± 1.2535618
#> Ford Pantera L 264 8 15.8 14.29175 ± 1.2067307
#> Ferrari Dino 175 6 19.7 17.66450 ± 1.4376777
#> Maserati Bora 335 8 15.0 13.31335 ± 2.1102372
#> Volvo 142E 109 4 21.4 24.63846 ± 1.1669329
And if you do want it as a draws_matrix, you can use as_draws_matrix()
:
as_draws_matrix(epred)
#> # A draws_matrix: 1000 iterations, 4 chains, and 32 variables
#> variable
#> draw x[Mazda RX4] x[Mazda RX4 Wag] x[Datsun 710] x[Hornet 4 Drive]
#> 1 21 21 26 21
#> 2 21 21 25 21
#> 3 18 18 25 18
#> 4 21 21 24 21
#> 5 21 21 25 21
#> 6 21 21 24 21
#> 7 23 23 28 23
#> 8 21 21 26 21
#> 9 21 21 28 21
#> 10 22 22 25 22
#> variable
#> draw x[Hornet Sportabout] x[Valiant] x[Duster 360] x[Merc 240D]
#> 1 15 21 13 28
#> 2 15 21 13 28
#> 3 14 19 15 27
#> 4 17 21 17 26
#> 5 17 21 16 27
#> 6 17 21 17 26
#> 7 15 23 12 30
#> 8 16 21 16 28
#> 9 15 22 13 30
#> 10 18 22 17 27
#> # ... with 3990 more draws, and 24 more variables
That said, it does seem like since draws_matrix()
has an .nchains
argument, perhaps as_draws_matrix()
should too?
Hi!
Indeed, as_draws_matrix(rvar(rstantools_samp, nchains=4))
gives me what I want for the example I quoted. Maybe all of the as_draws_*
should have a .nchains
argument? Certainly, the as_draws_matrix
needs it... and this needs doc on the format posterior
expects things to be (column-major).
Thanks!
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: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...