title: MLE for multiple parameters in R
body: <p>I wrote the following <strong>R</strong> function and I need to estimate the parameters using <strong>MLE</strong> in two cases. It is given below.</p>
  
  <p>Case 1: The choice <code>theta1=1</code>, to find 3 parameters: <code>lambda, p, theta2</code> <br />
  Case 2: The choice <code>theta1=1, p=1</code>, to find 2 parameters: <code>lambda, theta2</code> <br /></p>
  
  <p>I have copied the <strong>R</strong> code below.</p>
  
  <pre><code>x &lt;- rpois(100, 5)
  MLE &lt;- function(x, p, theta1, theta2, lambda){
  numerator &lt;- p*theta1*theta2*dpois(x, lambda)*ppois(x, lambda)^(theta1 -1)*(1-ppois(x, lambda)^theta1)^(theta2-1)
  denominator &lt;- (1 - (1-p)*((1-ppois(x, lambda)^theta1)^theta2))^2
  result = numerator/denominator
  return(result)
  }
  </code></pre>
  
  <p>Could you please give an idea? Thank you very much.</p>
  
  <p>I also wrote another code to solve the above problem. But its giving me an error. I have copied that code below too.</p>
  
  <pre><code>theta2 &lt;- 1
  p &lt;- 1
  x &lt;-  rpois(200, lambda = 4)
  pfun &lt;- function(lambda, theta1){
    n &lt;- 200
    x.poi &lt;- n*log(p*theta1*theta2) + (theta1-1)*sum(log(ppois(x, lambda)))+
            (theta2-1)*sum(log(1-ppois(x,lambda)^theta1))+sum(log(dpois(x,lambda)))-
                2*sum(log(1-(1-p)*(1-ppois(x,lambda)^theta1)^theta2))
  }
  
  out = optim(par=c(100,10), fn=pfun, data=x, maximum = T)
  out
  est1 &lt;- mle(minuslog=pfun, start = list(lambda = 2, theta1 = 1),method = "BFGS")
  summary(est1) 
  </code></pre>
  
  <p>I rewrote the above code in the following way. But it still produce some errors. I have copied them below. I really appreciate any kind of advice in this regards.</p>
  
  <p>Here, I fixed the parameter <strong>p = 1</strong> and estimated other three parameters such that <strong>theta1, theta2, lambda</strong>.</p>
  
  <pre><code>library(maxLik)
  extendDistr &lt;- function(x, p, theta1, theta2, lambda, log = FALSE) {
    numerator &lt;- p * theta1 * theta2 * dpois(x, lambda) * ppois(x,
                                                                lambda)^(theta1 - 1) * (1 - ppois(x, lambda)^theta1)^(theta2 -
                                                                                                                        1)
    denominator &lt;- (1 - (1 - p) * ((1 - ppois(x, lambda)^theta1)^theta2))^2
    pdf &lt;- numerator/denominator
    if (log)
      pdf &lt;- log(pdf)
    return(pdf)
  }
  
  set.seed(650)
  x &lt;- rpois(100, 2)
  
  llf &lt;- function(param) {
    theta1 &lt;- param[1]
    theta2 &lt;- param[2]
    lambda &lt;- param[3]
    return(sum(extendDistr(x, p = 1, theta1 = theta1, theta2 = theta2,
                           lambda = lambda, log = TRUE)))
  }
  ml &lt;- maxLik(llf, start = c(theta1 = 1, theta2 = 1, lambda = 1))
  print(summary(ml))
  
  --------------------------------------------
  Maximum Likelihood estimation
  Newton-Raphson maximisation, 8 iterations
  Return code 3: Last step could not find a value above the current.
  Boundary of parameter space?  
  Consider switching to a more robust optimisation method temporarily.
  Log-Likelihood: 115.1142 
  3  free parameters
  Estimates:
         Estimate Std. error t value Pr(&gt; t)    
  theta1  2.59598         NA      NA      NA    
  theta2  0.08531    0.00853      10  &lt;2e-16 ***
  lambda  0.02565         NA      NA      NA    
  ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  --------------------------------------------
  Warning messages:
  1: In sqrt(diag(vc)) : NaNs produced
  2: In sqrt(diag(vc)) : NaNs produced
  </code></pre>