Here, we discuss Weibull distribution functions in R, plots, parameter setting, random sampling, density, cumulative distribution and quantiles.
The Weibull distribution with parameters \(\tt{shape}=\alpha\), and \(\tt{scale}=\lambda\) has probability density function (pdf) formula as:
\[ f(x) = \begin{cases} \frac{\alpha}{\lambda}\left(\frac{x}{\lambda}\right)^{\alpha-1}e^{-(x/\lambda)^\alpha}, & x\ge0,\\ 0 \;, & x<0,\end{cases}\]
where \(\alpha>0\), and \({\lambda>0}\).
The mean is \(\lambda \, \Gamma(1+1/\alpha)\,\), and the variance is \(\lambda^2\left[\Gamma\left(1+\frac{2}{\alpha}\right) - \left(\Gamma\left(1+\frac{1}{\alpha}\right)\right)^2\right]\), where \(\Gamma\) is the \(\tt{gamma\;function}\).
See also probability distributions and plots and charts.
The table below shows the functions for Weibull distributions in R.
Function | Usage |
rweibull(n, shape, scale=1) | Simulate a random sample with \(n\) observations |
dweibull(x, shape, scale=1) | Calculate the probability density at the point \(x\) |
pweibull(q, shape, scale=1) | Calculate the cumulative distribution at the point \(q\) |
qweibull(p, shape, scale=1) | Calculate the quantile value associated with \(p\) |
Below is a plot of the Weibull distribution function with \(\tt{shape}=2.5\) and \(\tt{scale}=3\).
x = seq(0, 8, 1/1000); y = dweibull(x, 2.5, 3)
plot(x, y, type = "l",
xlim = c(0, 8), ylim = c(0, max(y)),
main = "Probability Density Function of Weibull Distribution (2.5, 3)",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add legend
legend("topright", "shape = 2.5 \nscale = 3",
lwd = 2,
col = "blue",
bty = "n")
Below is a plot of multiple Weibull distribution functions in one graph.
x1 = seq(0, 7, 1/1000); y1 = dweibull(x1, 3, 1)
x2 = seq(0, 7, 1/1000); y2 = dweibull(x2, 1, 1)
x3 = seq(0, 7, 1/1000); y3 = dweibull(x3, 1, 2)
x4 = seq(0.05, 7, 1/1000); y4 = dweibull(x4, 0.5, 2)
plot(x1, y1, type = "l",
xlim = c(0, 7), ylim = range(c(y1, y2, y3, y4)),
main = "Probability Density Functions of Weibull Distributions",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
points(x2, y2, type = "l", lwd = 2, col = "red")
points(x3, y3, type = "l", lwd = 2, col = "green")
points(x4, y4, type = "l", lwd = 2, col = "black")
# Add legend
legend("topright", c("shape = 3, scale = 1",
"shape = 1, scale = 1",
"shape = 1, scale = 2",
"shape = 0.5, scale = 2"),
lwd = c(2, 2, 2, 2),
col = c("blue", "red", "green", "black"),
bty = "n")
In the Weibull distribution functions, the scale parameter is pre-specified as \(\lambda=1\), hence it does not need to be specified, unless it is to be set to a different value.
For example, for dweibull()
, the following are the
same:
# The order of 2 and 1 matters here as the parameter names are not used.
# The first number 2 is shape, and 1 is scale.
dweibull(0.3, 2); dweibull(0.3, 2, 1)
[1] 0.5483587
[1] 0.5483587
[1] 0.5483587
[1] 0.5483587
Sample 1200 observations from the Weibull distribution with \(\tt{shape} = 1.2\) and \(\tt{scale} = 4\):
set.seed(100) # Line allows replication (use any number).
sample = rweibull(1200, 1.2, 4)
hist(sample,
main = "Histogram of 1200 Observations from Weibull Distribution
with Shape = 1.2 and Scale = 4",
xlab = "x",
col = "deepskyblue", border = "white")
Calculate the density at \(x = 0.65\), in the Weibull distribution with \(\tt{shape} = 0.7\) and \(\tt{scale} = 2.4\):
[1] 0.2890849
x = seq(0.1, 9, 1/1000); y = dweibull(x, 0.7, 2.4)
plot(x, y, type = "l",
xlim = c(0, 9), ylim = c(0, max(y)),
main = "Probability Density Function of Weibull Distribution
with Shape = 0.7 and Scale = 2.4",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add lines
segments(0.65, -1, 0.65, 0.2890849)
segments(-1, 0.2890849, 0.65, 0.2890849)
Calculate the cumulative distribution at \(x = 3.1\), in the Weibull distribution with \(\tt{shape} = 1.4\) and \(\tt{scale} = 2\). That is, \(P(X \le 3.1)\):
[1] 0.8422887
x = seq(0, 8, 1/1000); y = pweibull(x, 1.4, 2)
plot(x, y, type = "l",
xlim = c(0, 8), ylim = c(0,1),
main = "Cumulative Distribution Function of Weibull Distribution
with Shape = 1.4 and Scale = 2",
xlab = "x", ylab = "Cumulative Distribution",
lwd = 2, col = "blue")
# Add lines
segments(3.1, -1, 3.1, 0.8422887)
segments(-6, 0.8422887, 3.1, 0.8422887)
x = seq(0, 8, 1/1000); y = dweibull(x, 1.4, 2)
plot(x, y, type = "l",
xlim = c(0, 8), ylim = c(0, max(y)),
main = "Probability Density Function of Weibull Distribution
with Shape = 1.4 and Scale = 2",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add shaded region and legend
point = 3.1
polygon(x = c(x[x <= point], point),
y = c(y[x <= point], 0),
col = "limegreen")
legend("topright", c("Area = 0.8422887"),
fill = c("limegreen"),
inset = 0.01)
For upper tail, at \(x = 3.1\), that is, \(P(X \ge 3.1) = 1 - P(X \le 3.1)\), set the "lower.tail" argument:
[1] 0.1577113
x = seq(0, 8, 1/1000); y = dweibull(x, 1.4, 2)
plot(x, y, type = "l",
xlim = c(0, 8), ylim = c(0, max(y)),
main = "Shaded Upper Region: Probability Density Function of
Weibull Distribution with Shape = 1.4 and Scale = 2",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add shaded region and legend
point = 3.1
polygon(x = c(point, x[x >= point]),
y = c(0, y[x >= point]),
col = "limegreen")
legend("topright", c("Area = 0.1577113"),
fill = c("limegreen"),
inset = 0.01)
Derive the quantile for \(p = 0.9\), in the Weibull distribution with \(\tt{shape} = 3\) and \(\tt{scale} = 2\). That is, \(x\) such that, \(P(X \le x)=0.9\):
[1] 2.641001
x = seq(0, 4, 1/1000); y = pweibull(x, 3, 2)
plot(x, y, type = "l",
xlim = c(0, 4), ylim = c(0,1),
main = "Cumulative Distribution Function of Weibull Distribution
with Shape = 3 and Scale = 2",
xlab = "x", ylab = "Cumulative Distribution",
lwd = 2, col = "blue")
# Add lines
segments(2.641001, -1, 2.641001, 0.9)
segments(-1, 0.9, 2.641001, 0.9)
x = seq(0, 4, 1/1000); y = dweibull(x, 3, 2)
plot(x, y, type = "l",
xlim = c(0, 4), ylim = c(0, max(y)),
main = "Probability Density Function of Weibull Distribution
with Shape = 3 and Scale = 2",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add shaded region and legend
point = 2.641001
polygon(x = c(x[x <= point], point),
y = c(y[x <= point], 0),
col = "limegreen")
legend("topright", c("Area = 0.9"),
fill = c("limegreen"),
inset = 0.01)
For upper tail, for \(p = 0.1\), that is, \(x\) such that, \(P(X \ge x)=0.1\):
[1] 2.641001
x = seq(0, 4, 1/1000); y = dweibull(x, 3, 2)
plot(x, y, type = "l",
xlim = c(0, 4), ylim = c(0, max(y)),
main = "Shaded Upper Region: Probability Density Function of
Weibull Distribution with Shape = 3 and Scale = 2",
xlab = "x", ylab = "Density",
lwd = 2, col = "blue")
# Add shaded region and legend
point = 2.641001
polygon(x = c(point, x[x >= point]),
y = c(0, y[x >= point]),
col = "limegreen")
legend("topright", c("Area = 0.1"),
fill = c("limegreen"),
inset = 0.01)
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes