Understanding the Error in R's MLE Function: A Step-by-Step Guide to Removing Missing Values

Understanding the Error in R’s MLE Function

In this article, we will delve into the error encountered while using the mle function in R to perform Maximum Likelihood Estimation (MLE). We will explore the background of the problem, analyze the provided code, and examine possible solutions.

Background: Negative Likelihood Function

The likelihood function is a crucial concept in statistical inference. It measures the probability of observing data given a set of parameters. The negative log-likelihood function is often used as an alternative to the likelihood function due to its simpler form.

The negative likelihood function for our problem can be represented as:

- sum(log(pdf_yt))

where pdf_yt is the probability density function (PDF) of a normal distribution with mean mu and standard deviation sd.

Code: Likelihood Function

Here’s an example implementation of the likelihood function in R:

likelihood.func <- function(theta, kappa, sigma, rt){
  n <- NROW(rt)
  y <- rt[2:n,]
  dt <- 1/12
  mu <- rt[1:(n-1),]* exp(-kappa*dt) + theta* (1 - exp(-kappa*dt))
  sd <- sqrt((sigma^2)*(1 - exp(-2*kappa*dt))/(2*kappa))
  pdf_yt <- dnorm(y, mu, sd, log=FALSE)
  
  -sum(log(pdf_yt))
}

Code: MLE Command

The mle function in R is used to perform Maximum Likelihood Estimation. Here’s an example of how to use it with the likelihood function:

theta.est <- 0.04
kappa.est <- 0.5
sigma.est <- 0.02

library(stats4)
bound.lower <- parameters.est * 0.1
bound.upper <- parameters.est * 2

est.mle <- mle(likelihood.func, start = list(theta=theta.est, kappa=kappa.est, sigma=sigma.est),
               method="L-BFGS-B", lower = bound.lower, upper = bound.upper, fixed=list(rt = rt))

Error: Missing Value

The error encountered in the mle function is:

Error in if (!all(lower[is.fixed] <= fixed[is.fixed] &amp; fixed[is.fixed] <= : 
  missing value where TRUE/FALSE needed

This error occurs when there are missing values in the input data that cannot be compared with the fixed parameters.

Solution: Removing rt as Fixed Parameter

One possible solution is to remove rt as the fixed parameter. Here’s an updated implementation of the likelihood function:

LL2 <- function(theta, kappa, sigma){
  likelihood.func(theta,kappa,sigma)
}

And here’s the updated mle command:

est.mle <- mle(minuslogl = LL2, 
               start = list(theta=theta.est, kappa=kappa.est, sigma=sigma.est), 
               method = "L-BFGS-B", 
               lower = bound.lower, 
               upper = bound.upper
               )

By removing rt as the fixed parameter, we can avoid the error caused by missing values.

Conclusion

In this article, we explored the error encountered while using the mle function in R to perform Maximum Likelihood Estimation. We analyzed the provided code, identified the source of the error, and examined possible solutions. By removing rt as the fixed parameter, we can avoid the error caused by missing values.

Further Reading

For more information on Maximum Likelihood Estimation, refer to the following resources:

  • “Maximum Likelihood Estimation” by David V. Lindley
  • “Maximum Likelihood Methods in Estimation” by Lawrence D. Brown and Carl R. Johnson

Note: The code snippets provided are for demonstration purposes only and may not be suitable for production use without further modifications and testing.


Last modified on 2023-09-12