How to combine two posteriors plot into one graph?

349 views
Skip to first unread message

傅韋銘

unread,
Nov 19, 2020, 4:06:27 AM11/19/20
to hddm-users
Dear all,
I have a naive question about posterior plotting. I have a regression model with multiple regressors (ex: v~1+X1+X2). I am able to get the posterior of v_X1 and v_X2 in two different figures. I now hope to combine the two posteriors into one identical axis figure but I can not figure out how to do it with the package or with pasty. I am wondering if there is anyone who can guide me through the problem. I will be grateful for the help.

Sincerely.
Wei-ming

Sam Hewitt

unread,
Nov 19, 2020, 10:26:04 AM11/19/20
to hddm-users
Hi Wei-ming, 

If you have grabbed the posteriors fine to plot separately then it will be simple to plot these in one figure. You should look at the matplotlib documentation, as hddm uses this to make the figures. 

Plot multiple subplots with matplotlib

plot multiple scales on one figure:

See an example from my data, where I plot multiple groups. You should be able to do this by replacing the node reference for v_X1 and v_X2 and submitting these to hddm.analyse.plot_posterior_nodes: 

#model:
mreg6_last165 = hddm.models.HDDMRegressor(data, ['v ~ dot_difference*C(group)'], depends_on={'a':'group', 't':'group'}, group_only_nodes=['v', 'a', 't'])

#get nodes
v_dots_pre = mreg6_last165.nodes_db.node['v_dot_difference:C(group)[T.1]']
v_dots_early = mreg6_last165.nodes_db.node['v_dot_difference:C(group)[T.2]']
v_dots = mreg6_last165.nodes_db.node['v_dot_difference']

#plot nodes
hddm.analyze.plot_posterior_nodes([v_dots, v_dots_pre, v_dots_early], bins=20)
plt.xlabel('drift rate')
plt.ylabel('posterior probability')
plt.title('Group*stimulus interaction effect on v')

Yusuf Badaru

unread,
Nov 24, 2020, 10:26:31 AM11/24/20
to hddm-users
Hi Sam,

I am also new to hddm. I have a question related to what was asked. I have a model with the following parameters, hddm.HDDM(data,  depends_on={'v':'stim', 't':'response'}). However, from the posterior plot, what 1 get is stim_0 in one plot and stim_1 in another plot. How can I have stim_0_1 in the same plot? I was not able to understand this from the explanation above. 

Sam Hewitt

unread,
Nov 25, 2020, 4:54:30 AM11/25/20
to hddm-users
Can you post the code you have written? It is hard to answer without this. 

If your stim is coded as 0 and 1, from your text I would guess that you have a node called v(0) and v(1), which are drift rates for each stim. If that's the case you can specify the nodes as variables and then plot these together. Something like this. M is your model: 

v_stim0 = m.nodes_db.node['v(0)']
v_stim1 = m.nodes_db.node['v(1)']
hddm.analyze.plot_posterior_nodes([v_stim0, v_stim1], bins=20)

Bins is an optional argument, you can change this to specify the size of the distribution steps. 

Sam

Yusuf Badaru

unread,
Nov 25, 2020, 8:13:32 AM11/25/20
to hddm-users
Hello Sam

Thank you for your response. Here is my code;
m = hddm.HDDM(data, informative=False, is_group_model=False, depends_on={'v':'stim', 't':'response'}, bias=True, trace_subjs=True, include=('z','sv', 'st', 'sz'))
m.sample(5000, burn=1000)
where 'stim' (eg -10.0, 10.0, and so on) is the different levels of conditions and 'response' is coded 0 and 1 indicating the choice of response. I want to plot the posterior predictive to show how the model fits my data for each level of condition and response. But the plot I keep getting is -10.0(0) in one plot and -10.0(1) in another plot instead of -10.0(0,1). I will be grateful for the help.

Sam Hewitt

unread,
Nov 25, 2020, 8:30:13 AM11/25/20
to hddm-...@googlegroups.com
Hi Yusuf, 

This is the code for generating the model. I need the lines you have used to produce this plot. 

Did you try the solution I wrote? 

--
You received this message because you are subscribed to a topic in the Google Groups "hddm-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hddm-users/0Z5km6dyvNg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hddm-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hddm-users/09fe1a2e-3114-4498-aea0-08c7107b2dcen%40googlegroups.com.

Yusuf Badaru

unread,
Nov 25, 2020, 11:06:11 AM11/25/20
to hddm-users
Hello Sam,
I used m.plot_posterior_predictive() to generate the plot. I tried the solution you wrote, but what it does is to plot the   posteriors of (v) for the different conditions and  posteriors of (t)  for the choices. However,   I want to plot the posterior predictive to show how the model fits my data for each level of condition and response. 

傅韋銘

unread,
Nov 27, 2020, 1:53:54 AM11/27/20
to hddm-...@googlegroups.com
Hi Sam,
Thank you for the suggestion. It works pretty well. Sincerely thank you for the help.

Wei-ming

Sam Hewitt <samrc...@gmail.com> 於 2020年11月19日 週四 下午11:26寫道:
--
You received this message because you are subscribed to a topic in the Google Groups "hddm-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hddm-users/0Z5km6dyvNg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hddm-users+...@googlegroups.com.

Melissa

unread,
Nov 11, 2021, 7:26:23 PM11/11/21
to hddm-users
Screenshot 2021-11-12 at 00.06.10.pngScreenshot 2021-11-12 at 00.11.59.pngHi Sam,

I have a question related to it. If you could reply to me, I'll be very appreciated. 

I'd like to also combine posterior plotting for some model components across the conditions. 
Basically, I have 2 by 2 design. 2 different tasks (swapping/non_swapping) with 3 different difficultly levels(hard/middle/easy).
For the groups, that I coded as "task_type". For the task levels, that I coded as "stim". I've attached a screenshot from my data view. 

I'd like to look at the posterior plotting differences between the two tasks(swapping/non_swapping). 
I mean, I'd like to combine my task conditions (hard, middle, easy) for each task.
I can see my conditions separately now. I added the code below and attached the drift rate figure as an example.
But, I'm a little bit confused about how can I combine them. Any help would be appreciated. 

Many thanks in advance,
Best,
Melissa,

Code:

plt.rcParams['font.size'] = '16'
plt.figure(figsize = (16,3))
plt.subplot(131)
v_Hard_swapping, v_Hard_non_swapping, v_Middle_swapping, v_Middle_non_swapping, v_Easy_swapping, v_Easy_non_swapping = best_model.nodes_db.node[['v(Hard.swapping)', 'v(Hard.non_swapping)', 'v(Middle.swapping)', 'v(Middle.non_swapping)', 'v(Easy.swapping)', 'v(Easy.non_swapping)']]
plot_posterior_nodes([v_Hard_swapping, v_Hard_non_swapping, v_Middle_swapping, v_Middle_non_swapping, v_Easy_swapping, v_Easy_non_swapping],
                     cols = ['#ff1493', '#ff1493', '#0070e0','#0070e0', '#dd0000', '#dd0000'], leg=False, 
                     linestyles = ['-', '--', '-', '--', '-', '--'])
plt.xlabel('drift-rate ($v$)', fontsize = 15)
plt.ylabel('Posterior probability', fontsize = 15)
plt.legend(['hard swapping', 'hard non_swapping', 'middle swapping', 'middle non_swapping', 'easy swapping', 'easy non_swapping'],
           bbox_to_anchor=(1,1), loc="upper left",  prop={'size': 14})
plt.savefig('image_posterior_swapping_task.svg', format='svg', dpi=1200, bbox_inches='tight')
plt.show()



Sam Hewitt

unread,
Nov 12, 2021, 3:49:42 AM11/12/21
to hddm-...@googlegroups.com
Hi Melissa, 

If I understand correctly, basically you want to compare the effect of task_type (swapping / non-swapping). In which case you should run a separate model where only this is estimated and you ignore stim (hard middle easy). This will directly answer your question. You may find that this model has a lower DIC or BPIC and so might consider this to be a more parsimonious model to your data. As long as it meets other model requirements e.g., it accurately reproduces (simulates) reaction time data of your participants. 

Looking at your plot, there is clearly an effect of task_type (drift rate is faster in all non-swapping tasks). 

Combining the posteriors after their hierarchical estimation doesn't make sense - you should estimate a model which does this itself. For example, 

model = hddm.models.HDDMregressor(data, [ v ~ c(task_type)]) 

Then compare this to your model above which includes stim as an extra set of parameters. 


--
You received this message because you are subscribed to a topic in the Google Groups "hddm-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hddm-users/0Z5km6dyvNg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hddm-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages