Declare a user defined distribution

Joseph Kahn picture Joseph Kahn · Oct 11, 2012 · Viewed 7.3k times · Source

How do you define your own distributions in R? If I have a distribution that looks something like this:

P(D=0)=2/4, P(D=1)=1/4, P(D=2)=1/4

How do I turn that into a distribution I can work with?

In the end, I want to be able to use these and do things involving cdfs, icdfs and pmfs. Like find the probability of 1 through a cdf type thing. And I also need to find out how to graph things. But I was going to ask in smaller steps and try to figure things out in between.

Answer

Josh O'Brien picture Josh O'Brien · Oct 12, 2012

If you just need to generate random variates from the distribution, this should suffice:

rMydist <- function(n) {
    sample(x = c(0,1,2), size = n, 
           prob = c(.5, .25, .25), replace=T)
}

rMydist(20)
# [1] 1 0 2 0 2 1 1 0 2 2 0 0 2 1 0 0 0 0 0 1

prop.table(table(rMydist(1e6)))
#        0        1        2 
# 0.500555 0.250044 0.249401 

For something more fancy, try out the distr package. In addition to random number generation, it'll get you the density, distribution, and quantile functions associated with your distribution:

library(distr)
## For more info, type: vignette("newDistributions")  

# Define full suite of functions (d*, p*, q*, r*) for your distribution
D <- DiscreteDistribution (supp = c(0, 1, 2) , prob = c(0.5, .25, .25))
dD <- d(D)  ## Density function
pD <- p(D)  ## Distribution function
qD <- q(D)  ## Quantile function
rD <- r(D)  ## Random number generation

# Take them for a spin
dD(-1:3)
# [1] 0.00 0.50 0.25 0.25 0.00
pD(-1:3)
# [1] 0.00 0.50 0.75 1.00 1.00
qD(seq(0,1,by=0.1))
# [1] 0 0 0 0 0 0 1 1 2 2 2
rD(20)
# [1] 0 0 2 2 1 0 0 1 0 1 0 2 0 0 0 0 1 2 1 0