0

I have a stanfit object called fit returned by rstan::stan(...) to infer a parameter theta. I can now analyse theta using e.g. rstan::summary(fit, pars="theta").

I later realised that I am more interested in inference about the square of theta. I should have included a transformed parameters block in the STAN model to include theta_squared as a parameter in the output.

Is it possible to add the transformed parameter theta_squared <- theta^2 to the existing stanfit object, as if it had been calculated in a transformed parameters block?

sieste
  • 8,296
  • 3
  • 33
  • 48
  • 1
    We hope to add a way to do this automatically by running the generated quantities block after the parameters have been drawn; but it'll be a while before this is coded and released. – Bob Carpenter Mar 18 '16 at 22:39

2 Answers2

2

I don't know if you can (or should) add a parameter to the stanfit object manually. At least you can get the MCMC samples by as.data.frame(fit), and then play with it as you wish, including defining theta^2.

Kota Mori
  • 6,510
  • 1
  • 21
  • 25
  • Thanks for that. I would like to be able to use all the rstan functions like `stan_rhat`, `stan_ac` etc on the transformed parameter. It seems like the easiest way to achieve that is to add it to the `stanfit` object. – sieste Mar 18 '16 at 11:50
1

You can get a lot of those same graphs (rhat, ac, etc) using ShinyStan, which does allow you to add a quantity like this (if it's a scalar). For example,

library("shinystan")
# create shinystan object (sso)
sso <- as.shinystan(fit)
# add theta_squared to sso
sso <- generate_quantity(sso, fun = function(x) x^2, 
                         param1 = "theta", new_name = "theta_squared")
# launch the shinystan interface
launch_shinystan(sso)