title: optim in r :non finite finite difference error
body: <p>I have a simple likelihood function (from a normal dist with mean=0) that I want to maximize. optim keeps giving me this error:
  Error in optim(par = phi, fn = loglike, estimates = estimates, NULL, hessian = TRUE,  :   non-finite finite-difference value [1]</p>
  
  <p>Here is my data and likelihood function:</p>
  
  <pre><code>y =  [ -0.01472  0.03942  0.03592 0.02776 -0.00090 ]
  
  C = a varcov matrix:
  
    1.66e-03 -0.000120 -6.780e-06  0.000102 -4.000e-05
  
   -1.20e-04  0.001387  7.900e-05 -0.000140 -8.000e-05
  
   -6.78e-06  0.000079  1.416e-03 -0.000070  8.761e-06
  
    1.02e-04 -0.000140 -7.000e-05  0.001339 -6.000e-05
  
   -4.00e-05 -0.000080  8.761e-06 -0.000060  1.291e-03
  </code></pre>
  
  <p>my log likelihood function is:
  lglkl =  -.5*(log(det(v)) + (t(y)%<em>%vi%</em>%y))` where v = phi*I + C and vi=inverse(v) and I= 5*5 Identity matrix.</p>
  
  <p>I am trying to get the mle estimate for "phi". I thought this would be a simple optimization problem but am struggling. Would really appreciate any help. Thanks in advance. My code is below:</p>
  
  <pre><code>loglike &lt;- function(phi,y) {
  
  v = phi*I + C
  vi = solve(v)
  loglike = -.5*(log(det(v)) + (t(y)%*%vi%*%y))
  return(-loglike)
  }
  
  phi = 0
  parm &lt;- optim(par=phi,fn=loglike,y=y,NULL,hessian = TRUE, method="L-BFGS-B",lower=0,upper=1000)
  </code></pre>
  
  <blockquote>
    <p></p>
  </blockquote>