Still new to R so trying to practice working with functions, I have created some (probably quite inefficient) code which I will look at modifying later, currently it returns the result I need but I want to repeat the simulation 100 times within the function itself. This would effectively be turn[i] which returns outcomes -1 or 1 with a specified function probability:
game = function(n,pr) {
turn = cumsum(2*rbinom(n,1,prob=pr)-1)
bankrupcy.test = which(turn == -25)
winner.test = which(turn == 50)
if(length(bankrupcy.test)==0){bankrupcy.test=c(0)}
if(length(winner.test)==0){winner.test=c(0)}
if(bankrupcy.test==0 && winner.test==0){turn[n]}else
if(bankrupcy.test[1]>winner.test[1]){-25}else{
50}
return(replicate(100,game(n)))
}
I have tried creating a for loop but I can't seem to structure that correctly, hence I am looking to use the replicate command within the function I created, however I recieve the following error:
"evaluation nested too deeply: infinite recursion / options(expressions=)? Error during wrapup: evaluation nested too deeply: infinite recursion /options(expressions=)"
Where am I going wrong? I want to return a vector with 100 outcomes of the above simulation, in which the game is played until 50 profit is made or a 25 loss is made, whichever occurs first. If neither of the previous outcomes occur then the final value of the vector is taken.