Let's say we have a set of data points that we believe are drawn from a normal distribution. We can use maximum likelihood estimation (MLE) to estimate the parameters of the normal distribution, namely the mean and variance.
The likelihood function of a normal distribution is given by:
L(μ, σ²; x) = (1 / (2πσ²))^(n/2) * exp(-(x - μ)² / (2σ²))
where:
- μ is the mean of the distribution
- σ² is the variance of the distribution
- x is a vector of data points
- n is the number of data points
The goal of MLE is to find the values of μ and σ² that maximize the likelihood function. This can be done using a variety of methods, including numerical optimization.
In R, we can use the `optim()` function to maximize the likelihood function. The `optim()` function takes a function as its first argument, and the parameters to be optimized as its second argument. In our case, the function we want to maximize is the log-likelihood function of the normal distribution.
The following code shows how to maximize the likelihood function of a normal distribution in R:
# Define the log-likelihood function
logLik <- function(mu, sigma2, x) {
n <- length(x)
return(-(n / 2) * log(2 * pi * sigma2) - (sum((x - mu)^2) / (2 * sigma2)))
}
# Set the initial values of the parameters
mu <- 0
sigma2 <- 1
# Maximize the log-likelihood function
optimResult <- optim(par = c(mu, sigma2), fn = logLik, x = x)
# Print the maximum likelihood estimates
print(optimResult$par)
This code will print the maximum likelihood estimates for the mean and variance of the normal distribution.
- The `logLik()` function is the log-likelihood function of the normal distribution.
- The `optim()` function is used to maximize the log-likelihood function.
- The `par` argument to the `optim()` function is a vector of the initial values of the parameters.
- The `fn` argument to the `optim()` function is the function to be maximized.
- The `x` argument to the `logLik()` function is a vector of data points.
- The `optimResult$par` output is a vector of the maximum likelihood estimates for the parameters.