R Tutorial R Charts & Graphs R Statistics R References

R - Normal Distribution



Exponential distribution is the probability distribution of the time between events in a Poisson point process, i.e., a process in which events occur continuously and independently at a constant average rate. It is a particular case of the gamma distribution. For example, customers arriving at a store, file requests on a server etc.

The probability density function (pdf) of exponential distribution is defined as:

Exponential Distribution

Where, λ is the rate parameter of the distribution.

An exponential distribution has mean 1/λ and variance 1/λ2.

The cumulative distribution function (cdf) evaluated at x, is the probability that the random variable (X) will take a value less than or equal to x. The cdf of exponential distribution is defined as:

Exponential Distribution

In R, there are four functions which can be used to generate exponential distribution.

Syntax

dexp(x, rate)
pexp(q, rate)
qexp(p, rate)
rexp(n, rate)

Parameters

x Required. Specify a vector of numbers.
q Required. Specify a vector of numbers.
p Required. Specify a vector of probabilities.
n Required. Specify number of observation (sample size).
rate Optional. Specify rate parameter of the sample data. Default is 1.

dexp()

The dexp() function measures probability density function (pdf) of the distribution.

#creating a sequence of values between
#0 to 10 with a difference of 0.1
x <- seq(0, 10, by=0.1)
   
y <- dexp(x, 1)
   
#naming the file
png(file = "exponential.png")

#plotting the graph
plot(x, y, col="blue")

#saving the file
dev.off()

The output of the above code will be:

Exponential Distribution

pexp()

The pexp() function returns cumulative distribution function (cdf) of the distribution.

#creating a sequence of values between
#0 to 10 with a difference of 0.1
x <- seq(0, 10, by=0.1)
   
y <- pexp(x, 1)
   
#naming the file
png(file = "exponential.png")

#plotting the graph
plot(x, y, col="blue")

#saving the file
dev.off()

The output of the above code will be:

Exponential Distribution

qexp()

The qexp() function takes the probability value and returns cumulative value corresponding to probability value of the distribution.

#creating a sequence of probability from
#0 to 1 with a difference of 0.01
x <- seq(0, 1, by=0.01)
   
y <- qexp(x, 2)
   
#naming the file
png(file = "exponential.png")

#plotting the graph
plot(x, y, col="blue")

#saving the file
dev.off()

The output of the above code will be:

Exponential Distribution

rexp()

The rexp() function generates a vector containing specified number of random values from the given exponential distribution. In the example below, a histogram is plotted to visualize the result.

#fixing the seed to maintain the
#reproducibility of the result
set.seed(10)
x <- 10000

#creating a vector containing 10000
#exponentially distributed random values  
y <- rexp(x, 2)
   
#naming the file
png(file = "exponential.png")

#plotting the graph
hist(y, col="blue")

#saving the file
dev.off()

The output of the above code will be:

Exponential Distribution