Open In App

How to Plot a Weibull Distribution in R

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss what is Weibull Distribution and what are the Properties of Weibull Distribution and how we implement the Weibull Distribution in R Programming Language.

Introduction to Weibull Distribution

The Weibull Distribution is a continuous probability distribution commonly used in reliability analysis, failure time analysis, and survival studies. It is highly versatile due to its ability to model various types of data through its shape and scale parameters. The distribution can represent various other distributions, such as the exponential and Rayleigh distributions, making it suitable for different applications.

Applications of Weibull Distribution

  • Reliability Analysis: Used to model the life of products, estimate failure rates, and predict maintenance schedules.
  • Survival Analysis: Helps model survival times in medical studies.
  • Wind Speed Analysis: Employed to model wind speeds for wind energy assessments.

Now we will discuss step by step implementation of Weibull Distribution in R Programming Language:

Step 1: Installing and Loading Required Packages

To work with the Weibull Distribution in R, you can use the built-in functions, or for more advanced analyses, use additional packages like fitdistrplus and ggplot2.

R
# Install necessary packages
install.packages("fitdistrplus")
install.packages("ggplot2")

# Load the packages
library(fitdistrplus)
library(ggplot2)

Step 2: Generating Random Weibull Data

You can generate random values from a Weibull distribution in R using the rweibull() function.

R
# Generate random data from a Weibull distribution
set.seed(123)
weibull_data <- rweibull(n = 1000, shape = 1.5, scale = 2)

# Display the first few values
head(weibull_data)

Output:

[1] 2.3161664 0.7678148 1.8561779 0.4984264 0.3112006 4.2418637

Step 3: Visualizing the Weibull Distribution

You can visualize the generated data using a histogram with a density curve.

R
# Plot histogram with density
ggplot(data.frame(weibull_data), aes(x = weibull_data)) +
  geom_histogram(aes(y = ..density..), bins = 30, color = "black", fill = "skyblue") +
  stat_function(fun = dweibull, args = list(shape = 1.5, scale = 2), color = "red", size = 1) +
  labs(title = "Weibull Distribution", x = "Values", y = "Density") +
  theme_minimal()

Output:

gh
Weibull Distribution in R

Step 4: Estimating Weibull Parameters from Data

To fit a Weibull distribution to data, you can use the fitdist() function from the fitdistrplus package.

R
# Fit a Weibull distribution to the generated data
fit <- fitdist(weibull_data, "weibull")

# Display the estimated parameters
print(fit)

Output:

Fitting of the distribution ' weibull ' by maximum likelihood 
Parameters:
      estimate Std. Error
shape 1.511618 0.03716227
scale 2.012952 0.04435071
  • The shape parameter (k): Indicates the failure rate behavior. If k>1,the failure rate increases over time (wear-out phase).
  • The scale parameter (λ): Represents the characteristic life. It’s the point at which 63.2% of items have failed.

Step 5: Visualizing the Fit

The fitdistrplus package offers convenient functions for visualizing how well the Weibull distribution in R fits the data.

R
# Plot the fitted distribution
plot(fit)

Output:

gh
Weibull Distribution in R

Calculating Weibull Probability Functions

You can calculate the PDF, CDF, and quantile values using the following R functions:

  • dweibull(x, shape, scale): PDF
  • pweibull(q, shape, scale): CDF
  • qweibull(p, shape, scale): Quantiles

Here is the example for Calculating Weibull Probability Functions:

R
# Calculate the PDF at x = 1.5
pdf_value <- dweibull(1.5, shape = 1.5, scale = 2)
print(pdf_value)

# Calculate the CDF at x = 1.5
cdf_value <- pweibull(1.5, shape = 1.5, scale = 2)
print(cdf_value)

# Find the quantile for probability 0.5
quantile_value <- qweibull(0.5, shape = 1.5, scale = 2)
print(quantile_value)

Output:

[1] 0.3392418

[1] 0.4777031

[1] 1.56644

Draw multiple Weibull distributions with different shapes

We can also draw multiple Weibull distributions in R with different shapes, scales and range by specifying different colors to each distribution using curve function. The curve function Draws a curve corresponding to a function over the interval [from, to]. curve can plot also an expression in the variable xname, default x.

R
curve(dweibull(x, shape=2, scale=1), 
	from=0, to=5, col='blue') 

curve(dweibull(x, shape=3, scale=2), 
	from=0, to=7, col='red', add=TRUE) 

curve(dweibull(x, shape=4, scale=3), 
	from=0, to=10, col='purple', add=TRUE) 

Output:

gh
Plot a Weibull Distribution in R

Conclusion

The Weibull Distribution in R is a powerful tool for modeling failure times, reliability, and survival data. Its flexibility makes it suitable for a wide range of applications in various fields, such as manufacturing, healthcare, and wind energy. In R, you can generate, visualize, and analyze Weibull distributions using built-in functions and additional packages like fitdistrplus and ggplot2.


Next Article
Article Tags :

Similar Reads