The magic key you're looking for is:
get_adaptation_info
It IS in the doc.
> On Mar 2, 2015, at 11:58 PM, Michael Betancourt <
betan...@gmail.com> wrote:
>
> I’ve said it before and I’ll say it again, not having sampler parameters
> (accept stat, n divergent, etc) included in extract by default is dangerous.
> The user should be forced to look at these Clockwork Orange-style.
I agree that these should be included in extract, just like lp__.
> But I also can’t figure out how to access them from a stanfit object at
> all (there’s no official doc on how to do this and the archived users
> list is no help). Seriously, why is this so hard?
In the doc for stanfit-class, you see:
get_sampler_params
signature(object = "stanfit"): obtain the parameters used for
the sampler such as stepsizeand treedepth. The results are returned
as a list, each element of which is an array for a chain. The array
has number of columns corresponding to the number of parameters used
in the sampler and its column names provide the parameter names. Optional
parameter inc_warmup indicates whether to include the warmup period.
Here's an example:
> fit <- stan(model_code = "parameters { real y; } model { y ~ normal(0,1); }")
> samp_params <- get_sampler_params(fit);
At this point, samp_params[[1]] is the first chain, samp_params[[2]] is
the second chain and so on. Each chain has everyting in order.
> head(sps[[1]])
accept_stat__ stepsize__ treedepth__ n_leapfrog__ n_divergent__
[1,] 4.781267e-01 2.0000000 1 1 0
[2,] 1.860020e-218 5.5698093 1 1 0
[3,] 9.682160e-01 0.7105644 3 5 0
[4,] 8.290335e-01 0.7877146 2 3 0
[5,] 9.444573e-01 0.7123420 2 3 0
[6,] 9.966285e-01 0.9768503 1 1 0
If you want to get rid of warmup, use
get_sampler_params(fit, inc_warmup=FALSE)
but you probably don't for these since you've said we should be
paying attention to n_divergent__ in warmup.
It's in the R doc, but not under what you may be looking for,
This kind of doc issue (not knowing how the stanfit object was constructed
so not knowing what's in it by design) is one reason I don't like having single
classes or functions overloaded for different types of return types.
There's no way to provide clean doc for the get, because the columns
will vary based on the type of sampler (e.g., NUTS has treedepth__ but
vanilla HMC doesn't). Similarly, stan() is used for the call for both vanilla
HMC and NUTS. Just like in the command line.
- Bob