How to Fit a Gamma Distribution to a Dataset in R

R Guides

This tutorial explains how to fit a gamma distribution to a dataset in R.

Fitting a Gamma Distribution in R

Suppose you have a dataset that was generated using the approach below:

#generate 50 random values that follow a gamma distribution with shape parameter = 3
#and shape parameter = 10 combined with some gaussian noise
z <- rgamma(50, 3, 10) + rnorm(50, 0, .02)

#view first 6 values
head(z)
[1] 0.07730 0.02495 0.12788 0.15011 0.08839 0.09941

To see how well a gamma distribution fits this dataset z, we can use the fitdistrplus package in R:

#install 'fitdistrplus' package if not already installed
install.packages('fitdistrplus')

#load package
library(fitdistrplus)

The general syntax to use to fit a distribution using this package is:

fitdist(dataset, distr = “your distribution choice”, method = “your method of fitting the data”)

In this case, we will fit the dataset that we generated earlier using the gamma distribution and maximum likelihood estimation approach to fitting the data:

#fit our dataset to a gamma distribution using mle
fit <- fitdist(z, distr = "gamma", method = "mle")

#view the summary of the fit 
summary(fit)

This produces the following output:

Next, we can produce some plots that show how well the gamma distribution fits the dataset using the following syntax:

#produce plots
plot(fit)

This produces the following plots:

Here is the complete code we used to fit a gamma distribution to a dataset in R:

#install 'fitdistrplus' package if not already installed
install.packages('fitdistrplus')

#load package
library(fitdistrplus)

#generate 50 random values that follow a gamma distribution with shape parameter = 3
#and shape parameter = 10 combined with some gaussian noise
z <- rgamma(50, 3, 10) + rnorm(50, 0, .02)

#fit our dataset to a gamma distribution using mle
fit <- fitdist(z, distr = "gamma", method = "mle")

#view the summary of the fit
summary(fit)

#produce plots to visualize the fit
plot(fit)

Leave a Reply

Your email address will not be published. Required fields are marked *