How to Fit a Gamma Distribution to a Dataset in R
Last Updated :
18 Mar, 2022
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
data <- rgamma (20, 3, 10) + rnorm (20, 0, .02)
ans <- fitdist (data, distr = "gamma" , method = "mle" )
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
library (fitdistrplus)
data <- rgamma (20, 3, 10) + rnorm (20, 0, .02)
ans <- fitdist (data, distr = "gamma" , method = "mle" )
plot (ans)
|
Output:
Example:
R
library (fitdistrplus)
data <- rgamma (20, 3, 10) + rnorm (20, 0, .02)
ans <- fitdist (data, distr = "gamma" , method = "mle" )
summary (ans)
plot (ans)
|
Output:
Share your thoughts in the comments
Please Login to comment...