Learn R Programming

fitPlotR (version 0.1.0)

plot_multi_fitted: Plot Multiple Fitted Distributions

Description

Creates 2x2 plots for multiple fitted distributions: Fitted PDFs, Fitted CDFs vs Empirical CDF, QQ-Plots, and PP-Plots.

Usage

plot_multi_fitted(
  data,
  pdf_list,
  cdf_list,
  qf_list,
  params_list,
  dist_names = NULL,
  col_list = NULL,
  lty_list = NULL,
  lwd_list = NULL,
  main_pdf = "Fitted PDFs",
  main_cdf = "Fitted CDFs",
  main_qq = "QQ Plots",
  main_pp = "PP Plots",
  xlab = "x"
)

Value

NULL (plots are generated as a side effect)

Arguments

data

Numeric vector of observed data.

pdf_list

List of PDF functions. Each function should take x and par.

cdf_list

List of CDF functions. Each function should take x and par.

qf_list

List of quantile functions (inverse CDF). Each function should take p and par.

params_list

List of parameter vectors corresponding to each distribution.

dist_names

Optional vector of distribution names.

col_list

Optional vector of colors for each distribution.

lty_list

Optional vector of line types for each distribution.

lwd_list

Optional vector of line widths for each distribution.

main_pdf

Title for PDF plot.

main_cdf

Title for CDF plot.

main_qq

Title for QQ plot.

main_pp

Title for PP plot.

xlab

Label for x-axis.

Examples

Run this code
# Example Multiple Distributions
set.seed(1)
data <- rexp(200, 1.1)

# Exponential
pdf_exp <- function(x, par) par[1] * exp(-par[1] * x)
cdf_exp <- function(x, par) 1 - exp(-par[1] * x)
qf_exp  <- function(p, par) -log(1 - p) / par[1]

# Generalized Exponential
pdf_gexp <- function(x, par) {
  a <- par[1]; l <- par[2]
  a * l * exp(-l*x) * (1-exp(-l*x))^(a-1)
}
cdf_gexp <- function(x, par) {
  a <- par[1]; l <- par[2]
  (1-exp(-l*x))^a
}
qf_gexp <- function(p, par) {
  a <- par[1]; l <- par[2]
  -log(1 - p^(1/a)) / l
}

# Weibull
pdf_weibull <- function(x, par) {
  k <- par[1]; l <- par[2]
  (k/l) * (x/l)^(k-1) * exp(-(x/l)^k)
}
cdf_weibull <- function(x, par) {
  k <- par[1]; l <- par[2]
  1 - exp(-(x/l)^k)
}
qf_weibull <- function(p, par) {
  k <- par[1]; l <- par[2]
  l * (-log(1 - p))^(1/k)
}

# Normal
pdf_norm <- function(x, par) dnorm(x, par[1], par[2])
cdf_norm <- function(x, par) pnorm(x, par[1], par[2])
qf_norm <- function(p, par) qnorm(p, par[1], par[2])

data <- rexp(200, 1)
# Call the plot function
plot_multi_fitted(
  data = data,
  pdf_list = list(pdf_exp, pdf_gexp, pdf_weibull, pdf_norm),
  cdf_list = list(cdf_exp, cdf_gexp, cdf_weibull, cdf_norm),
  qf_list  = list(qf_exp, qf_gexp, qf_weibull, qf_norm),
  params_list = list(
    c(1.1),
    c(2, 1.3),
    c(1.5, 2),
    c(0, 1)
  ),
  dist_names = c("Exp", "GExp", "Weibull", "Normal"),
  col_list = c("blue", "red", "darkgreen", "purple"),
  lty_list = c(1, 2, 3, 4),
  lwd_list = c(3, 3, 3, 3)
)

Run the code above in your browser using DataLab