Exponential Distribution in R (4 Examples) | dexp, pexp, qexp & rexp Functions

 

This tutorial explains how to apply the exponential functions in the R programming language.

The content of the article looks as follows:

Let’s get started…

 

Example 1: Exponential Density in R (dexp Function)

Let’s begin with the exponential density.

We can use the dexp R function return the corresponding values of the exponential density for an input vector of quantiles.

Let’s create such a vector of quantiles in RStudio:

x_dexp <- seq(0, 1, by = 0.02)                     # Specify x-values for exp function

Now, we can apply the dexp function with a rate of 5 as follows:

y_dexp <- dexp(x_dexp, rate = 5)                   # Apply exp function

We can use the plot function to create a graphic, which is showing the exponential density based on the previously specified input vector of quantiles:

plot(y_dexp)                                       # Plot dexp values

 

exponential density plot in r

Figure 1: Exponential Density in R.

 

Example 2: Exponential Cumulative Distribution Function (pexp Function)

We can also use the R programming language to return the corresponding values of the exponential cumulative distribution function for an input vector of quantiles. Again, let’s create such an input vector:

x_pexp <- seq(0, 1, by = 0.02)                     # Specify x-values for pexp function

In order to get the values of the exponential cumulative distribution function, we need to use the pexp function:

y_pexp <- pexp(x_pexp, rate = 5)                   # Apply pexp function

We can draw a plot of our previously extracted values as follows:

plot(y_pexp)                                       # Plot pexp values

 

exponential cdf plot in r

Figure 2: Exponential Cumulative Distribution Function.

 

Example 3: Exponential Quantile Function (qexp Function)

Similar to Examples 1 and 2, we can use the qexp function to return the corresponding values of the quantile function. This time, we need to specify a vector oft probabilities:

x_qexp <- seq(0, 1, by = 0.02)                     # Specify x-values for qexp function

The qexp command can then be used to get the quantile function values…

y_qexp <- qexp(x_qexp, rate = 5)                   # Apply qexp function

…and we can also draw a scatterplot containing these values:

plot(y_qexp)                                       # Plot qexp values

 

exponential quantile function plot in r

Figure 3: Exponential Quantile Function.

 

Example 4: Random Number Generation (rexp Function)

In R, we can also draw random values from the exponential distribution.

First, we need to specify a seed and the sample size we want to simulate:

set.seed(13579)                                    # Set seed for reproducibility
N <- 10000                                         # Specify sample size

Then, we can use the rexp function as follows:

y_rexp <- rexp(N, rate = 5)                        # Draw N exp distributed values
y_rexp                                             # Print values to RStudio console

We can create a histogram of our randomly sampled values as follows:

hist(y_rexp, breaks = 100, main = "")              # Plot of randomly drawn exp density

 

exponential random numbers as plot in r

Figure 4: Histogram of Random Numbers Drawn from Exponential Distribution.

 

Video & Further Resources

If you need further info on the examples of this article, you may want to have a look at the following video of the Statistics Globe YouTube channel. I’m explaining the R programming code of this tutorial in the video.

 

The YouTube video will be added soon.

 

You might also read the other tutorials on probability distributions and the generation of random numbers in R:

 

In addition, you may read some of the other articles of my homepage:

 

In this post, I explained how to use the exponential functions and how to simulate random numbers with exponential growth in R. In case you have any further comments or questions, please let me know in the comments.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top