0

I have the mtcars dataset What I want is to make a simulation using a Poisson distribution The basic idea is to simulate hp using rpois() and compare it to the rest of the cars to see how many times one car has a lower hp than the other. That way I can have the probability that a car has a lower hp than the rest of the cars. I am using this example for simplicity in reality I am using golf players and base on their average score I want to know what is the probability to win the tournament

I can test the probability of one car has less hp than another by using their score

n1 = 93
n2 = 100
sum(rpois(1000, n1) < rpois(1000, n2))/1000

In this case, I am testing Mazda RX4 and Datsun 710 I would like a way to find this probability but using the whole sample, in other words, how many times Mazda RX4 has less hp from the Poisson simulation than the rest of the cars in the sample

Pastor Soto
  • 336
  • 2
  • 14
  • 1
    it doesn't seem like the poisson distribution is appropriate for this case. This answer suggest you should look into bayesian inference/networks which makes more sense: https://stackoverflow.com/questions/26769518/algorithm-to-calculate-the-odds-of-a-team-winning-a-sports-match-given-full-hist – Eric Mar 10 '21 at 16:52

1 Answers1

1

Here is a way of comparing all cars with all cars using outer and a vectorized comparison function, g.

f <- function(n1, n2){
  mean(rpois(1000, n1) < rpois(1000, n2))
}
g <- Vectorize(f, c("n1", "n2"))
res <- outer(mtcars$hp, mtcars$hp, g)
dimnames(res) <- list(row.names(mtcars), row.names(mtcars))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • The third column (datsun710) with the first row (Mazda Rx4) gives me the probability of Datsun has lower hp than Mazda rx4? – Pastor Soto Mar 10 '21 at 16:28
  • @PastorSoto Yes, that's it. – Rui Barradas Mar 10 '21 at 18:58
  • I was checking and it seems that is the other way around. The probability that one car has a lower mpg is in the rows compared to the columns. In the example, I think it should be the probability that Mazda RX4 has lower hp than Datsun 710. It is possible for you to check and let me know. Also, Using this code can I find the probability that one car has the lowest hp than the rest of the cars? – Pastor Soto Mar 10 '21 at 19:19