How to Plot a Weibull Distribution in R
Last Updated :
01 Oct, 2024
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:
Weibull Distribution in RStep 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:
Weibull Distribution in RCalculating Weibull Probability Functions
You can calculate the PDF, CDF, and quantile values using the following R functions:
dweibull(x, shape, scale)
: PDFpweibull(q, shape, scale)
: CDFqweibull(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:
Plot a Weibull Distribution in RConclusion
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
.
Similar Reads
How to Add Vertical Lines to a Distribution Plot
Vertical lines in distribution plots help emphasize specific values or thresholds within the data distribution, aiding in visualizing critical points or comparisons. In this article, we will explore three different/approaches to add vertical lines to a distribution plot in Python. Understanding Dist
3 min read
How to Plot a Log Normal Distribution in R
In this article, we will explore how to plot a log-normal distribution in R, providing you with an understanding of its properties and the practical steps for visualizing it using R Programming Language.Log-Normal DistributionThe log-normal distribution is a probability distribution of a random vari
5 min read
Plot t Distribution in R
The t-distribution, also known as the Student's t-distribution is a type of probability distribution that is used to perform sampling of a normally distributed distribution where the sample size is small and the standard deviation of the input distribution is unknown. The distribution normally forms
4 min read
How to plot a normal distribution with Matplotlib in Python?
Normal distribution, also known as the Gaussian distribution, is a fundamental concept in probability theory and statistics. It is a symmetric, bell-shaped curve that describes how data values are distributed around the mean. The probability density function (PDF) of a normal distribution is given b
4 min read
How to Use the Triangular Distribution in R
Statistical distributions are crucial for analyzing data, and the triangular distribution is especially handy for handling uncertain values with clear minimum, maximum, and peak points. In R Programming Language we can easily generate random numbers following this distribution pattern. It helps in s
4 min read
How to plot user-defined functions in R?
Plotting user-defined functions in R is a common task for visualizing mathematical functions, statistical models, or custom data transformations. This article provides a comprehensive guide on how to plot user-defined functions in R, including creating simple plots, enhancing them with additional fe
3 min read
Plot Probability Distribution Function in R
The PDF is the acronym for Probability Distribution Function and CDF is the acronym for Cumulative Distribution Function. In general, there are many probability distribution functions in R in this article we are going to focus on the Normal Distribution among them to plot PDF. To calculate the Norma
3 min read
How to Calculate Sampling Distributions in R
A sampling distribution is a probability distribution of a statistic obtained from a larger number of samples drawn from a specific population. The sampling distribution of a given population is the distribution of frequencies of a range of different outcomes that could possibly occur for a statisti
3 min read
How to use Binomial Distribution in Excel?
The BINOM.DIST Function is ordered under Excel Statistical capabilities. It works out the binomial distribution likelihood for the number of triumphs from a predetermined number of preliminaries. This binomial distribution Excel guide will tell you the best way to utilize the capability, bit by bit.
2 min read
Plot Cumulative Distribution Function in R
In this article, we will discuss how to plot a cumulative distribution function (CDF) in the R Programming Language. The cumulative distribution function (CDF) of a random variable evaluated at x, is the probability that x will take a value less than or equal to x. To calculate the cumulative distri
4 min read