Plotting Interaction Effects with Pymc3

Interpreting continuous interactions is extremely difficult, especially through table of numbers. That’s why I read it is recommended that we plot interactions.

So I’m trying to plot a two-way interaction between two continuous variables.

McElreath shows us a nice example, where Water and Shade have only 29 integer values of either -1, 0, or 1.

However, in my case I’m trying to plot a two-way interaction between predictor variables that have 74 different FLOAT values (for a completely different experiment of plants).

With so many different water values, I’m not sure it makes sense to try the same plots as McElreath did. Is it advisable to choose the max, min, and mean values of water to represent the plots, like I did?

Are there other ways to plot interactions with PYMC3?

I think it boils down to your stylistic preference. Your data has a feature space of 74x2 and an observed value of shape 74x1.

The first thing that comes to my mind in terms of visualization is to plot the predicted mean effect as a heatmap (you could also use the basic imshow from pyplot). This would show you a full picture of your fitted mean effect. To be honest, I’m not a big fan of heatmaps, it’s hard to tell the value by the color alone so I tend to try to find alternative visualizations, but I think they are a nice first step. The main issue is that you cannot see the uncertainty in this plot.

The second alternative I thought of was to plot all data, with their predictions in the same axes, but each line could be color coded to indicate a water value. This plot could end up very cluttered depending on how structured your data and the regressions are. To avoid the clutter you could add a vertical offset as in a ridgeplot (bear in mind that the link’s example shows the ridge plot applied to kdes, and you would plot something completely different using the FacetGrid).

My last idea is to either make an interactive plot, where you can manually explore how the regression and data change with water level. A very crude matplotlib implementation can be found here. There are many other options for interactive plots using D3 but I’m not an expert in those. Finally, if none of the above satisfy you, you could make an animation that strings plots for several water levels together.

Regarding plotting slices, it’s perfectly valid but you need to know that you are not visualizing all the information, so you could draw the wrong conclusions. That’s why the suggestions I listed above aimed at trying to at least see the mean regression value for every feature combination.

An arviz plotting feature you could maybe try is plot_pair, by concatenating your feature array with the blossoms observations array. This would show you some marginal covariances in the raw data.


After reading the first answer, another option is this They are using Bokeh.


thank you @rosgori.

Thank you @lucianopaz for all the great options to plot. I’m going to try your second suggestion of plotting plot all data, with their predictions in the same axes.