I try to plot the posterior predictive of y and X_1 in separate plots.
I use
az.plot_ppc(az.from_pymc3(posterior_predictive=ppc_model, model=model))
However this gives me a 1x2 subplot
Is there a way to plot them separately in one plot each?
Thanks in advance!
Like this maybe?
trace = az.from_pymc3(posterior_predictive=ppc_model, model=model)
az.plot_ppc(trace, var_names=['X'])
plt.show()
az.plot_ppc(trace, var_names=['y_like'])
plt.show()
2 Likes
Thank you!
Analogously, is there also a way to separate the trace from the posterior in Arviz’ traceplot function az.plot_trace?
az.plot_trace(trace, var_names=["X"])
Yes, most (all?) of the arviz plotting calls take the var_names
argument. The calls are all detailed here.
Oh, I think you didn’t understand me correctly. This was my fault.
I meant I would like to separate the posterior (kernel density) from the trace plot, this time. Because I don’t need the posterior plot as I generate it using az.plot_posterior.
trace_shohome_s1_re_norm_imp.pdf (62.5 KB)
Ah, sorry about that. If you just want to posterior, then use plot_posterior()
.
Also, that image you posted may be an example, but it indicates a large number of divergences. If you are sampling “for real”, you should not interpret any posterior you might be getting until you have dealt with and eliminated those divergences.
Thanks for this.
Is there also a way to only plot the trace (i.e. no posterior)
Not clear to me (@OriolAbril ?). But at that point you are basically just plotting the trace object itself. So something like this if you are using a MultiTrace object (i.e., return_inferencedata=False
):
plt.plot(trace['param_name'].flatten())
Or this if you are using an InferenceData object:
plt.plot(trace.['posterior']['param_name'].values.flatten())
The details (e.g., the shapes) will depend on your model, but that’s the basic idea.
1 Like
The right column of the plot_trace
is a line plot of the posterior. The name plot_posterior
is a bit confusing, especially given how it can be used for other groups too. The left column of plot_trace
is a kde or histogram of the posterior, the same type of plot as plot_posterior
or plot_density
but each has its own particular style. I think I understood the question after reading the previous discussion but wanted to clarify that I am plotting the posterior, even if using a different type of visualization than plot_posterior
I believe a variation of the code below adapted to your case will achieve what you want
import arviz as az
import matplotlib.pyplot as plt
idata = az.load_arviz_data("rugby")
idata.posterior.atts.plot(x="draw", hue="chain", col="team", col_wrap=3)
plt.show()
InferenceData
is not 100% custom made like MultiTrace so it builds on top of all the amazing work done by the xarray team. We can therefore use it’s methods to plot like we did here but also to summarize quantities, perform post-processing… everything using named dimensions, which is something I personally love because otherwise I mess up the dimension order, or come back to the code and don’t remember anymore if axis=2 meant reducing the subject or the experiment dimension.
2 Likes