lundi 18 mars 2019

Using for-loops and if-else statements coding in R to solve problems - getting error message

I am new to R and have been given some homework. I have to create code in R with the following properties:

1) IF CR(which is a supposedly a computer robot trying to learn outcomes) accepts the cue’s outcomes, compute the prediction error upon receipt of reward, and update the predicted value based on this prediction error, using a learning rate of .2

2) ELSE, if CR does NOT accept the outcome, do not give CR any feedback. In this case, CR’s predicted value on the next trial will be set to be the same as the predicted value on the current trial (i.e., no change in its 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
 CRAccepts = vector(mode = 'logical', length = 200)

I came up with following code below to try and satisfy the properties listed above:

for(trial in 1:200){CRAccepts[trial] = PredictedValue[trial] > 0
if(CRAccepts > 0){PredictionError[trial] = o1$Outcome[trial] - PredictedValue[trial]}
else {PredictedValue[trial + 1] = PredictedValue[trial] + .2*PredictionError[trial]}}

When I compute the code, I get an error message with 50 warnings. Where did I go wrong?

Aucun commentaire:

Enregistrer un commentaire