modelling factor interactions

542 views
Skip to first unread message

Tobias Hauser

unread,
Feb 23, 2016, 10:45:46 AM2/23/16
to hddm-users
Dear all

I have a design with 2 factors (A, B) and there is an interaction of them on RT and accuracy. I.e. under condition A==1, the effect of B is an increase in RT, whereas if A==2, then B causes a decrease in RT. Is there a good way to model that for a drift rate/etc?
I could just parameterize the whole into one factor using the interaction (i.e., B(x|A=1)=[1 2 3], B(x|A=2)=[3 2 1]), but this omits any offset differences between the conditions. Is there any other way to model it?
Ideally, my model would test the effect of condition A, B, and their interaction A*B on drift rate/etc.
Thanks a lot for your help!

Best,
Tobias

Sam Mathias

unread,
Feb 23, 2016, 11:01:50 AM2/23/16
to hddm-users
Assuming your design is completely within-subjects, use the HDDMRegressor class:


Since that module takes patsy formulae, you can easily code interaction terms as well as main effects. Assuming there are only two levels of A and B, something like 'v ~ 0 + C(A) * C(B)' should do what you want.

Samuel Robert Mathias
Postdoctoral Associate
Department of Psychiatry, Yale School of Medicine
Suite 3014, 2 Church Street South
New Haven, CT 06519
http://www.srmathias.com

--
You received this message because you are subscribed to the Google Groups "hddm-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hddm-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Hauser

unread,
Feb 23, 2016, 11:37:10 AM2/23/16
to hddm-users
Thanks Samuel! I was also thinking of using HDDM-Regression, but wasn't sure whether there is a more straight forward way to doing it...
Best,
Tobias

Chuan-Peng Hu

unread,
Nov 12, 2016, 1:51:44 AM11/12/16
to hddm-users
Dear all, 

I encountered a similar problem recently, I am new to python, sorry if my question is too sample.

I also have a 2 by 2 within-subject design. let's say that one is called "moral", the other is "id" .
As suggested by Samuel, I used follow code:

m_within_subj_L = hddm.HDDMRegressor(datL_match2, "v ~ C(id)*C(moral)", include = ['t','a', 'v','z'],keep_regressor_trace=True, p_outlier = 0.05)

After running the above code, following was shown:
Adding these covariates:
 ['v_Intercept', 'v_C(id)[T.self]', 'v_C(moral)[T.moral]', 'v_C(id)[T.self]:C(moral)[T.moral]']

then I run the following code

# estimate the posterior, sampling 5000 times, discard the first 200
m_within_subj_L.sample(5000, burn=200)

Then I tried to get the parameter "v" for each:

v_Intercept,v_id,v_moral,v_id_moral = m_within_subj_L.nodes_db.ix[['v_Intercept', 
                                                                 'v_C(id)[T.self]', 
                                                                 'v_C(moral)[T.moral]', 
                                                                 'v_C(id)[T.self]:C(moral)[T.moral]'],'nodes']

but get an error:

v_Intercept,v_id,v_moral,v_id_moral = m_within_subj_L.nodes_db.ix[['v_Intercept', 
                                                                 'v_C(id)[T.self]', 
                                                                 'v_C(moral)[T.moral]', 
                                                                 'v_C(id)[T.self]:C(moral)[T.moral]'],'nodes']
Traceback (most recent call last):

  File "<ipython-input-63-9c984221c78f>", line 4, in <module>
    'v_C(id)[T.self]:C(moral)[T.moral]'],'nodes']

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 75, in __getitem__
    return self._getitem_tuple(key)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 784, in _getitem_tuple
    return self._getitem_lowerdim(tup)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 908, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1018, in _getitem_axis
    return self._get_label(key, axis=axis)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 93, in _get_label
    return self.obj._xs(label, axis=axis)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\generic.py", line 1740, in xs
    return self[key]

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__
    return self._getitem_column(key)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\internals.py", line 3290, in get
    loc = self.items.get_loc(item)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\indexes\base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)

  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)

  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)

  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)

KeyError: 'nodes'

I also tried this code as suggested in this post 

v_Intercept,v_id,v_moral,v_id_moral = m_within_subj_L.nodes_db.node[['v_Intercept', 
                                                                 'v_C(id)[T.self]', 
                                                                 'v_C(moral)[T.moral]', 
                                                                 'v_C(id)[T.self]:C(moral)[T.moral]'],'nodes']

but also returned an error:
v_Intercept,v_id,v_moral,v_id_moral = m_within_subj_L.nodes_db.node[['v_Intercept', 
                                                                 'v_C(id)[T.self]', 
                                                                 'v_C(moral)[T.moral]', 
                                                                 'v_C(id)[T.self]:C(moral)[T.moral]'],'nodes']

Traceback (most recent call last):

  File "<ipython-input-65-06fbb44ee34d>", line 4, in <module>
    'v_C(id)[T.self]:C(moral)[T.moral]'],'nodes']

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\series.py", line 624, in __getitem__
    return self._get_with(key)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\series.py", line 637, in _get_with
    return self._get_values_tuple(key)

  File "C:\Program Files\Anaconda2\lib\site-packages\pandas\core\series.py", line 682, in _get_values_tuple
    raise ValueError('Can only tuple-index with a MultiIndex')

ValueError: Can only tuple-index with a MultiIndex

Not, I am not sure how to get the value of parameter.

Thomas Wiecki

unread,
Nov 12, 2016, 4:24:24 PM11/12/16
to hddm-...@googlegroups.com
Can you try replacing .ix with .loc? If that doesn't help, what does m_within_subj_L.nodes_db.columns say?

--
You received this message because you are subscribed to the Google Groups "hddm-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hddm-users+unsubscribe@googlegroups.com.

Chuan-Peng Hu

unread,
Nov 13, 2016, 12:00:34 PM11/13/16
to hddm-users
Hi, Thomas,

Thank you for your quick response.
I tried .loc, it returns "KeyError: 'the label [nodes] is not in the [columns]'"

the m_within_subj_L.nodes_db.columns , it says: "IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices"
To unsubscribe from this group and stop receiving emails from it, send an email to hddm-users+...@googlegroups.com.

Micah Johnson

unread,
Nov 27, 2016, 1:53:37 AM11/27/16
to hddm-users
Hi,

Perhaps this may be of help to some...I've been playing around with different combinations, and here's what I've found (using Python 2.7, IPython and Anaconda 4.2.0)

## note: (nothing worked if I first entered, %matplotlib inline , in command line, no idea why)

v_test = test.nodes_db.ix[["v_C(T, Treatment('b'))[T.t]:C(S, Treatment('LO'))[T.LP]"],'node']
    # works! allows hddm.analyze.plot_posterior_nodes(v_test)

v_test2 = test.nodes_db.loc[["v_C(T, Treatment('b'))[T.t]:C(S, Treatment('LO'))[T.LP]"],'node']
    # works as well

v_test3 = test.nodes_db.node[["v_C(T, Treatment('b'))[T.t]:C(S, Treatment('LO'))[T.LP]"]]
    # works as well

v_test4 = test.nodes_db.node[["v_C(T, Treatment('b'))[T.t]:C(S, Treatment('LO'))[T.LP]"],'node']
    # # ValueError: Can only tuple-index with a MultiIndex

## note: above code also works for me when doing multiple at a time, e.g. v_test1, v_test2 = test.nodes_db.ix[["patsy string1", "patsy string2"],'node']

## also, not 100% sure, but for me it seemed like none of this code worked if I was trying to apply it to a concatenated model of multiple chains

# in case anyone needs to know how to save output...
hddm.analyze.plot_posterior_nodes(v_test)
plt.xlabel('v')
plt.ylabel('post prob')
plt.title('woohoo!')
plt.savefig('v_test.jpg')

Cheers!
Micah

Micah Johnson

unread,
Nov 27, 2016, 2:02:20 AM11/27/16
to hddm-users
Also, Chuan-Peng, I just noticed that you have a typo in your code... should be 'node' not 'nodes', I believe

Chuan-Peng Hu

unread,
Nov 27, 2016, 9:10:42 AM11/27/16
to hddm-users
Hi, Micah,
Many thanks for pointing out the typo! I am so embarrassed.
After correcting the typo,  m_within_subj_L.nodes_db.loc and m_within_subj_L.nodes_db.ix both works!!
Thank you again, and apology for my typo.
Reply all
Reply to author
Forward
0 new messages