The Rescorla Wagner model for a singular CS

#clean the workspace
rm(list=ls())

#the rescorla wagner function
rescorla_wagner <- function(a, b, vMax, v0, trials, color, d){
  vOld = v0 #initiate v0
  vs = c() #intiate a list for all values of v
  dvs = c() #intiate a list for all values of delta v
  
  #for loop to do multiple trials
  for (i in 1:trials){
    
    #the formula
    deltav = a*b*(vMax - vOld) 
    vNew =  vOld + deltav
    
    vOld = vNew   #this value needs to be used for next trail
    dvs[i] = deltav   #store delta value
    vs[i] = vOld  #store v value
  }
  
  vs = c(v0, vs)   #add start value 
  xAxis = seq(0, trials, by = 1)  #create x axis which is equal to amount fo trails
  plot(xAxis, vs, xlab = "n trails", ylab = "associative value", ylim = range(v0,vMax))   #plot the results
  lines(xAxis,vs,col=color,lwd=2)   #add a line between the dots
  
  #a check if the user wants the line of absolute delta v values
  if (d == "yes"){
    lines(1:trials, abs(dvs), col = "blue", lwd =2) #add a line which represents the absolute value of the deltas
  }
  return(vNew)
}

The Rescorla Wagner model with single stimuli

The blue line indicates the the absolute delta values of each line. The other lines indicate instances of Rescorla Wagner.

v = rescorla_wagner(0.4,1,100,0,10, "green", "yes")
par(new=TRUE) #this will let both plots in one window
v = rescorla_wagner(0.2,1,100,0,10, "red", "yes")

#Extinction curve
#Here the start value is the max and the max is equal to 0
v = rescorla_wagner(0.4,1,0,100,10, "green", "yes")

#the rescorla wagner function but then with compound stimuli
rescorla_wagner2 <- function(a, b, vMax, v0, trials){
  
  vOldA = v0 #assign start value to calculate vB
  vOldB = 0 #assign start value to calculate deltaA and deltaB
  vOldAB = vOldA+ vOldB

  #initialize lists to store v values and delta values
  va = c()
  vb= c()
  vba = c()
  dva = c()
  dvb = c()

  #for loop to do multiple trials
  for (i in 1:trials){
    
    #the formula
    deltaA = a[1]*b*(vMax-vOldAB)
    deltaB = a[2]*b*(vMax-vOldAB)
    vA = vOldA + deltaA
    vB = vOldB + deltaB
    vAB = vA+vB
    
    #store values in lists
    va[i] = vA
    vb[i] = vB
    vba[i] = vAB
    dva[i] = deltaA
    dvb[i] = deltaB
    
    #assing new values to old for next iteration
    vOldA = vA
    vOldB = vB
    vOldAB = vAB
  }
  
  #add start value to lists
  va = c(v0, va)
  vb = c(0, vb)
  vba = c(v0, vba)
  #make x axis which is equal to amount of trials
  xAxis = seq(0, trials, by = 1)
  
  #plot the results
  plot(xAxis, vba, xlab = "n trails", ylab = "associative value", ylim = range(0,vMax))
  lines(xAxis,vba,col="red",lwd=2) #this, the red line, indicates vAB
  lines(xAxis,va,col="green",lwd=2) #this, the green line, indicates vA
  lines(xAxis,vb,col="black",lwd=2) #this, the black line, indicates vB
  
  return(list(vA, vB, vAB))
  
  
}

Rescorla Wagner model with compound stimuli

The red line indicates the compounds stimuli = stimuli A + stimuli B. The green line indicates stimuli A. The black line indicates stimuli B.

v = rescorla_wagner2(c(0.23, 0.17), 1, 100,0,10)

v = rescorla_wagner2(c(0.2, 0.4), 1, 100,0,10) #different learning rates than previous plot

BLOCKING

1)Geef voor een blocking experiment aan hoe de sequentie van US’en en CS’en
eruit ziet: - Eerst train je al op een cs met een bepaalde us tot het maximale - vervolgens voeg je een cs toe aan de training en ga je met beide tegelijke verder trainen - vervolgens haal je het cs weg waarmee getrained werd en test je met het tweede cs - het resultaat is dat er niet meer gereageerd wordt op het tweede cs omdat de max al was bereikt met het eerste cs.

cs1 = 0.4 #stimuli A's learning rate
cs2 = 0.3 #stimuli B's learning rate
va = rescorla_wagner(cs1,1,100,0,33,"green", "no") #train stimuli A to the max

vAB = rescorla_wagner2(c(cs1,cs2), 1, 100, va, 33) #train both stimulis with trained A

#round and cast to integer to avoid floating point error
va = as.integer(round(unlist(vAB[1])))
vb = as.integer(round(unlist(vAB[2])))
vab = as.integer(round(unlist(vAB[3])))

#train stimuli B 
vb = rescorla_wagner(cs2,1,vab-va,vb,33,"pink", "no")