Generate simulated data from Stan file?

1,106 views
Skip to first unread message

Dustin Tran

unread,
Oct 17, 2015, 3:20:27 PM10/17/15
to Stan users mailing list
Is there a way to generate simulated data from a Stan file? E.g., I specify hyperparameters from the data block, follow the data generating process from the model block, and dump all the latent variables and observations from the DGP into a file.

My current workflow for testing fits on simulated data is to write the Stan model, and then to write a similar version in R in order to generate the data and stan_rdump things.

Dustin

Ben Goodrich

unread,
Oct 17, 2015, 3:27:55 PM10/17/15
to Stan users mailing list
On Saturday, October 17, 2015 at 3:20:27 PM UTC-4, Dustin Tran wrote:
Is there a way to generate simulated data from a Stan file? E.g., I specify hyperparameters from the data block, follow the data generating process from the model block, and dump all the latent variables and observations from the DGP into a file.

My current workflow for testing fits on simulated data is to write the Stan model, and then to write a similar version in R in order to generate the data and stan_rdump things.

You can specify the argument algorithm = "Fixed_param" to stan() to do this, possibly with iter = 1.

Ben

Bob Carpenter

unread,
Oct 17, 2015, 4:33:19 PM10/17/15
to stan-...@googlegroups.com
This is something we need to write up --- the whole
process for doing this kind of thing.

I tend to do the simulations in R myself. You can
use Stan's generated quantities block to do pure simulation
(simple Monte Carlo, not MCMC).

In some ways, having to write it twice gives a kind of
unit-testing flavor to it. This is one of the cases
where having a directed graphical model without declared
data and parameters would make it easy to simulate and
estimate from the same model.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Andrew Gelman

unread,
Oct 17, 2015, 5:05:29 PM10/17/15
to stan-...@googlegroups.com
Soon (I hope) Stan will have the capability to run the generated quantities block without re-fitting the model, taking the simulations from a fitted Stan model as input. This should help in post-processing.

Bob Carpenter

unread,
Oct 17, 2015, 5:12:25 PM10/17/15
to stan-...@googlegroups.com
Definitely part of the problem is that the output
format for RStan doesn't match the input format for
data. A converter for a single draw would be great --- something
that went from the output back to a list.

- Bob

Andrew Gelman

unread,
Oct 17, 2015, 5:34:34 PM10/17/15
to stan-...@googlegroups.com
But I don’t want it do one draw at a time, I want it to do them all at once. That to me is more in the spirit of probabilistic programming.

Bob Carpenter

unread,
Oct 17, 2015, 5:46:12 PM10/17/15
to stan-...@googlegroups.com
Andrew --- I think we're talking about two different things.
I understand we need the new generated quantities thing to
work on a whole sample (everyone else --- I'm going to write
something that lets you write a generated quantities block that
will apply to an existing sample to generate the generated quantities
after the main iterations for sampling).

What I'm talking about here is a way to generate a single fake data
set to use as input to Stan to make sure my program is doing the
right hting. Right now, there's no easy way (that I know of) to convert
Stan output (in any interface) into something that can be fed back in directly
as data.

- Bob

Jan Tünnermann

unread,
Oct 17, 2015, 5:48:20 PM10/17/15
to stan-...@googlegroups.com
That would be great tool for doing power analysis as well. Jan

Andrew Gelman

unread,
Oct 17, 2015, 9:18:26 PM10/17/15
to stan-...@googlegroups.com
Ahh, I see, thanks for explaining.

Ben Goodrich

unread,
Oct 17, 2015, 10:15:57 PM10/17/15
to Stan users mailing list
On Saturday, October 17, 2015 at 5:46:12 PM UTC-4, Bob Carpenter wrote:
What I'm talking about here is a way to generate a single fake data
set to use as input to Stan to make sure my program is doing the
right hting.  Right now, there's no easy way (that I know of) to convert
Stan output (in any interface) into something that can be fed back in directly
as data.

It would somewhat help if PRNG functions could be called in transformed data.

Ben

Luc Coffeng

unread,
Oct 18, 2015, 6:30:25 AM10/18/15
to Stan users mailing list
Hear hear, that would be extremely useful!!

Alp Kucukelbir

unread,
Oct 18, 2015, 2:11:04 PM10/18/15
to Stan users mailing list
+1 to being able to call PRNG functions in transformed data!

Dustin Tran

unread,
Oct 18, 2015, 4:09:17 PM10/18/15
to stan-...@googlegroups.com
I don’t completely follow the fixed_param and iter=1 thing, but yup a guide to do these kinds of things would be really helpful. If there’s additional R code you have to use, or small hacks to the Stan file, then putting this all in a script would be nice. If no one has a version of this then I’ll write the guide when I come around to it.

Dustin

Bob Carpenter

unread,
Oct 19, 2015, 1:09:48 PM10/19/15
to stan-...@googlegroups.com
You need fixed_param as an option so as not to try to sample or
optimize. It's an alternative way to run Stan that just reads in the
data and inits (or randomly inits), sets the parameters to the init values, then
executes the transformed parameter and generated quantities blocks.
So the parameters remained fixed every iteration. And then iter=1 is to
just get a single draw.

- Bob

Jy

unread,
May 7, 2017, 8:48:46 PM5/7/17
to stan-...@googlegroups.com
Dear Bob,

Is this possible now in Stan (or RStan) - i.e. a way to simulate the design matrix that will then be provided as input to Stan?
If yes, would you please point me to documentation of how to do this?

Thanks!
Jy

Bob Carpenter

unread,
May 8, 2017, 6:58:13 PM5/8/17
to stan-...@googlegroups.com
You are never going to be able to simulate the design matrix
without giving it a distribution---if it's just covariates,
there's nothing to simulate from. Usually the covariates don't
get modeled because everything's conditionally independent given
their value.

Which interface are you using?

- Bob

P.S. We're shutting down this list soon. We've switched to:

http://discourse.mc-stan.org

> On May 7, 2017, at 8:48 PM, Jy <jyoti....@gmail.com> wrote:
>
> Dear Bob,
>
> Is this possible now in Stan - i.e. a way to simulate the design matrix that will then be provided as input to stan?
> If yes, would you please point me to documentation of how to do this?
>
> Thanks!
> Jy
>
>
> On Saturday, 17 October 2015 17:46:12 UTC-4, Bob Carpenter wrote:
> What I'm talking about here is a way to generate a single fake data
> set to use as input to Stan to make sure my program is doing the
> right hting. Right now, there's no easy way (that I know of) to convert
> Stan output (in any interface) into something that can be fed back in directly
> as data.
>
> - Bob
>
>
>
Reply all
Reply to author
Forward
0 new messages