0

I want to code the birthday problem in R using the plot . I've wrote the code the problem is that I get an error with that .

Any idea is welcomed .Thank you very much .

Edit edit I've changed the code :

paradoxe_anniversaires <- function(n){
  N<-365
  produit = 1 ;

    for(i in 1:(n-1))
    produit<-produit*(1 -(i/N) )

 resultat <- (1 - produit)
 return (resultat)
}
graphe_anniv <- function(){
  x<- c(1:100)
  y<-paradoxe_anniversaires(x)
  plot(x,y)
}

but this time I have Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ In addition: Warning message: In 1:(n - 1) :

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

ana maria
  • 101
  • I'm voting to close this question as off-topic because it is about code implementation, rather than mathematics. –  Oct 10 '18 at 22:52
  • ok i see and where I should put it? in what forum because now I've changed the code thank you :) – ana maria Oct 10 '18 at 22:56

1 Answers1

0

I think the problem is with taking the factorial of 365. When I just try to run factorial(365) in R, it returns the same errors about the value out of range in 'gammafn'. I ran your code with a smaller value for N and it works just fine. It seems to work fine with values of N which are less than or equal to 170.

gd1035
  • 590
  • 1
  • 4
  • 11
  • thank you for your answer but I have to do it with N = 365 to test the paradox of birthdays I've edited the code but now I have a different error – ana maria Oct 10 '18 at 22:57
  • @anamaria After doing some reading, it looks like you can work around this by using the lgamma() function. This returns the log of the gamma function, and will return results for values greater than 170. So to get factorial(365) you could use lgamma(366) and this will return the log of factorial(365). – gd1035 Oct 10 '18 at 23:56
  • Thank you so much but I've tried to use the wikipedia formule and I don't know if you have seen that I've put a edit in my post with another error I have – ana maria Oct 11 '18 at 04:53