- Notifications
You must be signed in to change notification settings - Fork1
Create tidy probability/density tibbles and plots of randomly generated and empirical data.
License
Unknown, MIT licenses found
Licenses found
spsanderson/TidyDensity
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
To view the full wiki click here:Full TidyDensityWiki
{TidyDensity} is a comprehensive R package that makes working withrandom numbers and probability distributions easy, intuitive, and tidy.Whether you’re simulating data, exploring distributions, or performingstatistical analysis, TidyDensity provides a unified interface thatintegrates seamlessly with the tidyverse ecosystem.
- 35+ Probability Distributions: Generate random data from a widevariety of continuous and discrete distributions
- Tidy Output: All functions return tibbles with a consistent,predictable structure
- Rich Metadata: Each distribution includes density (
d_),probability (p_), quantile (q_), and random generation (r_)components - Beautiful Visualizations: Built-in plotting functions with supportfor multiple plot types
- Parameter Estimation: Estimate distribution parameters fromempirical data using MLE, MME, and MVUE methods
- Bootstrap Analysis: Perform bootstrap resampling with integratedplotting and analysis tools
- Mixture Models: Create and analyze mixture distributions
- Interactive Plots: Generate interactive visualizations with plotlyintegration
Install the released version fromCRAN:
install.packages("TidyDensity")Or install the development version fromGitHub:
# install.packages("devtools")devtools::install_github("spsanderson/TidyDensity")
Generate random data from a normal distribution and visualize it:
library(TidyDensity)library(dplyr)library(ggplot2)# Generate data from normal distributiontn<- tidy_normal(.n=100,.mean=0,.sd=1,.num_sims=6)# View the tibble structuretn#> # A tibble: 600 × 7#> sim_number x y dx dy p q#> <fct> <int> <dbl> <dbl> <dbl> <dbl> <dbl>#> 1 1 1 -0.626 -3.51 0.000235 0.266 -0.626#> 2 1 2 0.184 -3.37 0.000617 0.573 0.184#> 3 1 3 -0.836 -3.22 0.00147 0.202 -0.836#> 4 1 4 1.60 -3.07 0.00322 0.945 1.60#> # ... with 596 more rows
Alltidy_ distribution functions return a tibble with the followingcolumns:
sim_number: Simulation identifierx: Index of generated pointy: The randomly generated valuedx: Density function x-valuesdy: Density function y-values (PDF)p: Cumulative probability (CDF)q: Quantile values
TidyDensity includestidy_autoplot() for quick, publication-readyvisualizations:
# Density plottidy_autoplot(tn,.plot_type="density")# Quantile plottidy_autoplot(tn,.plot_type="quantile")# Probability plottidy_autoplot(tn,.plot_type="probability")# QQ plottidy_autoplot(tn,.plot_type="qq")
When simulating many distributions, the legend is automatically hiddenfor clarity:
tn<- tidy_normal(.n=100,.num_sims=20)tidy_autoplot(tn,.plot_type="density")
TidyDensity supports 35+ probability distributions:
- Normal Family: Normal, Log-Normal, Inverse Normal
- Exponential Family: Exponential, Inverse Exponential
- Gamma Family: Gamma, Inverse Gamma
- Beta Family: Beta, Generalized Beta
- Pareto Family: Pareto, Inverse Pareto, Single Parameter Pareto,Generalized Pareto
- Weibull Family: Weibull, Inverse Weibull
- Burr Family: Burr, Inverse Burr
- Other: Cauchy, Chi-Square, F-Distribution, t-Distribution,Logistic, Paralogistic, Triangular, Uniform
- Bernoulli
- Binomial
- Zero-Truncated Binomial
- Geometric
- Zero-Truncated Geometric
- Hypergeometric
- Negative Binomial
- Poisson
- Zero-Truncated Poisson
Each distribution has a correspondingtidy_*() function, e.g.,tidy_beta(),tidy_gamma(),tidy_poisson().
Estimate distribution parameters from empirical data:
# Generate sample datax<-mtcars$mpg# Estimate normal distribution parametersest<- util_normal_param_estimate(x,.auto_gen_empirical=TRUE)# View parameter estimatesest$parameter_tbl#> # A tibble: 2 × 7#> dist_type samp_size min max mean method shape_est#> <chr> <int> <dbl> <dbl> <dbl> <chr> <dbl>#> 1 Gaussian 32 10.4 33.9 20.1 MLE/MME 6.03#> 2 Gaussian 32 10.4 33.9 20.1 MVUE 6.10# Compare empirical data with fitted distributionest$combined_data_tbl|> tidy_combined_autoplot()
Perform bootstrap resampling for robust statistical inference:
# Bootstrap resamplingbs<- tidy_bootstrap(mtcars$mpg,.num_sims=2000)# Bootstrap statisticsbootstrap_stat<- tidy_bootstrap(mtcars$mpg)|> bootstrap_unnest_tbl()|> summarise(mean_est= mean(y),sd_est= sd(y),ci_lower= quantile(y,0.025),ci_upper= quantile(y,0.975) )
Create mixture distributions by combining multiple distributions:
# Create a mixture of two normal distributionsmix<- tidy_mixture_density(.tbl_list=list( tidy_normal(.n=100,.mean=-2,.sd=0.5), tidy_normal(.n=100,.mean=2,.sd=0.5) ),.mixture_type="add")tidy_autoplot(mix,.plot_type="density")
Work directly with your own data:
# Create empirical distribution from dataemp<- tidy_empirical(mtcars$mpg,.num_sims=5)# Plot empirical distributiontidy_autoplot(emp,.plot_type="density")
Compare multiple distributions with different parameters:
# Create multiple simulations with different parametersmulti<- tidy_multi_single_dist(.tidy_dist="tidy_normal",.param_list=list(list(.n=100,.mean=0,.sd=1),list(.n=100,.mean=0,.sd=2),list(.n=100,.mean=2,.sd=1) ))tidy_autoplot(multi,.plot_type="density")
Contributions are welcome! Here’s how you can help:
- 🐛 Report bugs or request features viaGitHubIssues
- 📝 Submit pull requests for bug fixes or new features
- 📖 Improve documentation or add examples
- ⭐ Star the repository to show your support
Please follow ourCode of Conduct whenparticipating in this project.
If you use TidyDensity in your research, please cite it:
citation("TidyDensity")- 📖 Read thedocumentation
- 🐛 Report bugs atGitHubIssues
- 💬 Ask questions onGitHubDiscussions
Steven P. Sanderson II, MPH
- Email:spsanderson@gmail.com
- ORCID:0009-0006-7661-8247
MIT License - seeLICENSE.md for details
About
Create tidy probability/density tibbles and plots of randomly generated and empirical data.
Topics
Resources
License
Unknown, MIT licenses found
Licenses found
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.


