16.6 C
London
Thursday, July 4, 2024
HomeRProbability Distributions in RHow to Plot a Binomial Distribution in R

How to Plot a Binomial Distribution in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

To plot the probability mass function for a binomial distribution in R, we can use the following functions:

  • dbinom(x, size, prob) 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 size (e.g. number of trials) and prob (e.g. probability of success on a given trial) in the dbinom() function.

For example, the following code illustrates how to plot a probability mass function for a binomial distribution with size = 20 and prob = 0.3:

success 

Plot of Binomial distribution probability mass function in R

The x-axis shows the number of successes 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 

Binomial distribution probably mass function plot in R

You 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
dbinom(success, size=20, prob=.3)

[1] 0.00079792266297612 0.00683933711122388 0.02784587252426865
[4] 0.07160367220526231 0.13042097437387065 0.17886305056987975
[7] 0.19163898275344257 0.16426198521723651 0.11439673970486122
[10] 0.06536956554563482 0.03081708090008504 0.01200665489613703
[13] 0.00385928193090119 0.00101783259716075 0.00021810698510587
[16] 0.00003738976887529 0.00000500755833151 0.00000050496386536
[19] 0.00000003606884753 0.00000000162716605 0.00000000003486784

Additional Resources

An Introduction to the Binomial Distribution
Understanding the Shape of a Binomial Distribution

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories