{"id":17606515,"url":"https://github.com/mhahsler/fit_dist","last_synced_at":"2025-07-09T03:03:01.742Z","repository":{"id":66161441,"uuid":"223983627","full_name":"mhahsler/fit_dist","owner":"mhahsler","description":"Simple R script to fit distributions to data","archived":false,"fork":false,"pushed_at":"2021-01-21T15:52:18.000Z","size":71,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-05T00:41:38.007Z","etag":null,"topics":["distribution","educational","statistics"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc-by-sa-4.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mhahsler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-25T15:40:52.000Z","updated_at":"2024-04-30T20:29:37.000Z","dependencies_parsed_at":"2023-03-13T20:30:40.629Z","dependency_job_id":null,"html_url":"https://github.com/mhahsler/fit_dist","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhahsler%2Ffit_dist","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhahsler%2Ffit_dist/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhahsler%2Ffit_dist/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhahsler%2Ffit_dist/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mhahsler","download_url":"https://codeload.github.com/mhahsler/fit_dist/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246254148,"owners_count":20747949,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distribution","educational","statistics"],"created_at":"2024-10-22T15:44:42.364Z","updated_at":"2025-03-29T23:16:50.902Z","avatar_url":"https://github.com/mhahsler.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fit_dist\nSimple R script to fit distributions to data based on \nthe R package `fitdistrplus`. This script is intended to provide students with a simple way to fit distributions (e.g., for input analysis in a simulation course).\n\n![CC](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)\nThis work is licensed under the\n[Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/). For questions please contact\n[Michael Hahsler](http://michael.hahsler.net).\n\n\n## Required Software\n\n* Install [R](https://cran.r-project.org/) \n* Optional: Install [R Studio](https://rstudio.com/products/rstudio/download/)\n* Load the script: `source('https://raw.githubusercontent.com/mhahsler/fit_dist/master/fit_dist.R')`. \n\n## Usage\n\n```\nsource('https://raw.githubusercontent.com/mhahsler/fit_dist/master/fit_dist.R')\n\nfit_dist(x, distributions = NULL, discrete = NULL, plot = TRUE, ...)\n```\nwhere `x` is a vector with the data, `distributions` is a vector with the distributions to fit,\n`discrete` indicates if discrete or continuous distributions should be fit, and\n`plot` indicates if a Q-Q plot should be displayed. The function displays the results of statistical tests and returns a list with the estimated parameters.\n\n_Note:_ The plot might be too large for the little window in R Studio. Use `X11()` (`quarz()` for Macs) to open a bigger window for plotting.\n\n\n## Examples\n\nLoad the script first.\n```\nsource('https://raw.githubusercontent.com/mhahsler/fit_dist/master/fit_dist.R')\n```\n\nFit some random data drawn from a normal distribution.\n```\nx \u003c- rnorm(100, mean = 10, sd = 1)\nfit \u003c- fit_dist(x)\n```\n\n![Q-Q plot](example.png)\n\n```\nFitting unif, norm, lnorm, exp, gamma, beta, weibull\nError in computing default starting values.\nError in manageparam(start.arg = start, fix.arg = fix.arg, obs = data,  : \n  Error in start.arg.default(obs, distname) : \n  values must be in [0-1] to fit a beta distribution\n\nTest results:\n        Kolmogorov.iSmirnov.test Cramer.von.Mises.test Anderson.Darling.test Chi.Square.p.value\nunif                not rejected          not computed          not computed       1.677783e-02\nnorm                not rejected          not computed          not computed       7.435807e-01\nlnorm               not rejected          not computed          not computed       5.362755e-01\nexp                     rejected              rejected              rejected      2.148867e-127\ngamma               not rejected          not rejected          not rejected       6.183843e-01\nweibull             not rejected          not rejected          not rejected       7.797596e-01\n\n*** Best fit using the AIC is: norm ***\n*** Best fit using the BIC is: norm ***\n```\n\nThe code is unable to fit a beta distribution. Since the data is not between 0 and 1.\n\n```\nfit\n```\n\n```\n$unif\nFitting of the distribution ' unif ' by maximum likelihood \nParameters:\n     estimate Std. Error\nmin  7.570601         NA\nmax 12.224757         NA\n\n$norm\nFitting of the distribution ' norm ' by maximum likelihood \nParameters:\n     estimate Std. Error\nmean 9.986087 0.09884770\nsd   0.988477 0.06989556\n\n$lnorm\nFitting of the distribution ' lnorm ' by maximum likelihood \nParameters:\n         estimate  Std. Error\nmeanlog 2.2962177 0.010021790\nsdlog   0.1002179 0.007083301\n\n$exp\nFitting of the distribution ' exp ' by maximum likelihood \nParameters:\n      estimate Std. Error\nrate 0.1001393 0.01001293\n\n$gamma\nFitting of the distribution ' gamma ' by maximum likelihood \nParameters:\n       estimate Std. Error\nshape 100.68077   14.21488\nrate   10.08203    1.42700\n\n$weibull\nFitting of the distribution ' weibull ' by maximum likelihood \nParameters:\n      estimate Std. Error\nshape 11.16064 0.84788207\nscale 10.43341 0.09891693\n\nattr(,\"gof\")\nGoodness-of-fit statistics\n                                  unif       norm      lnorm        exp      gamma    weibull\nKolmogorov-Smirnov statistic 0.1675067 0.06388482 0.08325544  0.5419494 0.07685832 0.06338501\nCramer-von Mises statistic   0.7575686 0.05303841 0.07260888  8.0778787 0.06344904 0.08042399\nAnderson-Darling statistic         Inf 0.34161070 0.44366264 37.3407902 0.39257035 0.57981244\n\nGoodness-of-fit criteria\n                               unif     norm    lnorm      exp    gamma  weibull\nAkaike's Information Criterion   NA 285.4697 286.9495 662.2386 286.1829 289.6857\nBayesian Information Criterion   NA 290.6801 292.1599 664.8437 291.3932 294.8961\n```\n\n__Note:__ Look for the closest match in the Q-Q plot and the smallest numbers in Goodness-of-fit statistics and criteria. You can look up the different [goodness-of-fit statistics on Wikipedia](https://en.wikipedia.org/wiki/Goodness_of_fit). It is often also helpful to look at the\n[relationship between distributions](https://en.wikipedia.org/wiki/Relationships_among_probability_distributions) when choosing a fitted distribution.   \n\n\n\n### Fit a specific distribution\n```\nx \u003c- rexp(100))\nfit_dist(x, distributions = \"exp\")\n```\n\n### The function automatically recognizes data for discrete distributions \n```\nx \u003c- rpois(100, lambda = 2)\nfit_dist(x)\n\nTrying to fit binom, pois, nbinom, geom, hyper \n...\n```\n\nTo avoid this behavior and fit continuous distributions, use `discrete = FALSE`.\n\n### Fit your own data\nYou can use your own data by reading in CVS files in R Studio via `Environment` tab and `Import Datasets` (in the window to the right) or you can type `my_data \u003c- read.csv(\"my_data.csv\")`. You can then use the appropriate column (in this example `x`) to fit the distribution using `fit_dist(my_data$x)`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhahsler%2Ffit_dist","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmhahsler%2Ffit_dist","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhahsler%2Ffit_dist/lists"}