Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare a user defined distribution

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.

like image 367
Joseph Kahn Avatar asked Oct 11 '12 21:10

Joseph Kahn


People also ask

What is custom distribution?

For example, a custom distribution can be especially helpful if different ranges of values have specific probabilities. You can create a distribution of one shape for one range of values and a different distribution for another range. You can describe a series of single values, discrete ranges, or continuous ranges.


1 Answers

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
like image 195
Josh O'Brien Avatar answered Nov 13 '22 08:11

Josh O'Brien