question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Posterior predictive check (plot_ppc) with dataframe encoding.

See original GitHub issue

Hello everyone!

I have a model which has two cases enconded in df[‘membrane_enc’], so this should give two indepently modelled likelyhoods and posteriors. I train the model in this fashion:

shape = df['membrane_enc'].unique().shape
with pm.Model() as my_model:
    tau = pm.HalfNormal('tau', sigma=1.,shape =shape)
    tau_0 = pm.TruncatedNormal('tau_0', mu = min_val, sigma = 1, lower = 0, upper = min_val, shape =shape)
    like = DensityDist('like', logp_exp_lag(tau_0[df['membrane_enc']],tau[df['membrane_enc']]), observed=df['tpore'], random=custom_random, shape = (2,180))
with my_model:
    my_model_trace = pm.sample(4000, tune=3000,  random_seed=RANDOM_SEED)
    ppc = pm.sample_posterior_predictive(my_model_trace, var_names = ['like'],  random_seed=RANDOM_SEED)
    my_model_trace = az.from_pymc3(trace=my_model_trace, model=my_model, posterior_predictive=ppc)
    my_model_trace.to_netcdf(model_path)

The model converges nicely and gives reasonable posteriors for the two encodings. The problem comes when I try to plot the posterior predictive. I cannot find the way to plot the two distinct ppc’s in two graphs:

az.plot_ppc(my_model_trace, random_seed=RANDOM_SEED, var_names = ['like'], flatten = [])

This code fails and I am not sure which dimension I should flatten since there is only one in my_model_trace, like_dim_0 and it doesn’t work. All this code works nicely if you only work with one dimension.

Can someone please help me make this plotting work?

By the way, I am using arviz 0.10.0.

Thank you very much in advance! Best, Sergio

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sperezconesacommented, Jan 7, 2021

Thanks for the pointers, now the code looks like:

my_model_trace = my_model_trace.assign_coords(like_dim_0=df['membrane_enc'].to_numpy(),
                                              groups='observed_data')
my_model_trace.posterior_predictive = my_model_trace.posterior_predictive.rename({'like_dim_1':'membrane_enc'})
my_model_trace.observed_data = my_model_trace.observed_data.rename({'like_dim_0':'membrane_enc'})
az.plot_ppc(my_model_trace,  flatten=['draw', 'chain', 'like_dim_0']) # flatten all dimension except the one we are interested in

I have added labels to `like_dim_1’ and renamed the coordinates that represent the encoding. Now I get the result I was looking for: image Thanks a lot for your help!

0reactions
OriolAbrilcommented, Jan 7, 2021

Glad I could be of help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 20 Posterior Predictive Checks
A posterior predictive check compares simulated data using a draw of your posterior distribution to the observed data you are modelling - usually...
Read more >
Graphical posterior predictive checks using the bayesplot ...
This vignette focuses on graphical posterior predictive checks (PPC). Plots of parameter estimates from MCMC draws are covered in the ...
Read more >
Posterior Prediction from Logit Regression - The Stan Forums
I ran the model as a logistic regression with levels-means coding of group factors (each group getting their own intercept parameter). Here is ......
Read more >
posterior predictive check for hddmRegression model
Hi,. I tried to implement a simple posterior predictive check for a simple hddm regression model (using patsy to implement contrast coding of...
Read more >
Posterior Predictive Checks for brmsfit Objects — pp_check ...
Perform posterior predictive checks with the help of the bayesplot package. # S3 method for brmsfit pp_check( object, type, ndraws = NULL, nsamples...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found