extract.samples | R Documentation |
Extracts or draw samples from fit models.
extract.samples( object , n=10000 , pars , ... )
extract.prior( object , n=1000 , pars , ... )
object |
Fit model to extract samples from |
n |
Number of samples to simulate |
pars |
Character vector of parameters to return |
... |
Other parameters to pass to descendent functions (when defined) |
Use extract.samples
and extract.prior
to return lists of samples for posterior and prior distributions, respectively. Methods for extract.samples
are provided for map
, map2stan
, and stanfit
objects. Methods for extract.prior
are provided for map
and map2stan
objects.
For map2stan
and stanfit
models, extract.samples
returns cleaned samples already contained in the object. These samples are cleaned of dimension attributes and the lp__
, dev
, and log_lik
traces that are used internally. For map
and other types, it uses the variance-covariance matrix and coefficients to define a multivariate Gaussian posterior to draw n
samples from.
extract.prior
must simulate draws using the model definition. It attempts to return samples in the same list structure as posterior samples. This makes prior-posterior comparison easier. See examples below.
A named list
(for map2stan
and stanfit
) or data.frame
containing samples for each parameter in the posterior/prior distribution.
Richard McElreath
mvrnorm
data(chimpanzees)
d <- list(
pulled_left = chimpanzees$pulled_left ,
prosoc_left = chimpanzees$prosoc_left ,
condition = chimpanzees$condition ,
actor = as.integer( chimpanzees$actor ) ,
blockid = as.integer( chimpanzees$block )
)
m <- map(
alist(
pulled_left ~ dbinom(1,theta),
logit(theta) <- a + aj[actor] + bp*prosoc_left + bpc*condition*prosoc_left,
aj[actor] ~ dnorm( 0 , 1 ),
a ~ dnorm(0,2),
bp ~ dnorm(0,1),
bpc ~ dnorm(0,1)
) ,
data=d )
prior <- extract.prior(m,n=1e4)
post <- extract.samples(m)
ps <- par("bty")
par(bty="n")
plot( precis(prior,2) , col.ci="gray" , xlim=c(-3,3.5) , bty="n" )
plot( precis(post,2) , add=TRUE , pch=16 )
par(bty=ps)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.