Plinth
Plinth

Reputation: 289

Create a new probability distribution R

I would like to create a new probability density function in R as follows:

P{X=x} = p

P{X=/=x} follows a Poisson distribution with some parameter lambda but normalized s.t. the sum of all probabilities equals (1-p)

How could I make R recognize this as a new probability distribution and have it create all the corresponding functions (r, d, p, q)?

Upvotes: 2

Views: 2440

Answers (1)

IRTFM
IRTFM

Reputation: 263332

You are essentially creating a mixture class of a Dirac distribution and discrete Poisson distribution. The distr package does allow symbolic manipulations of distributions and it allows definitions of Dirac, various Discrete-classed functions including Pois, and mixtures of same with the UnivarMixingDistribution-class.

require(distr)
 x=5;lambda=4; p=0.3
 mylist <- UnivarMixingDistribution(Pois(4), Dirac(x) ,
           mixCoeff=c(p, 1-p))
 plot(mylist)
#NULL

You should look at the full package documentation since it provides [rdpq]* methods for distributions defined this way.

enter image description here

Here's how to use the p() and r() methods for that function/class definition:

> p(mylist)(4)
[1] 0.1886511
> p(mylist)(5)
[1] 0.9355392
> p(mylist)(0:10)
 [1] 0.005494692 0.027473460 0.071430997 0.130041046 0.188651095
 [6] 0.935539186 0.966797878 0.984659989 0.993591044 0.997560401
[11] 0.999148145
> r(mylist, 10)
Error in r(mylist, 10) : unused argument (10)
> r(mylist)( 10)
 [1] 5 2 5 5 5 5 5 3 5 5

You may also find useful information at ?family where a list of functions are defined for links and error structures for R models.

Upvotes: 4

Related Questions