title: Optimization a function in R
body: <p>I have a data set like this:</p>
  
  <pre><code>x=c(5, 11, 21, 31, 46, 75, 98, 122, 145, 165, 196,
      224, 245, 293, 321, 330, 350, 420)
  </code></pre>
  
  <p>I wrote a function as follows:</p>
  
  <pre><code>f.val=function(theta, x){
      a = theta[1]
      b=theta[2]
      c=theta[3]
      d=theta[4]
      n=nrow(x)
      value= sum(log(a*b*(a*x)^(b-1)+c*d*(c*x)^(d-1)))-a*sum(x^b)-c*sum(x^d)
      return(-value)
  }
  </code></pre>
  
  <p>and then I would like to maximise the function <code>f.val</code> and then get the estimation of <code>a, b, c, d</code>. I used optim function with lower and upper bounds in <code>R</code>. I know that the value of <code>b</code> should be bigger than <code>1</code> and the value <code>d</code> should be less that <code>1</code>. Also, the value of <code>a</code>and <code>b</code> should be bigger than zero. I applied the following function but I could not get the result :</p>
  
  <pre><code>&gt; optim(c(0, 0, 0, 0), f.val, x=x, method="L-BFGS-B", lower=c(0, 1, 0, 0),
   + upper=c(rep(Inf, 3), 1))
  
  Error in optim(c(0, 0, 0, 0), f.val, x = x, method = "L-BFGS-B", lower =c(0,  :  L-BFGS-B needs finite values of 'fn'
  </code></pre>
  
  <p>I try the following values but still can not get the correct values:</p>
  
  <pre><code>optim(c(1e-6, 1e-6, 1e-6, 1e-6), f.val, x=x, method="L-BFGS-B", lower=c(1e-6, 1, 1e-6, 1e-6), upper=c(rep(10000, 3), 1))
  
  
   $par
   [1] 0.0000010 1.0000000 0.0333163 0.5695943
  
   $value
   [1] 91.17824
  
   $counts
   function gradient 
    60       60 
  
   $convergence
   [1] 0
  
   $message
   [1] "CONVERGENCE: REL_REDUCTION_OF_F &lt;= FACTR*EPSMCH"
  </code></pre>
  
  <p>How can I solve this error and get the following result:</p>
  
  <pre><code>a = 0 (approximately); b = 2.2589; c = 0.0183 and d = 0.7266
  </code></pre>