1

I would like to declare a new probability distribution, I need a geometric distribution, I know that in R this type of distribution is already declared, but I need geometric distribution of the form p(1-p)^(k-1), so the mean is 1/p and k={1, 2, ...}. And then I would like to use all function of type rgeom and etc.

Thank you very much for any help.

Waney
  • 113
  • 3
  • Welcome to Stack Overflow! You may want to check out [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In particular, I would respond to your question with a few questions of my own: What have you tried? Where did you run into problems? – duckmayr Nov 17 '18 at 16:56

1 Answers1

1

The geometric distribution in R is defined as p(1-p)^(x) i.e. the number of trials not including the first success.

To get the distribution for all trials including the first success, you could simply adjust the formulas accordingly.

my_rgeom <- function(n, prob) rgeom(n, prob) + 1
my_dgeom <- function(x, prob, log = FALSE) {
  p <- dgeom(x, prob) / (1 - prob)
  if (isTRUE(log)) log(p) else p
}
erocoar
  • 5,723
  • 3
  • 23
  • 45