# Weibull Distribution in R (4 Examples) | dweibull, pweibull, qweibull & rweibull Functions

In this tutorial you’ll learn how to apply the weibull functions in R.

Let’s get started:

## Example 1: Weibull Density in R (dweibull Function)

In Example 1, we will create a plot representing the weibull density.

First, we need to create some x-values, for which we want to return the corresponding values of the weibull density:

`x_dweibull <- seq(- 5, 30, by = 1)                    # Specify x-values for dweibull function`

Now, we can apply the dweibull function of the R programming language to return the corresponding value of the weibull density with a shape of 0.1 and a scale of 1 for each of our input values:

`y_dweibull <- dweibull(x_dweibull, shape = 0.1)       # Apply dweibull function`

We can create a graphic showing these values with the plot function:

`plot(y_dweibull)                                      # Plot dweibull values`

Figure 1: Weibull Density in R Plot.

Figure 1 illustrates the weibull density for a range of input values between -5 and 30 for a shape of 0.1 and a scale of 1.

## Example 2: Weibull Distribution Function (pweibull Function)

In the second example, we’ll create the cumulative distribution function (CDF) of the weibull distribution.

Again, we need to specify a vector of input values:

`x_pweibull <- seq(- 5, 30, by = 1)                    # Specify x-values for pweibull function`

Now, we can apply the pweibull R command in order to return the corresponding CDF value for each input value…

`y_pweibull <- pweibull(x_pweibull, shape = 0.1)       # Apply pweibull function`

…and then we can draw a plot containing these values in R:

`plot(y_pweibull)                                      # Plot pweibull values`

Figure 2: Cumulative Distribution Function According to Weibull Distribution.

## Example 3: Weibull Quantile Function (qweibull Function)

Next, we will create a plot representing the weibull quantile function.

Let’s create a sequence of values between 0 and 1, for which we want to return the corresponding value of the quantile function:

`x_qweibull <- seq(0, 1, by = 0.02)                    # Specify x-values for qweibull function`

Now, we can use the qweibull R function to return the values of the quantile function:

`y_qweibull <- qweibull(x_qweibull, shape = 0.1)       # Apply qweibull function`

The following R code produces the corresponding scatterplot:

`plot(y_qweibull)                                      # Plot qweibull values`

Figure 3: Weibull Quantile Function.

## Example 4: Random Number Generation (rweibull Function)

We can also draw random values according to the weibull density.

First, we need to specify a seed and a sample size of random numbers:

```set.seed(13579)                                       # Set seed for reproducibility
N <- 100                                              # Specify sample size```

Now, we can use the rweibull command to draw a set of random numbers:

```y_rweibull <- rweibull(N, shape = 0.1)                # Draw N weibull distributed values
y_rweibull                                            # Print values to RStudio console
# 2.924615e+03 1.248956e-09 3.362811e+03 1.392134e-10 4.235278e-01 3.332413e+00 2.545625e+04```

The RStudio console output is showing the result of the previous R syntax.

We can also produce a density plot of these numbers:

```plot(density(y_rweibull),                             # Plot of randomly drawn weibull density
main = "Weibull Distribution in R")```

Figure 4: Random Weibull Numbers.

## Video, Further Resources & Summary

Do you need more information on the R code of this tutorial? Then you may want to have a look at the following video of my YouTube channel. I’m explaining the R programming codes of this tutorial in the video:

You could also read the other pages on distributions and the generation of random numbers in R:

Additionally, you may have a look at some of the related articles of this homepage.

In this post you learned some basics of the weibull distribution in R. Don’t hesitate to let me know in the comments section, in case you have additional questions.

Subscribe to the Statistics Globe Newsletter

• x_dweibull <- seq(- 5, 30, by = 1)
y_dweibull <- dweibull(x_dweibull, shape = 0.1)
Can I consider x_dweibull as the observed value and y_dweibull as the predicted value of x_dweibull? Now if I want to find the root mean square of the error (RMSE) using the following code,
library("Metrics")
rmse(x_dweibull, y_dweibull)
I got the result is Inf.
So, my question is: how do I find the RMSE of a Weibull distribution?

• Hello,

It seemed to me that it is due to the data you created and how randomly you selected the parameters of distribution. It is possible that that’s why you get an Infinite output. You can try first to fit the Weibull distribution and decide the scaling parameters accordingly, then check the difference between the observed and predicted data. See the example below. I have changed the observed data and how the predicted values are calculated.

```set.seed(42)
shape <- 2
scale <- 10
n <- 1000
observed_data <- rweibull(n, shape = shape, scale = scale)

#install.packages("fitdistrplus")
library("fitdistrplus")
fit <- fitdist(observed_data, "weibull")

fitted_shape <- fit\$estimate["shape"]
fitted_scale <- fit\$estimate["scale"]
predicted_data <- dweibull(n, shape = fitted_shape, scale = fitted_scale)

#install.packages("Metrics")
library("Metrics")
rmse(observed_data, predicted_data)```

Regards,
Cansu

• Thnak u.

• Very welcome.

Best,
Cansu

• Dear Cabsu,
Could you please help me how to find R square, AIC, kolmogorov-Smirnov(KS) also of a Weibull distribution?

• Hello,

The Weibull distribution is a continuous probability distribution that is often used in reliability analysis and survival analysis, not typically in regression where you’d calculate something like R-squared or AIC. In the case of a Weibull distribution, parameters can be estimated using maximum likelihood estimation (MLE). The goodness-of-fit of a Weibull distribution to the data could be assessed using a method like the KS test, but using AIC or R-squared would not be typical.

If you have a set of data and you are trying to fit a Weibull distribution to it, you would typically estimate the shape and scale parameters of the Weibull using your data. You could then use the KS test to compare the cumulative distribution function of your data to the cumulative distribution function of a Weibull distribution with the estimated parameters.

Best,
Cansu

• Dear Cansu,
Thanks 4 ur response. But, I have read several papers for wind data where authors estimated parameters using the Weibull distribution (WD), and after that, they determined R square, AIC, KS, RMSE. Some authors estimated parameters using the Gamma distribution. Considering R square, AIC, KS, RMSE, they evaluated which model is better for wind data.
In my case, for wind data, I found parameters using MLE for WD. Now, I tried to find R square, AIC, KS, RMSE. However, as you said, R squre, AIC generally not used for WD for goodness of fit tests.
Then could you plz tell me how to use KS for WD?

• Hello,

You can try this:

```# Load necessary packages
library(MASS)
library(stats)

# Let's generate some data that follows a Weibull distribution
set.seed(123) # for reproducibility
data_sample <- rweibull(200, shape = 2, scale = 1)

# Fit a Weibull distribution to the data
weibull_fit <- fitdistr(data_sample, "weibull")

# Extract the shape and scale parameters from the fit
shape_param <- weibull_fit\$estimate["shape"]
scale_param <- weibull_fit\$estimate["scale"]

# Perform a KS test comparing the data to a Weibull distribution with the estimated parameters
ks_test_result <- ks.test(data_sample, "pweibull", shape = shape_param, scale = scale_param)

# Print the result
print(ks_test_result)```

Best,
Cansu

• U r brilliant!!!.
How u know all these tips, codes? Why I can’t. Believe me, I tried it.

• Appreciated ðŸ™‚

• river735289
May 22, 2023 5:14 pm

Hey,
how can I plot a bimodal weibull-function (two-peaks)? I would like to reproduce the plots from a paper, but I have no data, only the 2 shape and scale parameters and a connection value (g) of the two functions.
scale1 = 9.09
scale2 = 52.30
shape1 = 0.92
shape2 = 2.76
g = 0.73

Thanks a lot!

• Hello!

Could the solution given below be what you are looking for?

```# Load required packages
library(ggplot2)

# Set the parameters for bimodal Weibull distribution
scale1 <- 9.09
scale2 <- 52.30
shape1 <- 0.92
shape2 <- 2.76
g <- 0.73

# Define the bimodal Weibull function
bimodal_weibull <- function(x) {
density <- g * dweibull(x, scale = scale1, shape = shape1) +
(1 - g) * dweibull(x, scale = scale2, shape = shape2)
density
}

# Generate x values
x <- seq(0, 100, length.out = 1000)

# Calculate the y values using the bimodal Weibull function
y <- bimodal_weibull(x)

# Create a data frame for plotting
data <- data.frame(x = x, y = y)

# Plot the bimodal Weibull function
ggplot(data, aes(x = x, y = y)) +
geom_line() +
xlab("x") +
ylab("Density")```

Regards,
Cansu

• Dear Cansu,
Do you know how to draw a truncated Weibull distribution?

• Hello,

I found the function dtrunc() in R seems like it helps to plot a truncated distribution. See the documentation. And here is an example code:

```install.packages("truncdist")
library(truncdist)

shape <- 2  # shape parameter
scale <- 2  # scale parameter
trunc_point <- 4  # truncation point

x <- seq(0, trunc_point, length.out = 1000)
y <- dtrunc(x, spec = "weibull", shape = shape, scale = scale, a = 0, b = trunc_point)

plot(x, y, type = "l", main = "Truncated Weibull Distribution", xlab = "x", ylab = "Probability Density")```

I hope it helps. I am not familiar with the topic; hence it is hard for me to judge the result. Please double-check it with other resources.

Best,
Cansu