Search code examples
rfor-loopprobability

How to for-loop over a custom function R?


I wrote this function which returns the probability that the value 1 does not appear in a random sample when iterated 1000 times for varying sample sizes.

bday.function <- function(sample.size){
  x <- vector()
  for (i in 1:1000){
  x[i] <- !any(data.frame(table(sample(1:365, sample.size, replace=TRUE)))$Var1 == 1)
}
  return(mean(x))
}

Now I want to use this function and another for-loop to calculate the probability for every sample size between 500 and 1500 and make a simple scatter plot of my results. Here is what I tried:

z <- vector()
for (i in 500:1500) {
  z[i] <- bday.function(i)
  return(plot(z))
}

Edit: when I run bday.function the output is number of TRUE values divided by the total (1000) TRUE/FALSE outcomes:

bday.function(750)
[1] 0.122

I would like to replicate this for sample sizes between 500 and 1500 to generate a simple scatter plot

Edit 2: Thanks to everybody for the help! Here's my final solution:

x <- vector(length = 1000)

for (i in 1:1000){
  x[i] <- !any(sample(1:365, 500, replace=TRUE) == 1) 
}

x
bday.function <- function(sample.size){
  x <- vector(length= 1000)
  for (i in 1:1000){
  x[i] <- !any(sample(1:365, sample.size, replace=TRUE) == 1) 
}
  return(mean(x))
}
bday.function(750)
z <- vector(length = 1000)
tmp.index <- 500:1500
for (i in seq_along(tmp.index)) {
  z[i] <- bday.function(tmp.index[i])
}
#Plot
plot(tmp.index, z, xlab = "sample size", ylab = "Probability of no birthdays")

enter image description here


Solution

  • As @JohnColeman pointed in his sage comment, your function can be slow. Try these changes on your code for the printing output. I have run only 60 sims as I need to complete other things:

    #Function
    bday.function <- function(sample.size){
      x <- vector()
      for (i in 1:1000){
        x[i] <- !any(data.frame(table(sample(1:365, sample.size, replace=TRUE)))$Var1 == 1)
      }
      return(mean(x))
    }
    #Loop
    z <- vector()
    vec <- 500:1500
    for (i in seq_along(vec)) {
      z[i] <- bday.function(vec[i])
    }
    #Plot
    plot(z)
    

    Output:

    enter image description here