Mary
Mary

Reputation: 27

new to R, receiving an error in my function

true_RA<-seq(-2,2 ,length.out = 13)  #true vector with -2<RA<2 in
# even intervals for 13 players 

true_RA

current_RA<-true_RA   #assign true_RA to the current ratings

ratings_overtime<-matrix(current_RA, nrow=13, ncol=10,000) #create a #matrix of 13 players
# and 10000 ratings 

p<-0.5825702

simulate_game<-function(i,j){
    old_RA_i<-current_RA[i] #assign the rate from 
#my current rating vector from vector 

    old_RA_j<-current_RA[j]
    win<rbinom(1,1,p)
if (win==1){current_RA_i<-old_RA_j+400}
else{current_RA_j<-old_RA_i+400}
}  
 return(c(current_RA_i,current_RA_j))
}

my function should do simulates a game between players i and j given their true underlying ratings from rbinom(1,1,p). I am getting an Error: object 'current_RA_i' not found.

Upvotes: 0

Views: 53

Answers (1)

Lyndon Gingerich
Lyndon Gingerich

Reputation: 655

You have an extra close curly bracket, so the function ends before the return statement.

Since the current_RA_i and current_RA_j variables are defined within the function, they are not global in scope, and thus have no value after the function ends.

Removing the } character just before your return statement should resolve the error.

Upvotes: 1

Related Questions