Statistics Using R

Using Weibull distribution in R programming

We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

  1. Introduction of Weibull distribution

Weibull distribution, named after Swedish mathematician Waloddi Weibull, is a continuous distribution which is widely used to model the distribution of random time between events. Exponential distribution, which is used to model the random time until next event occurs and have so-called memoryless feature or constant failure rate. In order to relax this memoryless condition, analysts may use either Gamma distribution or Weibull distribution instead.

Weibull distribution has two following parameters:

k – shape parameter , and

lambda – scale parameter.

The following figure shows several probability density functions of Weibull distribution, in terms of different parameters.

Weibull distribution has the following probability function:

Exponential distribution is actually one kind of Weibull distribution, when K = 1.

2. R functions for calculating Weibull distribution

There are primarily four functions in R programming that can be used for Weibull distribution calculation :

dweibull() – Probability density
pweibull() – Cumulative probability
qweibull() – Quantile value
rweibull() – Random number generation

dweibull() is used to compute probability densities for random variables from a given Weibull distribution. The basic form of the function is dweibull(x, shape, scale),

Where

x is a vector of Weibull variables, and shape and scale parameters both have default value 1.

The following code example illustrates the computation and plot of probability densities for Weibull variates between 0 and 10, with shape and scale both located at 1.

# Creating a sequence of x-values
> x <- seq(0, 10, by = 0.2)

> k<- 1
> lambda <- 1
 
# Calling dweibull() Function, to compute probability #densities
> y <- dweibull(x, shape = k, scale=lambda)
#plot densities
> plot(x, y)

pweibull() function is used to compute the cumulative probability for a range of random variables from Weibull distribution. The basic form of the function is pweibull(x , shape, scale),

Where

x is a vector of Weibull variables, and shape and scale parameters both have default value 1.

Following example shows the calculation of cumulative probabilities for a Weibull variates between 0 and 10, with shape and scale parameters located at 2 and 5, respectively.

# Creating a vector of Weibull variates
> x <- seq(0, 10, by = 0.2)

> k<- 2
> lambda <- 5
> 
# Calling pweibull() Function to calculate cumulative #probabilities 
> y <- pweibull(x, shape = k, scale=lambda)
> plot(x, y)

From the curve, we can see that around 50% of the variables of this distribution are less than or equal to 4.

qweibull() function is the opposite operation of pweibull(). It is used to compute the quantile values of a Weibull distribution, in terms of the given cumulative probabilities. The basic form of the function is qweibull(prob, shape, scale),

Where

prob represents the vector of cumulative probabilities, and shape and scale parameters both have default value 1.

The following code example presents the calculation of quantile values from a Weibull distribution whose shape and scale parameters both equal to 3, given the known cumulative probabilities between 0 and 1.

#vector of cumulative probabilities
probs <- seq(0, 1, by = 0.1)

> k<- 3
> lambda <- 3

# Calling qweibull() Function, to compute quantile values
> y <- qweibull(probs, shape = k, scale=lambda)
> plot(probs, y)
> 

The figure shows that about 40% of the variables in this distribution have values lower than 2.5 .

rweibull() is used to generate random variables from a given Weibull distribution. The basic form of the function is rweibull(N, shape, scale),

Where

N denotes the number of random values to be generated,

and shape and scale both have default value 1.

The following code example shows the generation and plot of 10000 random values from a Weibull distribution with both shape and scale located at 3.

N <- 10000 
> k<- 3
> lambda <- 3
# Calling rweibull() Function to generate random values from #a given Weibull distribution
> x <- rweibull(N, shape = k, scale=lambda)
#first 10 values
>x[1:10]
 [1] 2.732754 1.859545 2.897181 3.059580 4.030458 3.764852 1.305473
 [8] 1.180479 1.372219 2.271262
> plot(hist(x))

You can also visit our YouTube channel about using Weibull distribution in R.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago