Statistics with R
- Statistics with R
- R Objects, Numbers, Attributes, Vectors, Coercion
- Matrices, Lists, Factors
- Data Frames in R
- Control Structures in R
- Functions in R
- Data Basics: Compute Summary Statistics in R
- Central Tendency and Spread in R Programming
- Data Basics: Plotting – Charts and Graphs
- Normal Distribution in R
- Skewness of statistical data
- Bernoulli Distribution in R
- Binomial Distribution in R Programming
- Compute Randomly Drawn Negative Binomial Density in R Programming
- Poisson Functions in R Programming
- How to Use the Multinomial Distribution in R
- Beta Distribution in R
- Chi-Square Distribution in R
- Exponential Distribution in R Programming
- Log Normal Distribution in R
- Continuous Uniform Distribution in R
- Understanding the t-distribution in R
- Gamma Distribution in R Programming
- How to Calculate Conditional Probability in R?
- How to Plot a Weibull Distribution in R
- Hypothesis Testing in R Programming
- T-Test in R Programming
- Type I Error in R
- Type II Error in R
- Confidence Intervals in R
- Covariance and Correlation in R
- Covariance Matrix in R
- Pearson Correlation in R
- Normal Probability Plot in R
How to Plot a Weibull Distribution in R
What is Weibull Distribution
The Weibull distribution is a continuous probability distribution that is widely used in various fields such as reliability engineering, survival analysis, and extreme value theory. It is named after the Swedish mathematician Waloddi Weibull, who introduced it in a 1951 paper.
The Weibull distribution has two parameters: the shape parameter (k) and the scale parameter (λ). These parameters determine the shape and spread of the distribution. The probability density function (PDF) of the Weibull distribution is given by:
f(x; k, λ) = (k/λ) * (x/λ)^(k-1) * exp(-(x/λ)^k)
where x ≥ 0, k > 0, and λ > 0.
The Weibull distribution has a wide range of applications due to its flexibility. By varying the shape and scale parameters, it can represent different types of distributions, such as:
- When k = 1, the Weibull distribution becomes an exponential distribution with a constant failure rate, often used to model the lifetime of electronic components.
- When k > 1, the failure rate increases over time, which can be used to model systems with wear-out or aging effects, such as mechanical components.
- When k < 1, the failure rate decreases over time, suitable for modeling systems with “infant mortality” or a burn-in period, like new products with early-life failures.
- When k = 2, the Weibull distribution becomes a Rayleigh distribution, which is useful for modeling phenomena related to wind speeds or the distance between random points in a plane.
Overall, the Weibull distribution is a versatile tool for modeling various types of data, making it an essential distribution in statistics and engineering.
Plot a Weibull Distribution in R
To plot a Weibull distribution in R, you can use the dweibull()
function from the base R package to generate the probability density function (PDF) of a Weibull distribution, and then use the curve()
function to plot the PDF. Here’s a step-by-step guide:
1. Install and load the necessary packages, if not already installed:
# If necessary, uncomment and run the following line to # install the ggplot2 package # install.packages("ggplot2") # Load the ggplot2 package for advanced plotting library(ggplot2)
2. Define the Weibull distribution parameters:
# Set shape and scale parameters for the Weibull distribution shape_param <- 2 scale_param <- 5
3. Define the range for the x-axis:
x_min <- 0 x_max <- 20
4. Generate and plot the Weibull distribution using the curve()
function:
# Plot the Weibull distribution using the curve() function curve( dweibull(x, shape = shape_param, scale = scale_param), from = x_min, to = x_max, main = "Weibull Distribution", xlab = "x", ylab = "Density", col = "blue", lwd = 2 )
Alternatively, you can use the ggplot2 package for a more advanced plot:
# Create a data frame with a sequence of x values data <- data.frame(x = seq(x_min, x_max, length.out = 1000)) # Add a column for the probability density # function of the Weibull distribution data$weibull_density <- dweibull(data$x, shape = shape_param, scale = scale_param) # Generate and plot the Weibull distribution using ggplot2 ggplot(data, aes(x = x, y = weibull_density)) + geom_line(color = "blue", size = 1) + ggtitle("Weibull Distribution") + xlab("x") + ylab("Density") + theme_minimal()
This will create a plot of the Weibull distribution with the specified shape and scale parameters.
Another example of how to generate and plot a Weibull distribution in R
In this example, we’ll generate a sample of 1000 random numbers from a Weibull distribution with shape parameter k (also known as the scale parameter) equal to 2 and scale parameter λ (also known as the shape parameter) equal to 1.
library(ggplot2) # Set seed for reproducibility set.seed(42) # Generate random numbers from Weibull distribution shape <- 2 scale <- 1 n <- 1000 weibull_sample <- rweibull(n, shape, scale) # Create a data frame with the Weibull sample weibull_data <- data.frame(values = weibull_sample) # Plot the Weibull distribution using ggplot2 ggplot(weibull_data, aes(x = values)) + geom_histogram(aes(y = ..density..), bins = 30, color = "black", fill = "lightblue") + labs(title = "Weibull Distribution", x = "Values", y = "Density") + theme_minimal()
This script generates and plots a Weibull distribution with the specified parameters. The histogram shows the density of the generated random numbers following a Weibull distribution.