Tutorial for analyzing instrumental learning data with the HDDMnnRL module

import hddm
import pickle
import pandas as pd

Load the data

with open('./angle_d1_c3_s20_t500.pickle', 'rb') as handle:
    datafile = pickle.load(handle)

# Here, datafile is saved as a list of datasets. We pick the first dataset.
dataset = datafile[0]
1
# Reformat the dataset as a dataframe
data = hddm.utils.get_dataset_as_dataframe_rlssm(dataset)

Initialize the HDDMnnRL model and sample

# Specify number of samples and burnins
nsamples = 100
nburn = 50
m = hddm.HDDMnnRL(data, model='angle', rl_rule='RWupdate', non_centered=True, include=['v', 'a', 't', 'z', 'theta', 'rl_alpha'], p_outlier = 0.0)
m.sample(nsamples, burn=nburn, dbname='traces.db', db='pickle')
Printing model specifications --
ssm:  angle
rl rule:  RWupdate
using non-centered dist.:  False
Using default priors: Uninformative
Supplied model_config specifies params_std_upper for  z as  None.
Changed to 10
Supplied model_config specifies params_std_upper for  rl_alpha as  None.
Changed to 10
 [-----------------101%-----------------] 101 of 100 complete in 256.1 sec
<pymc.MCMC.MCMC at 0x7f5b5c224f10>

Save the model

# Save the model
m.save('rlssm_model')
# Load the model
# model = hddm.load('rlssm_model')

Check the posterior results

m.plot_posteriors()
Plotting v
Plotting v_std
Plotting a
Plotting a_std
Plotting z
Plotting z_std
Plotting t
Plotting t_std
Plotting theta
Plotting theta_std
Plotting rl_alpha
Plotting rl_alpha_std
_images/demo_HDDMnnRL_12_1.png _images/demo_HDDMnnRL_12_2.png _images/demo_HDDMnnRL_12_3.png _images/demo_HDDMnnRL_12_4.png _images/demo_HDDMnnRL_12_5.png _images/demo_HDDMnnRL_12_6.png _images/demo_HDDMnnRL_12_7.png _images/demo_HDDMnnRL_12_8.png _images/demo_HDDMnnRL_12_9.png _images/demo_HDDMnnRL_12_10.png _images/demo_HDDMnnRL_12_11.png _images/demo_HDDMnnRL_12_12.png
# Load the trace
with open('./traces.db', 'rb') as handle:
    tracefile = pickle.load(handle)
# Re-format traces as a dataframe
traces = hddm.utils.get_traces_rlssm(tracefile)
model_ssm = 'angle'
model_rl = 'RWupdate'

config_ssm = hddm.model_config.model_config[model_ssm]
config_rl = hddm.model_config_rl.model_config_rl[model_rl]
hddm.plotting.plot_posterior_pairs_rlssm(tracefile, config_ssm['params'] + config_rl['params'])

Posterior Predictive Checks

num_posterior_samples = 3
p_lower = {0: 0.15, 1:0.30, 2:0.45}
p_upper = {0: 0.85, 1:0.70, 2:0.55}
ppc_sdata = hddm.plotting.gen_ppc_rlssm(model_ssm, config_ssm, model_rl, config_rl, data, traces, num_posterior_samples, p_lower, p_upper, save_data=True, save_name='ppc_data')
100%|██████████| 3/3 [05:49<00:00, 116.55s/it]
ppc data saved at ./ppc_data.csv
# Load the saved ppc data
# ppc_sdata = pd.read_csv('./ppc_data.csv')
_ = hddm.plotting.plot_ppc_choice_rlssm(data, ppc_sdata, 40, 10)
_images/demo_HDDMnnRL_20_0.png
_ = hddm.plotting.plot_ppc_rt_rlssm(data, ppc_sdata, 40, 0.06)
_images/demo_HDDMnnRL_21_0.png