## Log-normal Distribution Probabilities using R

In this tutorial, you will learn about how to use `dlnorm()`

, `plnorm()`

, `qlnorm()`

and `rlnorm()`

functions in R programming language to compute the individual probabilities, cumulative probabilities, quantiles and to generate random sample for Log-normal distribution.

Before we discuss R functions for Log-normal distribution, let us see what is Log-normal distribution.

## Log-normal Distribution

Log-normal distribution distribution is a continuous type probability distribution. Log-normal distribution is commonly used in finance to analyze the stock prices.

The continuous random variable `$X$`

has a Log-Normal Distribution if the random variable `$Y=\ln (X)$`

has a normal distribution with mean `$\mu$`

and standard deviation `$\sigma$`

. The probability density function of `$X$`

is

` $$ \begin{align*} f(x;\mu,\sigma) &= \begin{cases} \frac{1}{\sqrt{2\pi}\sigma x}e^{-\frac{1}{2\sigma^2}(\ln x -\mu)^2}, & x\geq 0; \\ 0, & x < 0. \end{cases} \end{align*} $$ `

`$\mu$`

is location parameter`$\sigma$`

is scale parameter

where $e= 2.71828...$ and $\pi = 3.1425926...$.

In notation it can be written as $X\sim LN(\mu,\sigma^2)$.

If $X\sim LN(\mu,\sigma^2)$ then $\ln X \sim N(\mu, \sigma^2)$.

Read more about the theory and results of Log-normal distribution here.

## Log-normal probabilities using `dlnorm()`

function in R

For continuous probability distribution, density is the value of the probability density function at $x$ (i.e., $f(x)$).

The syntax to compute the probability density function for Log-normal distribution using R is

`dlnorm(x,meanlog=0, sdlog = 1)`

where

`x`

: the value(s) of the variable and,`meanlog`

: mean of the distribution on log scale,`sdlog`

: standard deviation of the distribution on log scale.

**Note:** If you do not specify the `meanlog`

and `sdlog`

, R will take the default value of `meanlog`

as 0 and `sdlog`

as 1.

The `dlnorm()`

function gives the density for given value(s) `x`

, `meanlog`

and `sdlog`

.

## Numerical Problem for Log-normal Distribution

To understand the four functions `dlnorm()`

, `plnorm()`

, `qlnorm()`

and `rlnorm()`

, let us take the following numerical problem.

### Log-normal Distribution Example

The life-time (in days) of certain electronic component that operates in a high-temperature environment is log-normally distributed with parameters $\mu=1.2$ and $\sigma=0.5$.

(a) Find the value of the density function at $x=3.5$.

(b) Plot the graph of Log-normal probability distribution.

(c) Find the probability that the component works till 4 days.

(d) Find the probability that the component works more than 5 days.

(e) Find the probability that the component works between 3 and 5 days.

(f) Plot the graph of cumulative Log-normal probabilities.

(g) What is the value of $c$, if $P(X\leq c) \geq 0.80$?

(h) Simulate 1000 Log-normal distributed random variables with $\mu= 1.2$ and $\sigma = 0.5$.

Let $X$ denote the life-time (in days) of certain electronic components that operates in a high-temperature environment. Given that $X\sim LN(1.2, 0.5^2)$. That is $\mu = 1.2$ and $\sigma = 0.5$.

Then $\ln(X)\sim N(1.2,0.25)$ distribution.

### Example 1: How to use ` dlnorm()`

function in R?

To find the value of the density function at $x=3.5$ we need to use ` dlnorm()`

function.

First let us define the given parameters as

```
# mean log of distribution
mu <- 1.2
# sd log of distribution
sigma <- 0.5
```

The probability density function of $X$ is

` $$ \begin{align*} f(x;\mu,\sigma) &= \begin{cases} \frac{1}{0.5 x\sqrt{2\pi} }e^{-\frac{1}{2}\big(\frac{\ln x -1.2}{0.5}\big)^2}, & x\geq 0; \\ 0, & x < 0. \end{cases} \end{align*} $$ `

For part (a), we need to find the density function at $x=3.5$. That is $f(3.5)$.

(a) The value of the density function at $x=3.5$ is

` $$ \begin{aligned} f(3.5)&=\frac{1}{0.5\times 3.5\times \sqrt{2\pi}}e^{-\frac{1}{2}\big(\frac{\ln(3.5)-1.2}{0.5}\big)^2}\\ &= 0.2267013 \end{aligned} $$ `

The above probability can be calculated using `dlnorm(3.5,meanlog=1.2,sdlog=0.5)`

function in R.

```
# Compute Log-normal probability
result1 <- dlnorm(3.5,meanlog=mu,sdlog=sigma)
result1
```

`[1] 0.2267013`

### Example 2 Visualize Log-normal probability distribution

Using `dlnorm()`

function we can compute Log-normal distribution probabilities for given `x`

, `meanlog`

and `sdlog`

. To plot the probability density function of Log-normal distribution, we need to create a sequence of `x`

values and compute the corresponding probabilities.

```
# create a sequence of x values
x <- seq(0,12, by=0.01)
## Compute the Log-normal pdf for each x
px<- dlnorm(x,meanlog=mu,sdlog=sigma)
```

(b) Visualizing Log-normal Distribution with `dlnorm()`

function and `plot()`

function in R:

The probability density function of Log-normal distribution with given $\mu=1.2$ and $\sigma=0.5$ can be visualized using `plot()`

function as follows:

```
## Plot the Log-normal probability dist
plot(x,px,type="l",xlim=c(0,12),ylim=c(0,max(px)),
lwd=3, col="blue4",ylab="f(x)",
main=expression(paste("PDF of Log-normal with ",
mu,"=1.2 and ",sigma,"=0.5")))
```

## Log-normal cumulative probability using `plnorm()`

function in R

The syntax to compute the cumulative probability distribution function (CDF) for Log-normal distribution using R is

`plnorm(q,meanlog=0, sdlog=1)`

where

`q`

: the value(s) of the variable,`meanlog`

: mean of the distribution on log scale,`sdlog`

: standard deviation of the distribution on log scale.

Using this function one can calculate the cumulative distribution function of Log-normal distribution for given value(s) of `q`

(value of the variable `x`

), `meanlog`

and `sdlog`

.

### Example 3: How to use `plnorm()`

function in R?

In the above example, for part (c), we need to find the probability $P(X\leq 4)$.

(c) The probability that the component works till 4 days is $P(X < 4)$.

The $Z$ score that corresponds to $4$ is

` $$ \begin{aligned} z&=\dfrac{\ln(X)-\mu}{\sigma}\\ &=\dfrac{\ln(4)-1.2}{0.5}\\ &\approx0.37 \end{aligned} $$ `

Thus the probability that the component works till 4 days is

` $$ \begin{aligned} P(X < 4) &=P(\ln(X) < \ln(4))\\ &=P(Z < 0.37)\\ &=0.6443088 \end{aligned} $$ `

```
## Compute cumulative Log-normal probability
result2 <- plnorm(4,meanlog=mu,sdlog=sigma)
result2
```

`[1] 0.6452727`

### Example 4: How to use `plnorm()`

function in R?

In the above example, for part (d), we need to find the probability $P(X \geq 5)$.

(d) The probability that the component works more than 5 days is $P(X > 5)$.

The $Z$ score that corresponds to $5$ is

` $$ \begin{aligned} z&=\dfrac{\ln(X)-\mu}{\sigma}\\ &=\dfrac{\ln(5)-1.2}{0.5}\\ &\approx0.82 \end{aligned} $$ `

The probability that the component works more than 5 days is

` $$ \begin{aligned} P(X > 5) &=1-P(X < 5)\\ &= 1-P(\ln X < \ln (5))\\ &= 1-P(Z < 0.82)\\ &=1-0.7938919\\ &=0.2061081 \end{aligned} $$ `

To calculate the probability that a random variable $X$ is greater than a given number, one can use the option `lower.tail=FALSE`

in `plnorm()`

function.

Above probability can be calculated easily using `plnorm()`

function with argument `lower.tail=FALSE`

as

$P(X \geq 5)$= `plnorm(5,meanlog=1.2,sdlog=0.5,lower.tail=FALSE)`

or by using complementary event as

$P(X \geq 5) = 1- P(X\leq 5)$= 1- `plnorm(5,meanlog=1.2,sdlog=0.5)`

```
# compute cumulative Log-normal probabilities
# with lower.tail False
plnorm(5,meanlog=mu,sdlog=sigma,lower.tail=FALSE)
```

`[1] 0.2064286`

```
# Using complementary event
1-plnorm(5,meanlog=mu,sdlog=sigma)
```

`[1] 0.2064286`

### Example 5: How to use `plnorm()`

function in R?

One can also use `plnorm()`

function to calculate the probability that the random variable $X$ is between two values.

(e) The probability that the component works between 3 and 5 days is $P(3 < X < 5)$.

The Z score that corresponds to $3$ and $5$ are respectively

` $$ \begin{aligned} z_1&=\dfrac{\ln(X)-\mu}{\sigma}\\ &=\dfrac{\ln(3)-1.2}{0.5}\\ &\approx-0.2 \end{aligned} $$ `

and

` $$ \begin{aligned} z_2&=\dfrac{\ln(X)-\mu}{\sigma}\\ &=\dfrac{\ln(5)-1.2}{0.5}\\ &\approx0.82 \end{aligned} $$ `

The probability that the component works between 3 and 5 days is

` $$ \begin{aligned} P(3 \leq X\leq 5) &=P(\ln (3) \leq \ln X\leq \ln(5))\\ &=P(-0.2\leq Z\leq 0.82)\\ &= P(Z < 0.82) -P( Z < -0.2)\\ &=0.7938919-0.4207403\\ &= 0.3731517 \end{aligned} $$ `

The above probability can be calculated using `plnorm()`

function as follows:

```
a <- plnorm(5,meanlog=mu,sdlog=sigma)
b <- plnorm(3,meanlog=mu,sdlog=sigma)
result3 <- a - b
result3
```

`[1] 0.3739161`

### Example 6: Visualize the cumulative Log-normal probability distribution

Using `plnorm()`

function we can compute Log-normal cumulative probabilities (CDF) for given `x`

, `meanlog`

and `sdlog`

. To plot the CDF of Log-normal distribution, we need to create a sequence of `x`

values and compute the corresponding cumulative probabilities.

```
# create a sequence of x values
x <- seq(0,12, by=0.01)
## Compute the Log-normal pdf for each x
Fx <- plnorm(x,meanlog=mu,sdlog=sigma)
```

(f) Visualizing Log-normal Distribution with `plnorm()`

function and `plot()`

function in R:

The cumulative probability distribution of Log-normal distribution with given `x`

, `meanlog`

and `sdlog`

can be visualized using `plot()`

function as follows:

```
## Plot the Log-normal probability dist
plot(x,Fx,type="l",xlim=c(0,12),ylim=c(0,1),
lwd=3, col="blue4",ylab="F(x)",
main=expression(paste("CDF of Log-normal with ",
mu,"=1.2 and ",sigma,"=0.5")))
```

## Log-normal Distribution Quantiles using `qlnorm()`

in R

The syntax to compute the quantiles of Log-normal distribution using R is

`qlnorm(p,meanlog=0,sdlog=1)`

where

`p`

: the value(s) of the probabilities,`meanlog`

: mean of the distribution on log scale,`sdlog`

: standard deviation of the distribution on log scale.

The function `qlnorm(p,meanlog,sdlog)`

gives $100*p^{th}$ quantile of Log-normal distribution for given value of `p`

, `meanlog`

and `sdlog`

.

The $p^{th}$ quantile is the smallest value of Log-normal random variable $X$ such that $P(X\leq x) \geq p$.

It is the inverse of `plnorm()`

function. That is, inverse cumulative probability distribution function for Log-normal distribution.

### Example 7: How to use `qlnorm()`

function in R?

In part (g), we need to find the value of $c$ such a that $P(X\leq c) \geq 0.80$. That is we need to find the $80^{th}$ quantile of given Log-normal distribution.

```
mu <- 1.2
sigma <- 0.5
prob <- 0.80
```

```
# compute the quantile for Log-normal dist
qlnorm(0.80,meanlog=mu, sdlog=sigma)
```

`[1] 5.057188`

The $80^{th}$ percentile of given Log-normal distribution is 5.0571881.

### Visualize the quantiles of Log-normal Distribution

The quantiles of Log-normal distribution with given `p`

, `meanlog=mu`

and `sdlog=sigma`

can be visualized using `plot()`

function as follows:

```
p <- seq(0,1,by=0.01)
qx <- qlnorm(p,meanlog=mu,sdlog=sigma)
# Plot the Quantiles of Log-normal dist
plot(p,qx,type="l",lwd=2,col="blue4",
ylab="quantiles",
main=expression(paste("Quantiles of Log-normal with ",
mu,"=1.2 and ",sigma,"=0.5")))
```

## Simulating Log-normal random variable using `rlnorm()`

function in R

The general R function to generate random numbers from Log-normal distribution is

`rlnorm(n,meanlog=0,sdlog=1)`

where,

`n`

: the sample observations,`meanlog`

: mean of the distribution on log scale,`sdlog`

: standard deviation of the distribution on log scale.

The function `rlnorm(n,meanlog,sdlog)`

generates `n`

random numbers from Log-normal distribution with given `meanlog`

and `sdlog`

.

### Example 8: How to use `rlnorm()`

function in R?

In part (h), we need to generate 1000 random numbers from Log-normal distribution with given $meanlog = 1.2$ and $sdlog=0.5$.

(h) We can use `rlnorm(1000,meanlog,sdlog)`

function to generate random numbers from Log-normal distribution.

```
## initialize sample size to generate
n <- 1000
# Simulate 1000 values From Log-normal dist
x_sim <- rlnorm(n,meanlog=mu,sdlog=sigma)
```

The below graphs shows the density of the simulated random variables from Log-normal Distribution.

```
## Plot the simulated data
plot(density(x_sim),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated data from Log-normal with ",
mu,"=1.2 and ",sigma,"=0.5")))
```

If you use same function again, R will generate another set of random numbers from $LN(1.2,0.5^2)$.

```
# Simulate 1000 values From Log-normal dist
x_sim_2 <- rlnorm(n,meanlog=mu,sdlog=sigma)
```

```
## Plot the simulated data
plot(density(x_sim_2),xlab="Simulated x",ylab="density",
lwd=5,col="blue",
main=expression(paste("Simulated data from Log-normal with ",
mu,"=1.2 and ",sigma,"=0.5")))
```

For the simulation purpose to reproduce same set of random numbers, one can use `set.seed()`

function.

```
# set seed for reproducibility
set.seed(1457)
# Simulate 1000 values From Log-normal dist
x_sim_3 <- rlnorm(n,meanlog=mu,sdlog=sigma)
```

```
## Plot the simulated data
plot(density(x_sim_3),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated data from Log-normal with ",
mu,"=1.2 and ",sigma,"=0.5")))
```

```
set.seed(1457)
# Simulate 1000 values From Log-normal dist
x_sim_4 <- rlnorm(n,meanlog=mu,sdlog=sigma)
```

```
## Plot the simulated data
plot(density(x_sim_4),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated data from Log-normal with ",
mu,"=1.2 and ",sigma,"=0.5")))
```

Since we have used `set.seed(1457)`

function, R will generate the same set of Log-normal distributed random numbers.

```
hist(x_sim_4,breaks = 30,col="blue4",
main=expression(paste("Histogram of Simulated data from Log-normal with ",
mu,"=1.2 and ",sigma,"=0.5")))
```

To learn more about other discrete and continuous probability distributions using R, go through the following tutorials:

**Discrete Distributions Using R**

Binomial distribution in R

Poisson distribution in R

Geometric distribution in R

Negative Binomial distribution in R

Hypergeometric distribution in R

**Continuous Distributions Using R**

Uniform distribution in R

Exponential distribution in R

Normal distribution in R

Beta distribution in R

Gamma distribution in R

Cauchy distribution in R

Laplace distribution in R

Logistic distribution in R

Weibull distribution in R

## Endnote

In this tutorial, you learned about how to compute the probabilities, cumulative probabilities and quantiles of Log-normal distribution in R programming. You also learned about how to simulate a Log-normal distribution using R programming.

To learn more about R code for discrete and continuous probability distributions, please refer to the following tutorials:

Probability Distributions using R

Let me know in the comments below, if you have any questions on Log-normal Distribution using R and your thought on this article.