title: AR(1) with Nonlinear Least Squares using Marquardt Algorithm: EViews vs R
body: <p>Some weeks ago, I <a href="https://stackoverflow.com/questions/12805793/ols-estimation-with-ar1-term">posted</a> a poorly thought-out question plagued with vague information. This is my attempt to correct the original question and get better answers.</p>
  
  <p>The main <strong>problem</strong> is:  I cannot obtain similar parameter estimates with EViews and R.</p>
  
  <p>For reasons I do not know myself, I need to estimate parameters for certain data using EViews. This is done by picking the NLS (nonlinear least squares) option and using the following formula: <code>indep_var c dep_var ar(1)</code></p>
  
  <p>EViews claims that they estimate linear AR(1) processes such as:</p>
  
  <blockquote>
    <p>Y<sub>t</sub> = a + B * X<sub>t</sub> + u<sub>t</sub></p>
  </blockquote>
  
  <p>where u<sub>t</sub> errors are defined as </p>
  
  <blockquote>
    <p>u<sub>t</sub> = p * u<sub>t-1</sub> + e</p>
  </blockquote>
  
  <p>by using an equivalent equation (with some algebraic substitutions):</p>
  
  <blockquote>
    <p>Y<sub>t</sub> = (1 - p) * a + p * Y<sub>t - 1</sub> + B *
    X<sub>t</sub> - p * B *  X<sub>t - 1</sub> + e<sub>t</sub></p>
  </blockquote>
  
  <p>Furthermore, <a href="http://forums.eviews.com/viewtopic.php?f=4&amp;t=3768" rel="nofollow noreferrer">this thread over at the EViews forums</a> suggests that their NLS estimations are generated by the Marquardt algorithm.</p>
  
  <p>Now, the go-to R function to estimate AR(1) processes is <code>arima</code>. However, there are two problems: 1) the estimates are maximum likelihood estimates; 2) the intercept estimate <a href="http://www.stat.pitt.edu/stoffer/tsa2/Rissues.htm" rel="nofollow noreferrer">is not actually the intercept estimate</a>.</p>
  
  <p>Therefore, I turned to the <code>nlsLM</code> function from the minpack.lm package. This function uses the Marquardt algorithm to achieve nonlinear least squares estimates, which should yield the same results as the EViews implementation (or very similar ones, at least).</p>
  
  <p>Now the code. I have a data frame (<code>data</code>) with an independent variable and a dependent variable such as the one generated by the following code:</p>
  
  <pre><code>data &lt;- data.frame(independent = abs(rnorm(48)), dependent = abs(rnorm(48)))
  </code></pre>
  
  <p>To estimate parameters in the equation EViews claims to estimate (3<sup>rd</sup> one on this post), I use the following commands:</p>
  
  <pre><code>library(minpack.lm)
  result &lt;-
  nlsLM(dependentB ~ ((1 - theta1) * theta2) + (theta1 * dependentA) +
                      (theta3 * independentB) - (theta1 * theta3 * independentA),
  data = list(dependentB = data$dependent[2:48], dependentA = data$dependent[1:47],
     independentB = data$independent[2:48], independentA = data$independent[1:47]),
  start = list(theta1 = -10, theta2 = -10, theta3 = -10)
  )
  </code></pre>
  
  <p>Unfortunately, the estimates output by <code>nlsLM</code> are not close to the ones output by EViews. Do you have any idea of what might be causing this? Or maybe my code is wrong?</p>
  
  <p>Finally, I would like to say that I personally am an R user - that is exactly why I'm trying to do this in R instead of EViews. I would also love to provide you the data I'm working with but it's impossible since it's confidential data.</p>