To plot the probability mass function for a Poisson distribution in R, we can use the following functions:
- dpois(x, lambda) to create the probability mass function
- plot(x, y, type = ‘h’) to plot the probability mass function, specifying the plot to be a histogram (type=’h’)
To plot the probability mass function, we simply need to specify lambda (e.g. the rate of occurrence of events) in the dpois() function.
For example, the following code illustrates how to plot a probability mass function for a Poisson distribution with lambda = 5:
#define range of "successes"
success #create plot of probability mass function
plot(success, dpois(success, lambda=5), type='h')
The x-axis shows the number of “successes” – e.g. the number of events that occurred – and the y-axis shows the probability of obtaining that number of successes in 20 trials.
We can add a title, change the axes labels, and increase the line width to make the plot more aesthetically pleasing:
success
We can use the following code to obtain the actual probabilities for each number of successes shown in the plot:
#prevent R from displaying numbers in scientific notation options(scipen=999) #define range of successes success #display probability of success for each number of trials dpois(success, lambda=5) [1] 0.0067379469991 0.0336897349954 0.0842243374886 0.1403738958143 [5] 0.1754673697679 0.1754673697679 0.1462228081399 0.1044448629571 [9] 0.0652780393482 0.0362655774156 0.0181327887078 0.0082421766854 [13] 0.0034342402856 0.0013208616483 0.0004717363030 0.0001572454343 [17] 0.0000491391982 0.0000144527054 0.0000040146404 0.0000010564843 [21] 0.0000002641211