# Decomposing dynamical subprocesses for compositional generalization

This is the code supplement for the statistical models/GLMs implemented in
R, fitting and simulations of successor feature models implemented in MATLAB

## Instructions

To run the code, you'll need to install RStudio (specific libraries: rethinking - uses RStan) and MATLAB (version 2020b).

Follow these steps to go through the results:
1.) Statistics/GLMs: use stats.Rmd in RStudio - preprocessed data (long data format) is loaded from subfolder "data". 
You will have to set paths to this folder in the script, depending on where it is stored on your computer.

2.) Model simulations (prior): use sim_SR.m, sim_SF_feature.m and sim_SRSF_hybrid.m in MATLAB to run prior simulations with successor feature models. 
Scripts use task schedules from subfolder "schedules", schedule numbers from "sched_num_all.mat" and load parameter values from space_alpha.mat, space_omega.mat, space_tau.mat and 
Scripts use function "plotting simulations" and "SR_update.m".
Scripts store results in "prior simulations". 

3.) Model fitting (MLE): use fit_models_main_function.m in MATLAB. Script uses "*_model.m" functions implementing the models and task schedules from 
subfolder "schedules". Uses helper functions from subfolder "functions", stores fitted parameters in subfolder "fitted_parameters" 
(also contains fitted parameters from Structural Inference model - fits_all_llmax.csv)

4.) Model comparison: use model_comparison.m in MATLAB. Loads fitted parameters, likelihood values and subject IDs/condition labels stored in 
subfolder "fitted_parameters". Plots BIC values and model frequencies.

5.) Model simulations (posterior): use sim_posterior_main.m in MATLAB to posterior simulations of successor feature models with fitted parameter estimates 
(loaded from subfolder "fitted_parameters"). Stores results in subfolder "posterior simulations". Needs parallel pool, iterations (1000) may take some time, depending on how many parallel workers you can call.

6.) Plot posterior simulations using "sim_plots_ppc.m" in MATLAB: Uses "all_sim_plots.m" and functions from subfolder "functions"


############

The Structural Inference model (model 4) is implemented in
`julia`.

To run the code, you'll need to install julia. See https://julialang.org/downloads/
for instructions.

Then start julia from this folder, and run the main file:

    julia main.jl

This will run the preprocessing and analyses for the results relating to the Structural Inference model/model 4
from the paper.

You can also open the file in an editor of your choice and play around with the
model.
