Chapter 7 DDM Simulation Function

Simulate the DDM for a single trial. Simulation will stop and report NA if subject takes longer than 2 minutes for a single trial

simulate_ddm <- function(evidence, a = 1.0, d = 0.001, s = 0.03, b = 0.0, c = 0.0, ts_size = 10, rt_max = 120000) {
  
  bounds = a
  rdv_t = b
  inside_bounds = T
  timestep = 0
  
  while(inside_bounds==T & timestep<(rt_max/ts_size)) {
    rdv_t = rdv_t + d * evidence
    rdv_t = rdv_t + rnorm(1, 0, s)
    timestep = timestep + 1
    if (abs(rdv_t)>=bounds) {inside_bounds = F}
    rdv_t_1 = rdv_t
  }
  
  if (rdv_t >= bounds) {choice = 1}
  if (rdv_t <= bounds) {choice = 0}
  rt = timestep * ts_size
  if (rt >= rt_max) {choice = NA}
  
  return(list(choice=choice, rt=rt))
  
}