title: Manual Maximum-Likelihood Estimation of an AR-Model in R
body: <p>I am trying to estimate a simple AR(1) model in R of the form <b>y[t] = alpha + beta * y[t-1] + u[t] </b> with u[t] being normally distributed with mean zero and standard deviation sigma.</p>
  
  <p>I have simulated an AR(1) model with <b>alpha = 10</b> and <b>beta = 0.1</b>:</p>
  
  <pre><code>library(stats)
  data&lt;-arima.sim(n=1000,list(ar=0.1),mean=10)
  </code></pre>
  
  <p>First check: OLS yields the following results:</p>
  
  <pre><code>lm(data~c(NA,data[1:length(data)-1]))
  
  Call:
  lm(formula = data ~ c(NA, data[1:length(data) - 1]))
  
  Coefficients:
                  (Intercept)  c(NA, data[1:length(data) - 1])  
                     10.02253                          0.09669  
  </code></pre>
  
  <p>But my goal is to estimate the coefficients with ML. My negative log-likelihood function is:</p>
  
  <pre><code>logl&lt;-function(sigma,alpha,beta){
  -sum(log((1/(sqrt(2*pi)*sigma)) * exp(-((data-alpha-beta*c(NA,data[1:length(data)-1]))^2)/(2*sigma^2))))
  }
  </code></pre>
  
  <p>that is, the sum of all log-single observation normal distributions, that are transformed by u[t] = y[t] - alpha - beta*y[t-1]. The lag has been created (just like in the OLS estimation above) by c(NA,data[1:length(data)-1]).</p>
  
  <p>When I try to put it at work I get the following error:</p>
  
  <pre><code>library(stats4)
  mle(logl,start=list(sigma=1,alpha=5,beta=0.05),method="L-BFGS-B")
  Error in optim(start, f, method = method, hessian = TRUE, ...) : 
  L-BFGS-B needs finite values of 'fn'
  </code></pre>
  
  <p>My log-likelihood function must be correct, when I try to estimate a linear model of the form <b>y[t] = alpha + beta * x[t] + u[t]</b> it works perfectly.</p>
  
  <p>I just do not see how my initial values lead to a non-finite result? Trying any other initial values does not solve the problem.</p>
  
  <p>Any help is highly appreciated!</p>