To calculate probabilities for continuous distributions, you need to use the cumulative distribution function, CDF. Unlike discrete distributions, there is no single point probability, P(X=x), there is a density. In this article, we will learn how to calculate probabilities of continuous distributions in R.
To calculate these probabilities, we can use function prefixed with p
for each distribution. For example, to calculate the cdf of the normal
distribution, we can use pnorm
.
pnorm(q = .8, mean = 0, sd = 1)
## [1] 0.7881446
This calculates the probability of selecting an item below .8 in the normal 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
qninom
.
qnorm(0.7881446, mean = 0, sd = 1)
## [1] 0.8
Notice that our result is .8 which is exactly the value of q
from the
previous section. This shows how the two functions are inversely
related.
It is common to generate a random sample of normal variables. These are
used in simulations and many other situations you will learn. To do
this, we can follow a similar pattern of the above function using the
norm
preceded by a r
for random.
rnorm(10)
## [1] 0.86408802 -0.50945900 0.84883333 0.37336024 1.43838537 1.11448386
## [7] 0.46681953 0.75352516 0.25414154 0.09360259
Here we generated sample of size 10 from the normal distributions.
The pdf isn’t very useful for continuous variables as you are usually
looking for the probability which is the cumulative density. However,
you can use the dnorm
and other functions to generate the density
plots.
x <- seq(-4, 4, length=100)
y <- dnorm(x)
plot(y, type='l')
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 |