This function is used to give the maximum likelihood fitting of univariate distribution and defined under the MASS package.
In R, the fitdistr() function is part of the MASS package and is used for fitting univariate distributions to data. It is particularly useful when you want to estimate the parameters of a probability distribution based on observed data. Here’s an explanation of the fitdistr() function:
Syntax:
fitdistr(x, densfun, start, …)
x: This is the data vector for which you want to fit a probability distribution.
densfun: This parameter specifies the probability density function (PDF) that you want to fit to the data. You need to provide the name of the density function as a character string. Common options include “normal” for the normal distribution, “gamma” for the gamma distribution, “poisson” for the Poisson distribution, etc.
start: This is an optional parameter that specifies the initial values for the parameters of the distribution. It helps the optimization algorithm converge to a solution faster.
…: Additional arguments that can be passed to the optimization function. These are optional and depend on the specific distribution being fitted.
Example:
# Assuming ‘data’ is your observed data vector
library(MASS)
# Fit a normal distribution to the data
fit <- fitdistr(data, “normal”)
# Display the estimated parameters
print(fit)
In this example, fitdistr() is used to fit a normal distribution to the observed data. The result (fit) contains the estimated parameters of the normal distribution.
Keep in mind that the success of fitting depends on the choice of the distribution and the quality of the data. Additionally, you might need to install and load the MASS package before using fitdistr() if you haven’t done so already:
install.packages(“MASS”)
library(MASS)