Towards efficient IGT model simulation/estimation in R/Stan

As part of my playing around with alternative objective functions for estimating reinforcement learning models of the Iowa Gambling Task (IGT), I needed a way to quickly simulate large numbers of participants. Since base R is slow, I implemented a few models in C++, but then I realized that it was pretty easy to set up an R package with Stan as the backend, so I did, or will have done in the future, but have also partly done already, now, in the present. What I wanted was a way to build models with arbitrary combinations of utility/updating/temperature functions without having to write entirely new model files.

The R package (in progress and largely undocumented/untested, caveat emptor) allows for the efficient simulation/estimation of reinforcement learning models of the Iowa Gambling Task (IGT), or n-armed bandit tasks more generally. The package implements several utility, updating, and temperature functions, and allows them to be combined arbitrarily to create new models. Both simulation and estimation use Stan, making the package very quick. The package can generate either full posterior distributions for parameters, or penalized ML estimates and CIs.

The package can be installed and loaded using the devtools package with

Suppose that we want to simulate performance on the deck configuration using a model with a prospect utility function, and delta learning rule, and a trial independent temperature function. Then we can specify

where scale = .01 indicates that wins and losses should be divided by 100 (as is standard), and full.output = FALSE returns only the trial choices, wins, and losses (otherwise, the function will return the deck valuations, utilities, and choice probabilities for every trial).

The object sim is a list containing arrays choice, wins, and losses.

Estimation

To fit a model to observed data, we first specify the utility, updating, and temperature functions (already done above), and create a list specifying the desired parameter bounds.

where reg is a regularization term. All parameters are initialized with support on the unit interval and a beta(reg,reg) prior. These raw parameters are then scaled to the width specified by the user. A value of reg = 1 is equivalent to a uniform prior, with larger values concentrating more tightly in the center of the parameter space. IGT model estimates have an annoying tendency to cluster towards the boundaries of the parameter space, owing largely to most models of the IGT being crappy models. I would generally recommend using something like reg = 2 (at least) in practice, especially for ML estimation.

The argument method = "bayes" causes the function to return a stanfit object with the full posterior samples for all parameters. If point estimates are needed, then ideally the user would still just set method = "bayes" and take the posterior means, but if speed is a concern, then method = "map" returns maximum a posteriori estimates along with (in the near future) confidence intervals. With the reg term, these can be thought of almost as l2 regularized ML estimates, with shrinkage towards the center of the parameter space.

Adding new functions

The package is designed to make it relatively simple to add new utility/updating/temperature functions. I plan to make a more complete guide in the future, but the basic steps for adding a new utility function are as follows (adding new updating/temperature functions works the same way, mutatis mutandis):

Add the new utility function to inst/chunks/utilityFunctions.stan. Note that all utility functions must have the same signature! Look at the other functions to see how this works

Add a new conditional statement to the end of inst/chunks/utilityWrapper.stan. Again, the function is simple, so you’ll see how it works.

The package contains an internal lookup table in the form of a list called modelComponentDetails, which contains information about the number of parameters in each utility function, bounds on those parameters (if any), and the parameter names. You’ll need to add the necessary information to the list and save it to R/sysdata.rda. Note that order is important: The order of functions in modelComponentDetails must be identical to the order in inst/chunks/utilityFunctions.stan.

Then recompile/reinstall. Everything else should work more or less automatically.