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
for each distribution. For example, to calculate the cdf of the normal
distribution, we can use
pnorm(q = .8, mean = 0, sd = 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
qnorm(0.7881446, mean = 0, sd = 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
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.
##  0.86408802 -0.50945900 0.84883333 0.37336024 1.43838537 1.11448386 ##  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
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.
|Wilcoxon Rank Sum Statistic||pwilcox||qwilcox||dwilcox||rwilcox|
|Wilcoxon Signed Rank Statistic||psignrank||qsignrank||dsignrank||rsignrank|