Weibull Distribution probabilities using R

Weibull Distribution probabilities using R

In this tutorial, you will learn about how to use dweibull(), pweibull(), qweibull() and rweibull() functions in R programming language to compute the individual probabilities, cumulative probabilities, quantiles and to generate random sample for Weibull distribution.

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

Weibull Distribution

Weibull distribution distribution is a continuous type probability distribution. Weibull distribution has found applications in many fields. For example, Weibull distribution is suitable for describing waiting times between successive occurrences of a random events, survival times, system reliability, etc.

Let $X\sim W(\alpha,\beta)$. Then the probability density function of Weibull random variable $X$ is

$$ \begin{aligned} f(x;\alpha, \beta)&= \begin{cases} \frac{\alpha}{\beta} \big(\frac{x}{\beta}\big)^{\alpha-1}e^{-\big(\frac{x}{\beta}\big)^\alpha}, & x>0, \alpha, \beta>0; \\ 0, & Otherwise. \end{cases} \end{aligned} $$

where $\alpha$ is the shape parameter and $\beta$ is the scale parameter of Weibull distribution.

Read more about the theory and results of Weibull distribution here.

Weibull probabilities using dweibull() 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 Weibull distribution using R is

dweibull(x,shape,scale=1)

where

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

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

Numerical Problem for Weibull Distribution

To understand the four functions dweibull(), pweibull(), qweibull() and rweibull(), let us take the following numerical problem.

Weibull Distribution Example

The lifetime $X$ (in hundreds of hours) of a certain type of vacuum tube has a Weibull distribution with parameters $\alpha = 2$ and $\beta = 3$. Compute the following:

(a) Find the value of the density function at $x=3.5$.
(b) Plot the graph of Weibull probability distribution.
(c) Find the probability that the lifetime of vacuum tube is at most 6 unit of time.
(d) Find the probability that the lifetime of vacuum tube is at least 3 unit of time.
(e) Find the probability that the lifetime of equipment is less than 6 unit of time but greater than 1.8 unit of time.
(f) Plot the graph of cumulative Weibull probabilities.
(g) What is the value of $c$, if $P(X\leq c) \geq 40$?
(h) Simulate 1000 Weibull distributed random variables with $\alpha= 2$ and $\beta = 3$.

Let $X$ denote the lifetime of vacuum tube (in hundreds of hours). Given that $X\sim Weibull(\alpha=2, \beta=3)$.

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

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

Let $X$ denote the lifetime of certain equipment. Here $X\sim Weibull(2,3)$.

First let us define the given parameters as

# shape parameter 
alpha <- 2
# scale parameter
beta <- 3

The probability density function of Weibull random variable $X$ is

$$ \begin{aligned} f(x)&=\frac{2}{3} \big(\dfrac{x}{3}\big)^{1}e^{-\big(\dfrac{x}{3}\big)^2};\; x>0. \end{aligned} $$

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{2}{3} \big(\dfrac{3.5}{3}\big)^{1}e^{-\big(\dfrac{3.5}{3}\big)^2}\\ &= 0.1994034 \end{aligned} $$

The above probability can be calculated using dweibull(0.35,shape=2,scale=3) function in R.

# Compute Weibull probability
result1 <- dweibull(3.5,shape=alpha,scale=beta)
result1
[1] 0.1994034

Example 2 Visualize Weibull probability distribution

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

# create a sequence of x values
x <- seq(0,10, by=0.02)
## Compute the Weibull pdf for each x
px<- dweibull(x,shape=alpha,scale=beta)

(b) Visualizing Weibull Distribution with dweibull() function and plot() function in R:

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

## Plot the Weibull probability dist
plot(x,px,type="l",xlim=c(0,10),ylim=c(0,max(px)),
     lwd=3, col="darkred",ylab="f(x)")
title("PDF of Weibull (alpha = 2, beta= 3)")
PDF Weibull Dist
PDF Weibull Dist

Weibull cumulative probability using pweibull() function in R

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

pweibull(q,shape,scale=1)

where

  • q : the value(s) of the variable,
  • shape : shape parameter of Weibull distribution,
  • scale : scale parameter of Weibull distribution.

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

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

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

(c) The probability that the lifetime of vaccum tube is at most 6 unit of time is

$$ \begin{aligned} P(X\leq 6) &=\int_0^{6} f(x)\; dx. \end{aligned} $$

## Compute cumulative Weibull probability
result2 <- pweibull(6,shape=alpha,scale=beta)
result2
[1] 0.9816844

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

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

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

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

$P(X \geq 3) =\int_{3}^\infty f(x)\; dx$= pweibull(3,shape=alpha,scale=beta,lower.tail=FALSE)

or by using complementary event as

$P(X \geq 3) = 1- P(X\leq 3)$= 1- pweibull(3,shape=alpha,scale=beta)

# compute cumulative Weibull probabilities
# with lower.tail False
pweibull(3,shape=alpha,scale=beta,lower.tail=FALSE)
[1] 0.3678794

(d) The probability that the lifetime of vacuum tube is at least 3 unit of time is

$$ \begin{aligned} P(X\geq 3) &=\int_{3}^\infty f(x)\; dx\\ &=0.3678794. \end{aligned} $$

# Using complementary event
1-pweibull(3,shape=alpha,scale=beta)
[1] 0.3678794

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

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

(e) The probability that the lifetime of equipment is less than 6 unit of time but greater than 1.8 unit of time can be written as $P(1.8 < X < 6)$.

$$ \begin{aligned} P(1.8 < X < 6) &= P(X< 6) -P(X < 1.8)\\ &= 0.9816844 - 0.3023237\\ &= 0.6793607 \end{aligned} $$

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

a <- pweibull(6,shape=alpha,scale=beta)
b <- pweibull(1.8,shape=alpha,scale=beta)
result3 <- a - b
result3
[1] 0.6793607

Example 6: Visualize the cumulative Weibull probability distribution

Using pweibull() function we can compute Weibull cumulative probabilities (CDF) for given x, shape1 and shape2. To plot the CDF of Weibull 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,10, by=0.02)
## Compute the Weibull pdf for each x
Fx <- pweibull(x,shape=alpha,scale=beta)

(f) Visualizing Weibull Distribution with pweibull() function and plot() function in R:

The cumulative probability distribution of Weibull distribution with given x, shape1 and shape2 can be visualized using plot() function as follows:

## Plot the Weibull  probability dist
plot(x,Fx,type="l",xlim=c(0,10),ylim=c(0,1),
     lwd=3, col="darkred",ylab="F(x)")
title("CDF of Weibull (alpha = 2, beta= 3)")
CDF Weibull Dist
CDF Weibull Dist

Weibull Distribution Quantiles using qweibull() in R

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

qweibull(p,shape,scale=1)

where

  • p : the value(s) of the probabilities,
  • shape : shape parameter of Weibull distribution,
  • scale : scale parameter of Weibull distribution.

The function qweibull(p,shape,scale) gives $100*p^{th}$ quantile of Weibull distribution for given value of p, shape and scale.

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

It is the inverse of pweibull() function. That is, inverse cumulative probability distribution function for Weibull distribution.

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

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

alpha <- 2
beta <- 3
prob <- 0.40
# compute the quantile for Weibull  dist
qweibull(0.40,shape=alpha, scale=beta)
[1] 2.144162

The $40^{th}$ percentile of given Weibull distribution is 2.144162.

Visualize the quantiles of Weibull Distribution

The quantiles of Weibull distribution with given p, shape=alpha and scale=beta can be visualized using plot() function as follows:

p <- seq(0,1,by=0.02)
qx <- qweibull(p,shape=alpha,scale=beta)
# Plot the Quantiles of Weibull  dist
plot(p,qx,type="l",lwd=2,col="darkred",
     ylab="quantiles",
main="Quantiles of Weibull(alpha= 2,beta = 3)")
Quantile Weibull Dist
Quantile Weibull Dist

Simulating Weibull random variable using rweibull() function in R

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

rweibull(n,shape,scale=1)

where,

  • n : the sample observations,
  • shape : shape parameter of Weibull distribution,
  • scale : scale parameter of Weibull distribution.

The function rweibull(n,shape,scale) generates n random numbers from Weibull distribution with given shape and scale.

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

In part (h), we need to generate 1000 random numbers from Weibull distribution with given $shape = 2$ and $scale=3$.

(h) We can use rweibull(1000,shape,scale) function to generate random numbers from Weibull distribution.

## initialize sample size to generate
n <- 1000
# Simulate 1000 values From Weibull  dist
x_sim <- rweibull(n,shape=alpha,scale=beta)

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

## Plot the simulated data
plot(density(x_sim),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Weibull(2,3) dist")
Random Sample Weibull Dist
Random Sample Weibull Dist

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

# Simulate 1000 values From Weibull  dist
x_sim_2 <- rweibull(n,shape=alpha,scale=beta)
## Plot the simulated data
plot(density(x_sim_2),xlab="Simulated x",ylab="density",
     lwd=5,col="blue",
     main="Simulated data from Weibull(2,3) dist")
Random Sample Weibull Dist 2
Random Sample Weibull Dist 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 Weibull  dist
x_sim_3 <- rweibull(n,shape=alpha,scale=beta)
## Plot the simulated data
plot(density(x_sim_3),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Weibull(2,3) dist")
Random Sample Weibull Dist 3
Random Sample Weibull Dist 3
set.seed(1457)
# Simulate 1000 values From Weibull  dist
x_sim_4 <- rweibull(n,shape=alpha,scale=beta)
## Plot the simulated data
plot(density(x_sim_4),xlab="Simulated x",ylab="density",
     lwd=5,col="darkred",
     main="Simulated data from Weibull(2,3) dist")
Random Sample Weibull Dist 3
Random Sample Weibull Dist 3

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

hist(x_sim_4,breaks = 30)
Histogram Weibull Sample
Histogram Weibull Sample

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
Log-Normal 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

Endnote

In this tutorial, you learned about how to compute the probabilities, cumulative probabilities and quantiles of Weibull distribution in R programming. You also learned about how to simulate a Weibull 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 Weibull Distribution using R and your thought on this article.

VRCBuzz co-founder and passionate about making every day the greatest day of life. Raju is nerd at heart with a background in Statistics. Raju looks after overseeing day to day operations as well as focusing on strategic planning and growth of VRCBuzz products and services. Raju has more than 25 years of experience in Teaching fields. He gain energy by helping people to reach their goal and motivate to align to their passion. Raju holds a Ph.D. degree in Statistics. Raju loves to spend his leisure time on reading and implementing AI and machine learning concepts using statistical models.

Leave a Comment