How to Fit a Gamma Distribution to a Dataset in R
The Gamma distribution is specifically used to determine the exponential distribution, Erlang distribution, and chi-squared distribution. It is also referred to as the two-parameter family having the continuous probability distribution.
Stepwise Implementation
Step 1: Install and import the fitdistrplus package in the R:
install.package("fitdistrplus") library(fitdistrplus)
The fitdistrplus package provides us fitdist function to fit a distribution.
Syntax:
fitdist(dataset, distr = “choice”, method = “method”)
Here,
- distr = “choice” : It represents the distribution choice
- method = “method” : It represents the method of fitting the data
Step 2: Now, we would fit the dataset data with the help of the gamma distribution and with the help of the maximum likelihood estimation approach to fit the dataset.
R
# Generating 20 random values that uses # a gamma distribution having shape # parameter as 10 # combined with some gaussian noise data <- rgamma (20, 3, 10) + rnorm (20, 0, .02) # Fit the dataset to a gamma distribution # using mle ans <- fitdist (data, distr = "gamma" , method = "mle" ) # Display the summary of ans summary (ans) |
Output:
Now we will produce some plots that will show how well the gamma distribution fits the dataset with the help of the below syntax.
R
# Import the package library (fitdistrplus) # Generating 20 random values that uses a # gamma distribution having shape parameter # as 10 # combined with some gaussian noise data <- rgamma (20, 3, 10) + rnorm (20, 0, .02) # Fitting the dataset to a gamma distribution # with the help of mle ans <- fitdist (data, distr = "gamma" , method = "mle" ) # Display the plot plot (ans) |
Output:
Example:
R
# Import the package library (fitdistrplus) # Generating 20 random values that uses a # gamma distribution having shape parameter # as 10 # combined with some gaussian noise data <- rgamma (20, 3, 10) + rnorm (20, 0, .02) # Fitting the dataset to a gamma distribution # with the help of mle ans <- fitdist (data, distr = "gamma" , method = "mle" ) # Display the summary of the ans summary (ans) # Display the plot plot (ans) |
Output: