Created
December 21, 2021 03:31
-
-
Save jvcasillas/628232a0c759e97b9911ab3eeff26241 to your computer and use it in GitHub Desktop.
ddm_sims
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# DDM simulation function | |
sim_ddm <- function(drift_rate, boundary_separation, bias, ndt, n_sims, seed = NULL) { | |
set.seed(seed) | |
sim_df = NULL | |
for (sim in 1:n_sims){ | |
step = 1 | |
value = bias | |
n = 1 | |
while (abs(value[n]) < boundary_separation) { | |
n = n + 1 | |
value[n] = (value[n - 1] + rnorm(1, 0, abs(drift_rate))) | |
step[n] = n | |
} | |
dd_df <- tibble(sim_n = sim, step, value) %>% | |
mutate( | |
sim_n = as.factor(sim_n), | |
value = case_when( | |
value > boundary_separation ~ boundary_separation, | |
value < -boundary_separation ~ -boundary_separation, | |
TRUE ~ .$value) | |
) | |
sim_df = rbind(sim_df, dd_df) | |
} | |
return(sim_df) | |
} | |
# Get α, β, δ, and τ from DDM | |
# Plot 20 random simulations after fitting model | |
sim_ddm( | |
drift_rate = 0.8, | |
boundary_separation = 1, | |
bias = 0, | |
ndt = 0.2, | |
n_sims = 20, | |
seed = 12345) %>% | |
ggplot(., aes(x = step, y = value, color = sim_n)) + | |
geom_line(show.legend = F) | |
# You can increase the number of sims and plot the average trajectories too |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah the plot you are referring to is just a schematic to explain the model (it's in the method section of the paper Im working on). I use it when I explain what each parameter represents. Here is the code:
This is how Im using it: