Two common probabilities you will calculate in statistics is P(X = x) and P(X ≤ x). R provides your some simple ways to calculate these probabilities. In this article, we will learn how to calculate probabilities for Discrete Distributions in R.
To calculate P(X = x), we can use the density functions. R has
many functions for this all prefixed with d
. For example, we can use
dbinom
to calculate the binomial distribution.
dbinom(7, size = 100, prob = 0.5)
## [1] 1.262774e-20
The above shows us the P(X = 7) when X is from a Bin(100, .5) distribution.
To calculate the probability that an item is less than x or
P(X ≤ x), we can use the probability mass function, or the CDF. R
has many methods all prefixed with p
. For example, we can use pbiom
for the binomial distribution.
pbinom(7, size = 10, prob = 0.5)
## [1] 0.9453125
The above shows us the P(X ≤ 7) when X is from a Bin(10, .5) distribution.
Next, we can take a look at getting the quantile from a distribution.
This is also known as the inverse CDF. To do this, we can use the
distribution functions prefixed with a q
. In our case, we will use
qbinom
.
qbinom(0.9453125, size = 10, prob = 0.5)
## [1] 7
In the above example, we put in the P(X ≤ 7) which we got from our previous example. We can see the value returned is 7 which is the value of X we used in our pdf.
We can also generate random samples from any of the distributions.
Again, we follow the same pattern of having a short name for the
distribution, i.e. binom
, preceded by a prefix, r
. To generate
random samples, we can use rbinom
.
rbinom(10, size = 10, prob = .5)
## [1] 2 3 8 6 9 6 4 6 4 7
The example above will generate a random sample, which is multiple values of X from a Bin(10, .5) distribution. In our case, we supplied 10 as the first argument, thus our sample size will be 10.
R provides many other continuous distributions with the same structure as above. Here is a list of many distributions and their respective functions.
Distribution | Functions | |||
---|---|---|---|---|
Beta | pbeta | qbeta | dbeta | rbeta |
Binomial | pbinom | qbinom | dbinom | rbinom |
Cauchy | pcauchy | qcauchy | dcauchy | rcauchy |
Chi-Square | pchisq | qchisq | dchisq | rchisq |
Exponential | pexp | qexp | dexp | rexp |
F | pf | qf | df | rf |
Gamma | pgamma | qgamma | dgamma | rgamma |
Geometric | pgeom | qgeom | dgeom | rgeom |
Hypergeometric | phyper | qhyper | dhyper | rhyper |
Logistic | plogis | qlogis | dlogis | rlogis |
Log Normal | plnorm | qlnorm | dlnorm | rlnorm |
Negative Binomial | pnbinom | qnbinom | dnbinom | rnbinom |
Normal | pnorm | qnorm | dnorm | rnorm |
Poisson | ppois | qpois | dpois | rpois |
Student t | pt | qt | dt | rt |
Studentized Range | ptukey | qtukey | dtukey | rtukey |
Uniform | punif | qunif | dunif | runif |
Weibull | pweibull | qweibull | dweibull | rweibull |
Wilcoxon Rank Sum Statistic | pwilcox | qwilcox | dwilcox | rwilcox |
Wilcoxon Signed Rank Statistic | psignrank | qsignrank | dsignrank | rsignrank |