#Set a seed
set.seed(2024)


#Variable declaration-------------
nConds <- 2     #Number of conditions
stim <- c("R", "L")       #Stimulus labels
pi <- c(.75, .65)         #Probability of step (+1) for random walk; 1-pi will be the probability of stepping -1
diff <- c("Easy", "Hard")  #Difficulty label for the condition
mean_a <- c(40, 40)       #Mean upper bound for each condition
a_s <- c(0, 0)            #Variability in upper bound for each condition
zp=c(.5, .5)              #Multiplier of a for starting point for condition (Z = a*zp)
zp_s=c(0, 0)              #Variability for starting point multiplier
Nsubs=1                   #Number of subjects
Trials_per_cond=100       #Number of trials per condition
timeout=500               #Timeout parameter for the random walk


run_walk <- function(mytrial, mycond) {
    condpi=pi[mycond]
    cond_a=mean_a[mycond]
    cond_a_s=a_s[mycond]
    cond_zp=zp[mycond]
    cond_zp_s=zp_s[mycond]
    cond_stim=stim[mycond]
    cond_diff=diff[mycond]
    
    trial_a=rnorm(1, mean=cond_a, sd=cond_a_s)
    trial_Z=rnorm(1, mean=trial_a*cond_zp, sd=cond_zp_s)
    trial_Pi=rnorm(1, mean=condpi, sd=0)
    
    #Begin setup for random walk
    time=0
    curpointer=trial_Z
  
    #Conduct random walk
    while (time < timeout){
       time=time+1
       curpointer=curpointer+sample(c(-1, 1), size=1, prob = c(1-trial_Pi, trial_Pi))

       if (curpointer >= trial_a) { 
           myresponse="1"
           break
       }
       else if (curpointer <= 0){
           myresponse="0"
           break
       }
    }
  
    return(
        c(RESPONSE=myresponse, RT=abs(time), STIMULIS=cond_stim)
    )
}

mydata2 <- data.frame(
    Sub = rep(1:1, each=100), Trial = 1:100, Condition = rep(1:2, 50)
)


mydata2[,c("RESPONSE", "RT", "STIMULIS")] <- with(
    mydata2,
    t(mapply(run_walk, Trial, Condition))
) 

mydata2 <- transform(
    mydata2,
    RESPONSE = as.integer(RESPONSE),
    RT = as.integer(RT)
)

str(mydata2)

mydata2

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: