I am new to R and have been given some homework. I have to create code in R with the following properties:
1) IF Al accepts the cue’s outcomes, compute his prediction error upon receipt of reward, and update his predicted value based on this prediction error, using a learning rate of .2
2) OTHERWISE, if Al does NOT accept the outcome, do not give Al any feedback. In this case, Al’s predicted value on the next trial will be set to be the same as his predicted value on the current trial (i.e., no change in his prediction). Make sure you SET the predicted value on the next trial, but make sure that it is the same as on the current trial.
I have been given the following to work with:
o1 is saved as the data set
PredictedValue = vector(mode = 'numeric', length = 200)
PredictionError = vector(mode = 'numeric', length = 200)
PredictedValue[1] = 0
AlAccepts = vector(mode = 'logical', length = 200)
I came up with following code below to try and satisfy the properties listed above. Did I do it correctly?
for(trial in 1:200){AlAccepts[trial] = PredictedValue[trial] > 0
if(AlAccepts > 0){PredictionError[trial] = o1$Outcome[trial] - PredictedValue[trial]}
else {PredictedValue[trial + 1] = PredictedValue[trial] + .2*PredictionError[trial]}}
From computing the code written above, I would then have to calculate Al's average acceptance rate and the average accuracy. Which I did using the code below.
mean(AlAccepts)
mean(AlAccepts == (o1$Outcome >= 0))
Can someone check over the code written above if it is written properly to accommodate the properties?
Aucun commentaire:
Enregistrer un commentaire