njtierney / mmcc

Fast, tidy functions for mcmc diagnostics and summaries, built with data.table
http://mmcc.njtierney.com/
Other
24 stars 5 forks source link

number of variables in an mcmc object? #42

Open njtierney opened 5 years ago

njtierney commented 5 years ago

This is my current thinking for n_var for stanfit and jags classes:

#' @export
n_var.stanfit <- function(x){
    # note - is "lp__" a parameter we care about?
    # what about y[1] and y[2]?
    length(x@model_pars)
}

#' @export
n_var.jags <- function(x){
    x$state() %>%
        purrr::pluck(1) %>%
        length()
}

You can test on: example_stan_model and example_jags_model - the models for which are given in data-raw

I'm not sure if this is a good idea the way it is currently written...also, the number of variables is kind of a pretty big question! if things are nicely laid out in an mcmc.list then it is clear, but things like jags and stanfit models are perhaps less clear.

What do you think, @samclifford ?

samclifford commented 5 years ago

So this is operating on a model object rather than on a set of samples? If that's the case, surely we'd be pulling out the number of partially observed nodes from the graph.

njtierney commented 5 years ago

we'd be pulling out the number of partially observed nodes from the graph.

That sounds about right, but I'm not sure how to retrieve the graph structure from the jags/stan models? Maybe I can wrap up this feature and then we can add methods for n_var for stanfit and jags later.

samclifford commented 5 years ago

See my recent push to the glance branch. My thinking is that if a node has an value upon compilation then it is likely to be a parameter in the model. For nodes which have size k we can count it as k parameters or as 1 using the unroll_vectors logical.