Pystan and shinystan

398 views
Skip to first unread message

Noah

unread,
Feb 11, 2017, 7:22:44 AM2/11/17
to Stan users mailing list
Hi,

I'm using Pystan for a project.  (Entire framework for this project was built in Python, so that's the easiest way to call Stan.)

The Shinystan package is awesome, and something I'd very much like to use for model diagnostics. However, Shinystan only works through R

There is a nice Python library to call R (it actually runs R as an instance inside of Python somehow) called: "rpy2".  I can easily launch the shinystan demo through Python, using that library.  Works great!

However, the problem comes with passing the samples from Pystan through rpy2 to R.  Pystan uses a completely different data format, so it won't transfer directly.

Has anybody else tried this?

Does anybody have any knowledge on how to construct an object in R, by hand, that shinystan will accept?


Thank You,

Mikhail S. Spektor

unread,
Feb 11, 2017, 7:43:45 AM2/11/17
to stan-...@googlegroups.com
Hello Noah,

You can pass the chains to R and then run ShinyStan (but you will not have all the functions available). Like this (if you're using Jupyter Notebook, otherwise you have to replace the R magic with the appropriate rpy2 commands):

temp = fit.extract(permuted=False, inc_warmup=True)

%%R -i temp
require('shinystan')
temp
<- as.shinystan(temp, burnin = 1000)
temp
<- launch_shinystan(temp)

Best,
Michael

Noah

unread,
Feb 11, 2017, 11:43:48 AM2/11/17
to Stan users mailing list
Hi Michael,

That *almost* works

from rpy2.robjects.packages import importr
shinystan = importr('shinystan')

temp = fit.extract(permuted=False, inc_warmup=True)
sso
= shinystan.as_shinystan(temp)

NotImplementedError: Conversion 'py2ri' not defined for objects of type '<class 'numpy.ndarray'>'



Thoughts?

Thank You!

Noah

unread,
Feb 11, 2017, 11:48:57 AM2/11/17
to Stan users mailing list
Update,

I got it a bit further.  This now launches shinystan without error, BUT I can't see any plots of the chains. 

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

shinystan = importr('shinystan')
temp = fit.extract(permuted=False, inc_warmup=True)
sso = shinystan.as_shinystan(temp)
shinystan.launch_shinystan(sso)

Jonah Gabry

unread,
Feb 11, 2017, 1:31:16 PM2/11/17
to Stan users mailing list
I don't know much about running R through Python, but as.shinystan will allow you to create shinystan objects in R after importing your stuff from wherever. In the latest release there's even now a sampler_params argument so you can pass the NUTS sampler parameters even if you didn't fit the model using RStan. Although that might require more work to format them correctly if bringing them over from Python. The format required is the same format returned by rstan's get_sampler_params function.

In the future we hope to have a version of ShinyStan running entirely on the web but there's still a lot of work to be done on that.

Jonah

Bob Carpenter

unread,
Feb 12, 2017, 5:26:54 PM2/12/17
to stan-...@googlegroups.com
And we are going to standardize all of our serialized (file based, network based, etc.) formats, so this won't be a problem in the future.

- 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.

Reply all
Reply to author
Forward
0 new messages