Learn R Programming

fitPlotR (version 0.1.0)

plot_pdf: Plot for the PDF 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_pdf(
  pdf,
  param_list,
  xlim = c(0, 10),
  ylim = c(0, 6),
  n = 500,
  main = "PDF Plot",
  xlab = "x",
  ylab = "Density",
  colors = NULL,
  shade_colors = NULL,
  lwd = 3,
  lty = 2,
  grid = TRUE,
  grid_lty = 3,
  grid_col = "gray80",
  grid_lwd = 1
)

Value

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

Arguments

pdf

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

param_list

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

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 "PDF Plot".

xlab

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

ylab

Label for the y-axis. Default is "Density".

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 with Generalized Exponential Distribution
ge_pdf <- function(x, alpha, lambda) {
alpha * lambda * exp(-lambda * x) * (1 - exp(-lambda * x))^(alpha - 1)
}
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_pdf(pdf = ge_pdf, param_list = param_values, ylim = c(0, 1),
        main = "Generalized Exponential Distribution")

# Example 2 with 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)
}
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))
colors <- c("green", "purple", "yellow", "orange", "darkblue")
plot_pdf(pdf = pdf_expweibull, param_list = param_values,
        main = "PDF of EW Distribution",
        colors = colors, xlim = c(0, 5), ylim = c(0, 3))

Run the code above in your browser using DataLab