Load necessary library
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
Stimulate the Weibull distributed data
set.seed(123)
n <- 1000
shape <- 1.5
scale <- 1 / 0.03
X <- rweibull(n, shape = shape, scale = scale)
sample mean and median
sample_mean <- mean(X)
sample_median <- median(X)
theoretical mean and median
gamma_value <- gamma(1 + 1/shape)
theoretical_mean <- scale * gamma_value
theoretical_median <- scale * log(2)^(1/shape)
Print theoretical mean and median
cat("Sample Mean:", sample_mean, "\n")
## Sample Mean: 30.24802
cat("Sample Median:", sample_median, "\n")
## Sample Median: 26.61471
cat("Theoretical Mean:", theoretical_mean, "\n")
## Theoretical Mean: 30.09151
cat("Theoretical Median:", theoretical_median, "\n")
## Theoretical Median: 26.10733
hazard function
weibHaz <- function(x, shape, scale) {
dweibull(x, shape = shape, scale = scale) / pweibull(x, shape = shape, scale = scale, lower.tail = FALSE)
}
Plotting hazard functions
plot(0, 0, type = "n", xlim = c(0, 80), ylim = c(0, max(weibHaz(X, 1.5, 1/0.03), weibHaz(X, 1, 1/0.03), weibHaz(X, 0.75, 1/0.03))),
xlab = "Time", ylab = "Hazard", main = "Parametric Survival Distribution(Weibull")
curve(weibHaz(x, shape = 1.5, scale = 1/0.03), from = 0, to = 80, col = 'red', add = TRUE)
curve(weibHaz(x, shape = 1, scale = 1/0.03), from = 0, to = 80, col = 'blue', add = TRUE)
curve(weibHaz(x, shape = 0.75, scale = 1/0.03), from = 0, to = 80, col = 'green', add = TRUE)
legend("topright", legend = c("Shape=1.5, Scale=1/0.03", "Shape=1, Scale=1/0.03", "Shape=0.75, Scale=1/0.03"),
col = c("red", "blue", "green"), lty = 1)