Automated random variable distribution inference using Kullback-Leibler divergence and simulating best-fitting distribution

[This article was first published on T. Moudiki's Webpage - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Another post from R package misc! This time, we’ll see how to fit multiple continuous parametric distributions on a vector of data and simulate best-fitting distribution. Under the hood, misc::fit_param_dist uses a loop of MASS::fitdistr calls and Kullback-Leibler divergence for checking distribution adequacy.

remotes::install_github("thierrymoudiki/misc")

Example usage 1

set.seed(123)
n <- 1000
vector <- rweibull(n, 2, 3)  # Replace with your vector

start <- proc.time()[3]
simulate_function <- misc::fit_param_dist(vector)
end <- proc.time()[3]
print(paste("Time taken:", end - start))

simulated_data <- simulate_function(n)  # Generate 100 samples from the best-fit distribution

par(mfrow = c(1, 2))
hist(vector, main = "Original Data", xlab = "Value", ylab = "Frequency")
hist(simulated_data, main = "Simulated Data", xlab = "Value", ylab = "Frequency")

xxx

Example usage 2

set.seed(123)
n <- 1000
vector <- rnorm(n)  # Replace with your vector

start <- proc.time()[3]
simulate_function <- misc::fit_param_dist(vector)
end <- proc.time()[3]
print(paste("Time taken:", end - start))

simulated_data <- simulate_function(n)  # Generate 1000 samples from the best-fit distribution

par(mfrow = c(1, 2))
hist(vector, main = "Original Data", xlab = "Value", ylab = "Frequency")
hist(simulated_data, main = "Simulated Data", xlab = "Value", ylab = "Frequency")

xxx

Example usage 3

# Example usage 1
set.seed(123)
n <- 1000
vector <- rlnorm(n)  # Replace with your vector

start <- proc.time()[3]
simulate_function <- misc::fit_param_dist(vector)
end <- proc.time()[3]
print(paste("Time taken:", end - start))

simulated_data <- simulate_function(n)  # Generate 1000 samples from the best-fit distribution

par(mfrow = c(1, 2))
hist(vector, main = "Original Data", xlab = "Value", ylab = "Frequency")
hist(simulated_data, main = "Simulated Data", xlab = "Value", ylab = "Frequency")

xxx

Example usage 4

set.seed(123)
n <- 1000
vector <- rbeta(n, 2, 3)  # Replace with your vector

start <- proc.time()[3]
simulate_function <- misc::fit_param_dist(vector, verbose=TRUE)
end <- proc.time()[3]
print(paste("Time taken:", end - start))

simulated_data <- simulate_function(n)  # Generate 1000 samples from the best-fit distribution

par(mfrow = c(1, 2))
hist(vector, main = "Original Data", xlab = "Value", ylab = "Frequency")
hist(simulated_data, main = "Simulated Data", xlab = "Value", ylab = "Frequency")

xxx

Bonus: You can develop a package at the command line, by putting this file in the root directory of your package, and typing make or make help at the command line. Here’s the Makefile:

To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)