Learn R Programming

fitPlotR (version 0.1.0)

plot_hf: Plot Hazard Function of a Probability Distribution

Description

#' Plots the PDF of a probability distribution. Supports multiple sets of parameters with shaded areas under the curves.

Usage

plot_hf(
  pdf,
  cdf,
  param_list,
  xlim = c(0, 10),
  ylim = c(0, 6),
  n = 500,
  main = "Hazard Function Plot",
  xlab = "x",
  ylab = "h(x)",
  colors = NULL,
  shade_colors = NULL,
  lwd = 3,
  lty = 2,
  grid = TRUE,
  grid_lty = 3,
  grid_col = "gray80",
  grid_lwd = 1
)

Value

A hazard function plot is displayed. The function invisibly returns NULL.

Arguments

pdf

Function that computes the PDF. Must accept x as the first argument.

cdf

Function that computes the CDF. Must accept x as the first argument.

param_list

A list of parameter lists. Each element is a named list of parameters for pdf and cdf.

xlim

Numeric vector of length 2, specifying the x-axis limits. Default is c(0, 10).

ylim

Numeric vector of length 2, specifying the y-axis limits. Default is c(0, 6).

n

Number of points to evaluate on the x-axis. Default is 500.

main

Title of the plot. Default is "Hazard Function Plot".

xlab

Label for the x-axis. Default is "x".

ylab

Label for the y-axis. Default is "h(x)".

colors

Vector of colors for the lines. Default is rainbow.

shade_colors

Vector of colors for shading under curves. Default is semi-transparent version of colors.

lwd

Line width. Default is 3.

lty

Line type. Default is 2 (dashed).

grid

Logical, whether to draw a grid. Default is TRUE.

grid_lty

Line type for grid. Default is 3.

grid_col

Grid color. Default is "gray80".

grid_lwd

Grid line width. Default is 1.

Examples

Run this code
# Example 1: Generalized Exponential Distribution
pdf_ge <- function(x, alpha, lambda) {
  alpha * lambda * exp(-lambda*x) * (1 - exp(-lambda*x))^(alpha - 1)
}
cdf_ge <- function(x, alpha, lambda) {
  1 - (1 - exp(-lambda*x))^alpha
}
param_values <- list(
  list(alpha = 1, lambda = 1),
  list(alpha = 2, lambda = 1),
  list(alpha = 3, lambda = 0.5),
  list(alpha = 4, lambda = 1.5),
  list(alpha = 5, lambda = 2.5)
)
plot_hf(pdf_ge, cdf_ge, param_values, xlim=c(0,5), ylim=c(0,4), main="HF GE Distribution")

# Example 2: Exponentiated Weibull Distribution
pdf_expweibull <- function(x, a, b, c){
  a * b * c * exp(-(b*x)^c) * (b*x)^(c-1) * (1 - exp(-(b*x)^c))^(a-1)
}
cdf_expweibull <- function(x, a, b, c){
  1 - (1 - exp(-(b*x)^c))^a
}
param_values <- list(
  list(a = 0.3, b = 1.2, c = 1.0),
  list(a = 1.3, b = 0.4, c = 2.3),
  list(a = 1.5, b = 0.9, c = 3.0),
  list(a = 2.0, b = 1.8, c = 2.8),
  list(a = 3.7, b = 2.0, c = 1.5)
)
plot_hf(pdf_expweibull, cdf_expweibull, param_values)

Run the code above in your browser using DataLab