0
votes

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.

1
You are calling game from inside the function, and so game ends up calling itself. This is causing the recursion. - Oliver Frost
I figured that might be the case but how else can I replicate the simulation within the function? Is the only way a for loop? I can never seem to get those to work if the function gets even slightly complicated... - Aesler
is there a reason you want the replication within the function? if you remove the return(replicate(100,game(n))) and then outside the function run replicate(100, game(n = n, pr = pr)) it should work (but maybe I don't understand what you are trying to do) - Lucy
I want to add to the function further, so there will be additional factors which are reliant on the final vector being of 100 simulations (or more), so I have a sample of data to return information about. I will expand the function to several more variables (function(x,y,z....) and with only one result in the vector it isn't possible to do this as of yet. - Aesler
Can you give an example input? I.e. some values for n and pr when you call the function for the first time? - Oliver Frost

1 Answers

1
votes

Try this:

n <- 1000
pr <- 0.50

game <- function(n, pr) {
  result = 
    replicate(100, {{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 = 0}
      if(length(winner.test)==0){winner.test = 0}
      if(bankrupcy.test==0 && winner.test==0){turn[n]} else
        if(bankrupcy.test[1]>winner.test[1]) {-25} else {50}
      }})
  return(result)
}

game(n, pr)