Generalized Method of Moments (GMM) in R (Part 2 of 3): The Weird Behaviors of optim() and Why You Should Avoid It

Nan Wang

Hatched by Nan Wang

Nov 30, 2023

4 min read

0

Generalized Method of Moments (GMM) in R (Part 2 of 3): The Weird Behaviors of optim() and Why You Should Avoid It

In the previous article, we explored the concept of Generalized Method of Moments (GMM) and its implementation in R. We discussed how GMM can be used to estimate parameters in econometric models by matching the theoretical moments with the sample moments. However, in this article, we will delve into the peculiar behaviors of the optim() function in R and highlight why it is highly recommended to avoid using it in the context of GMM.

Before we dive into the weird behaviors of optim(), let's briefly recap the GMM estimation procedure. GMM relies on the principle that the sample moments should be close to the theoretical moments. This is achieved by minimizing an objective function that measures the distance between the sample and theoretical moments. The optim() function in R is commonly used to find the parameter values that minimize this objective function.

However, while optim() may seem like a convenient choice for GMM estimation, it has some inherent issues that can lead to unreliable results. One of the main issues is the sensitivity of optim() to the choice of starting values. Depending on the initial values given to the function, it may converge to different local minima, resulting in different parameter estimates.

This sensitivity to starting values can be quite problematic, as it introduces an element of randomness into the estimation process. Researchers often expect estimation methods to provide consistent results when applied to the same dataset, but optim() fails to meet this expectation due to its reliance on starting values.

Another peculiar behavior of optim() is its tendency to get stuck in flat regions of the objective function. This means that even with a well-defined and well-behaved objective function, optim() may struggle to converge to the global minimum and instead settle for a suboptimal solution. This can significantly affect the accuracy and reliability of the estimated parameters, undermining the whole purpose of GMM estimation.

To illustrate the weird behaviors of optim() in R, let's consider a simple example. Suppose we have a dataset with a single independent variable, X, and a dependent variable, y. We want to estimate the coefficient of X using GMM. In this case, the objective function can be defined as:

β = (X′X)^(−1)X′y

E[( ˆβ − β)( ˆβ − β)′] = (X′X)^(−1)X′(σ^2I)X(X′X)^(−1) = σ^2I(X′X)^(−1)X′X(X′X)^(−1) = σ^2(X′X)^(−1)

Assuming no autocorrelation, X′ee′X is a consistent (but not unbiased) estimator of X′E[≤≤′]X.

Now, let's see how optim() performs in this scenario. We will use the coef() function to obtain the estimated coefficients and multiply them by 0.01 for simplicity:

optim_result <- optim(fn = function(x) coef(x)*0.01, par = c(1), method = "BFGS")  
estimated_coefficient <- optim_result$par  

Upon running this code, we may obtain different estimated coefficients depending on the starting value given to optim(). This highlights the sensitivity of optim() and the potential for inconsistent results.

Given these weird behaviors of optim(), it is highly recommended to avoid using it for GMM estimation. Instead, there are alternative methods that can provide more reliable and consistent results. Here are three actionable pieces of advice to consider:

  1. Use alternative optimization algorithms: Instead of relying solely on optim(), explore other optimization algorithms that are more robust and less sensitive to starting values. Some popular options include the Nelder-Mead algorithm and the simulated annealing algorithm. These algorithms can help mitigate the issues associated with optim() and provide more stable estimates.

  2. Implement robust estimation techniques: GMM estimation can be enhanced by incorporating robust estimation techniques that are less affected by outliers and influential observations. One such technique is the M-estimation method, which minimizes a robust objective function based on weighted residuals. By using robust estimation techniques, you can obtain more reliable parameter estimates that are less influenced by the peculiar behaviors of optim().

  3. Consider using specialized packages: Instead of relying on the base R function optim(), consider using specialized packages that are specifically designed for GMM estimation. These packages often include built-in algorithms and tools that address the issues of optim() and provide more robust estimation results. Examples of such packages include the "gmm" package and the "systemfit" package.

In conclusion, while GMM is a powerful estimation technique, caution must be exercised when using the optim() function in R. Its weird behaviors, such as sensitivity to starting values and getting stuck in suboptimal solutions, can undermine the reliability of GMM estimation results. By exploring alternative optimization algorithms, implementing robust estimation techniques, and using specialized packages, researchers can overcome these issues and obtain more accurate and consistent parameter estimates in GMM estimation.

Sources:

  • "Generalized Method of Moments (GMM) in R (Part 2 of 3)"
  • "matrix_OLS_NYU_notes.pdf"

Sources

← Back to Library

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)

Start Hatching 🐣