Simulating Hazard Functions from Mixture Distributions: A Step-by-Step Guide in R

Mixture Distributions in R: Simulating Hazard Functions

===========================================================

In this article, we will delve into the world of mixture distributions in R and explore how to simulate hazard functions from a mixture of Weibull distributions. We’ll also discuss the limitations of using Exponential distributions as a special case of Weibull and provide guidance on modifying existing code to achieve the desired hazard function.

Introduction to Mixture Distributions


A mixture distribution is a probabilistic model that combines multiple underlying distributions with a specified probability mass. In this article, we’ll focus on simulating hazard functions from a mixture of Weibull distributions, which will enable us to create bathtub-shaped hazard functions.

Understanding Weibull Distributions


The Weibull distribution is a continuous probability distribution named after Swedish engineer Waloddi Weibull, who described it in detail. It’s commonly used to model the time between failures in reliability engineering and survival analysis. The Weibull distribution has two parameters: shape (β) and scale (θ). The hazard function of the Weibull distribution is given by:

h(x | β, θ) = β / θ * (x / θ)^(β - 1)

where h(x) is the hazard function at time x.

Simulating Hazard Functions from Mixture Distributions


To simulate a hazard function from a mixture of Weibull distributions, we need to specify the weights and parameters of each underlying distribution. The weights represent the proportion of data points that belong to each distribution. Let’s assume we have two Weibull distributions with parameters β1, θ1 and β2, θ2, respectively.

The hazard function for a mixture of these two distributions can be represented as:

h(x) = w1 * dweibull(x, β1, θ1) + w2 * dweibull(x, β2, θ2)

where w1 and w2 are the weights, dweibull is the Weibull survival function, and dweibull(x, β, θ) = 1 - (1 + (x / θ)^β)^(-1/β).

Code: Simulating Hazard Functions from Mixture Distributions


In the provided Stack Overflow answer, the author uses the pexp and rexp functions to simulate data from Exponential distributions, which are degenerate cases of Weibull distributions with shape parameters less than 1. To achieve a bathtub-shaped hazard function, we need to modify this code to create a mixture of two Weibull distributions.

# Load necessary libraries
library(ggplot2)
library(dweibull)

# Define the weights and parameters of each underlying distribution
w1 <- 0.6
w2 <- 0.4

β1 <- 3
θ1 <- 10

β2 <- 6
θ2 <- 8

# Define the function to simulate hazard functions from mixture distributions
hazmix = function(w1, w2, β1, θ1, β2, θ2) {
  w1 * dweibull(x, β1, θ1) / (1 - pweibull(x, β1, θ1)) +
    w2 * dweibull(x, β2, θ2) + ((1 - (w1 + w2)) *
      dweibull(x, 10, 6) / (1 - pweibull(x, 10, 6)))
}

# Simulate hazard functions from mixture distributions
x <- seq(0, 15, by = 0.1)
hazmix(x, w1, β1, θ1, β2, θ2)

# Plot the simulated hazard functions
png()
plot(hazmix(x, w1, β1, θ1, β2, θ2), ylim = c(0, 1))
dev.off()

Modifying Existing Code to Achieve Desired Hazard Function


In this example, we modified the code to create a mixture of two Weibull distributions with shape parameters β1 and β2. To achieve a hazard function with a more gradual slope near zero (i.e., less than 0.1), we can adjust the weights and parameters accordingly.

For instance, if we want to create a hazard function that is more “scoopy” near the origin, we can increase the weight of the first Weibull distribution (w1) or decrease its shape parameter (β1). Conversely, if we want a more gradual slope near zero, we can decrease the weight of the second Weibull distribution (w2) or increase its scale parameter (θ2).

Normalization of Mixture Distributions


In some cases, it may be necessary to normalize the mixture distribution so that the weights sum up to 1. This is easily done using the normalize function in R:

# Normalize the mixture distribution
hazmix_normalized = hazmix(w1 / (w1 + w2), w2 / (w1 + w2),
  β1, θ1, β2, θ2)

Conclusion


In this article, we explored how to simulate hazard functions from a mixture of Weibull distributions in R. We discussed the importance of understanding Weibull distributions and simulated hazard functions using a simple example code. By adjusting weights and parameters, we can create bathtub-shaped hazard functions with more gradual slopes near zero.


Last modified on 2025-04-03