Logistic Distribution Probabilities using R

Logistic Distribution probabilities using R

In this tutorial, you will learn about how to use dlogis(), plogis(), qlogis() and rlogis() functions in R programming language to compute the individual probabilities, cumulative probabilities, quantiles and to generate random sample for Logistic distribution.

Before we discuss R functions for Logistic distribution, let us see what is Logistic distribution.

Logistic Distribution

Logistic distribution distribution is a continuous type probability distribution.

Let $X\sim Logis(\mu,\lambda)$. Then the probability density function of $X$ is

 \begin{aligned} f(x)&= \begin{cases} \frac{1}{\lambda}\frac{e^{\frac{(x-\mu)}{\lambda}}}{\bigg(1+e^{\frac{(x-\mu)}{\lambda}}\bigg)^2} , & -\infty < x< \infty; \\ & -\infty < \mu< \infty, \lambda>0;\\ 0, & Otherwise. \end{cases} \end{aligned}

where $\mu$ is the location parameter and $\lambda$ is the scale parameter of Logistic distribution.

The distribution function of Logistic distribution is

 $$\begin{equation*} F(x) =1- \frac{1}{1+e^{\frac{(x-\mu)}{\lambda}}}. \end{equation*}$$

Logistic probabilities using dlogis() 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 Logistic distribution using R is

dlogis(x,location, scale)

where

• x : the value(s) of the variable and,
• location : location parameter of Logistic distribution,
• scale : scale parameter of Logistic distribution.

The dlogis() function gives the density for given value(s) x, location and scale.

Note: If you do not specify the location and scale, R assumes the default value location=0 and scale=1 (which is a standard Logistic distribution).

Numerical Problem for Logistic Distribution

To understand the four functions dlogis(), plogis(), qlogis() and rlogis(), let us take the following numerical problem.

Logistic Distribution Example

Let random variable $X$ follows a Logistic distribution with $\mu=1$ and $\lambda=2$.

(a) Find the value of the density function at $x=1.5$.
(b) Plot the graph of Logistic probability distribution.
(c) Find the probability that, $X$ is less than 2.
(d) Find the probability that, $X$ is greater than 3.
(e) Find the probability that, $X$ is less than 2.5 but greater than 4.5.
(f) Plot the graph of cumulative Logistic probabilities.
(g) What is the value of $c$, if $P(X\leq c) \geq 0.60$?
(h) Simulate 1000 Logistic distributed random variables with $\mu= 1$ and $\lambda = 2$.

Let $X$ denote the daily proportion of major automobile accidents across the United States. Given that $X\sim Logis(\mu=1, \lambda=2)$.

Example 1: How to use dlogis() function in R?

To find the value of the density function at $x=2.5$ we need to use dlogis() function.

Let $X\sim Logis(1,2)$.

First let us define the given parameters as

# parameter 1 location
mu <- 1
# parameter 2 scale
lambda <- 2

The probability density function of $X$ is

 \begin{aligned} f(x)&= \frac{1}{2}\frac{e^{\frac{(x-1)}{2}}}{\bigg(1+e^{\frac{(x-1)}{2}}\bigg)^2},\\ &\quad\text{for } -\infty \leq x \leq \infty. \end{aligned}

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

First I will show you how to calculate the value of the density function for given value of $x$. Then I will show you how to compute the same using dlogis() function in R.

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

 \begin{aligned} f(2.5)&=\frac{2}{\pi}\cdot \frac{1}{2^2+(2.5-1)^2}\\ &=\frac{2}{\pi}\cdot \frac{1}{2^2+(2.5-1)^2} &= 0.1089475 \end{aligned}

The above probability can be calculated using dlogis(2.5,1,2) function in R.

# Compute Logistic  probability
result1 <- dlogis(2.5,mu,lambda)
result1
[1] 0.1089475

Example 2 Visualize Logistic probability distribution

Using dlogis() function we can compute Logistic distribution probabilities for given x, location and scale. To plot the probability density function of Logistic distribution, we need to create a sequence of x values and compute the corresponding probabilities.

# create a sequence of x values
x <- seq(-10,14, by=0.02)
## Compute the Logistic pdf for each x
px<-dlogis(x,mu,lambda)

(b) Visualizing Logistic Distribution with dlogis() function and plot() function in R:

The probability density function of Logistic distribution with given 1 and 2 can be visualized using plot() function as follows:

## Plot the Logistic  probability dist
plot(x,px,type="l",xlim=c(-10,14),ylim=c(0,max(px)),
lwd=3, col="darkred",ylab="f(x)",
main=expression(paste("PDF of Logis(",
mu,"=1, ",lambda,"=2)")))
abline(v=c(1),lty=2,col="gray")

Logistic cumulative probability using plogis() function in R

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

plogis(q,location, scale)

where

• q : the value(s) of the variable,
• location : first parameter of Logistic distribution,
• scale : second parameter of Logistic distribution.

Using this function one can calculate the cumulative distribution function of Logistic distribution for given value(s) of q (value of the variable x), location and scale.

Example 3: How to use plogis() function in R?

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

(c) The probability that $X$ is less than $2$ is

 \begin{aligned} P(X \leq 2) &=F(2)\\ &=0.5+\frac{1}{\pi} tan^{-1}\big(\frac{2-1}{2}\big)\\ &=0.5 + \frac{1}{3.1416}tan^{-1}\big(0.5\big)\\ &=0.5 + \frac{1}{3.1416}(0.4636)\\ &= 0.6225 \end{aligned}

## Compute cumulative Logistic  probability
result2 <- plogis(2,mu,lambda)
result2
[1] 0.6224593

Example 4: How to use plogis() function in R?

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

(d) The probability that $X$ is greater than $3$ is

 \begin{aligned} P(X > 3) &=1- P(X < 3)\\ &= 1- F(3)\\ &=1-\bigg(0.5+\frac{1}{\pi} tan^{-1}\big(\frac{3-1}{2}\big)\bigg)\\ &=0.5 - \frac{1}{3.1416}tan^{-1}\big(1\big)\\ &=0.5 - \frac{1}{3.1416}(0.7854)\\ &= 0.2689 \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 plogis() function.

Above probability can be calculated easily using plogis() function with argument lower.tail=FALSE as

$P(X \geq 3)=$ plogis(3,mu,lambda,lower.tail=FALSE)

or by using complementary event as

$P(X \geq 3) = 1- P(X\leq 3)$= 1- plogis(3,mu,lambda)

# compute cumulative Logistic  probabilities
# with lower.tail False
plogis(3,mu,lambda,lower.tail=FALSE)
[1] 0.2689414
# Using complementary event
1-plogis(3,mu,lambda)
[1] 0.2689414

Example 5: How to use plogis() function in R?

One can also use plogis() function to calculate the probability that the random variable $X$ is between two values.

(e) The probability that $X$ is between $2.5$ and $4.5$ is

 \begin{aligned} P(2.5 \leq X \leq 4.5)&=P(X\leq 4.5)-P(X\leq 2.5)\\ &=F(4.5) -F(2.5)\\ &=\bigg[0.5+\frac{1}{\pi} tan^{-1}\big(\frac{4.5-1}{2}\big)\bigg]-\bigg[0.5+\frac{1}{\pi} tan^{-1}\big(\frac{2.5-1}{2}\big)\bigg]\\ &=\frac{1}{\pi} tan^{-1}\big(1.75\big)-\frac{1}{\pi} tan^{-1}\big(0.75\big)\\ &=\frac{1}{3.1416}(1.0517)-\frac{1}{3.1416}(0.6435)\\ &=0.1728 \end{aligned}

The above probability can be calculated using plogis() function as follows:

a <- plogis(4.5,mu,lambda)
b <- plogis(2.5,mu,lambda)
result3 <- a - b
result3
[1] 0.1727741

Example 6: Visualize the cumulative Logistic probability distribution

Using plogis() function we can compute Logistic cumulative probabilities (CDF) for given x, location and scale. To plot the CDF of Logistic distribution, we need to create a sequence of x values and compute the corresponding cumulative probabilities.

# create a sequence of x values
x <- seq(-10,14, by=0.02)
## Compute the Logistic pdf for each x
Fx <- plogis(x,mu,lambda)

(f) Visualizing Logistic Distribution with plogis() function and plot() function in R:

The cumulative probability distribution of Logistic distribution with given x, location and scale can be visualized using plot() function as follows:

## Plot the Logistic  probability dist
plot(x,Fx,type="l",xlim=c(-10,14),ylim=c(0,1),
lwd=3, col="darkred",ylab="F(x)",
main=expression(paste("Distribution function of
Logis(",mu,"=1, ",lambda,"=2)")))

Logistic Distribution Quantiles using qlogis() in R

The syntax to compute the quantiles of Logistic distribution using R is

qlogis(p,location,scale)

where

• p : the value(s) of the probabilities,
• location : first parameter of Logistic distribution,
• scale : second parameter of Logistic distribution.

The function qlogis(p,location,scale) gives $100*p^{th}$ quantile of Logistic distribution for given value of p, location and scale.

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

It is the inverse of plogis() function. That is, inverse cumulative probability distribution function for Logistic distribution.

Example 7: How to use qlogis() function in R?

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

mu <- 2
lambda <- 4
prob <- 0.60
# compute the quantile for Logistic  dist
qlogis(0.60,mu, lambda)
[1] 3.62186

The $60^{th}$ percentile of given Logistic distribution is 3.6218604.

Visualize the quantiles of Beta Distribution

The quantiles of Beta distribution with given p, location and scale can be visualized using plot() function as follows:

p <- seq(0,1,by=0.02)
qx <- qlogis(p,mu,lambda)
# Plot the quantiles of Logistic  dist
plot(p,qx,type="l",lwd=2,col="darkred",
ylab="quantiles",
main=expression(paste("Quantiles of Logis(",
mu,"=1, ",lambda,"=2)")))

Simulating Logistic random variable using rlogis() function in R

The general R function to generate random numbers from Logistic distribution is

rlogis(n,location,scale)

where,

• n : the sample observations,
• location : first parameter of Logistic distribution,
• scale : second parameter of Logistic distribution.

The function rlogis(n,location,scale) generates n random numbers from Logistic distribution with given location and scale.

Example 8: How to use rlogis() function in R?

In part (h), we need to generate 1000 random numbers from Logistic distribution with given $location = 2$ and $scale=4$.

(h) We can use rlogis(1000,mu,lambda) function to generate random numbers from Logistic distribution.

## initialize sample size to generate
n <- 1000
# Simulate 1000 values From Logistic  dist
x_sim <- rlogis(n,mu,lambda)

The below graphs shows the density of the simulated random variables from Logistic Distribution.

## Plot the simulated data
plot(density(x_sim),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated Data from Logis(",
mu,"=1, ",lambda,"=2)")))

If you use same function again, R will generate another set of random numbers from $Logis(2,4)$.

# Simulate 1000 values From Logistic  dist
x_sim_2 <- rlogis(n,mu,lambda)
## Plot the simulated data
plot(density(x_sim_2),xlab="Simulated x",ylab="density",
lwd=5,col="blue",
main=expression(paste("Simulated Data from Logis(",
mu,"=1, ",lambda,"=2)")))

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 Logistic  dist
x_sim_3 <- rlogis(n,mu,lambda)
## Plot the simulated data
plot(density(x_sim_3),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated Data from Logis(",
mu,"=1, ",lambda,"=2)")))
set.seed(1457)
# Simulate 1000 values From Logistic  dist
x_sim_4 <- rlogis(n,mu,lambda)
## Plot the simulated data
plot(density(x_sim_4),xlab="Simulated x",ylab="density",
lwd=5,col="darkred",
main=expression(paste("Simulated Data from Logis(",
mu,"=1, ",lambda,"=2)")))

Since we have used set.seed(1457) function, R will generate the same set of Logistic distributed random numbers.

hist(x_sim_4,breaks = 50,col="red4",
main=expression(paste("Histogram Logis(",
mu,"=1, ",lambda,"=2)")))

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

Discrete Distributions Using R

Continuous Distributions Using R

Endnote

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