title: R maximum likelihood algorithm
body: <p>I have to build a maximum likelihood estimator from scratch, no built-in mle function allowed.
  I am maximizing a random variable but not w.r.t. the mean or the variance, I have another parameter (the beta from a moving average (1) process).
  so here is the code I have built, but it tells me that the "optimize" function cannot find the initial values. I have the observations in a matrix. Rows are different observations, the columns are different simulations (that is why I have a loop at the beginning, running from 1 to R=10000).
  Help MUCH appreciated :)</p>
  
  <pre><code>for (i in 1:R) {
  llh_regr &lt;- function (β, y, z) {
    kappa &lt;- -((N-1)*log(pi*2)+(N-1)*log(1))/2
    residuals &lt;- (y-c-β*z)
    loglik &lt;- kappa-(1/2)*crossprod(residuals, residuals)
    return(loglik)
                                 }
  
  targetfunction &lt;- function(β) {
    return(-llh_regr(β, y = x[,1], z = u_lag[,1]))
           }
    B[1,i] &lt;- optim(runif(1), targetfunction)
  }
  </code></pre>