kayp
kayp

Reputation: 1

Error in optimize in R for generating distribution parameters for a log-likelihood function

I am trying to calculate distribution parameters to calculate log-likelihood values. Whilst "optim" seems to work for the function defined below, it obviously changes the output every time I change the starting value. I am trying to use "optimize" instead but I keep getting this error message

Error in if (par[1] < 0 | par[2] < 0) { : 
  missing value where TRUE/FALSE needed

I am not sure how to move forward with this and all possible ways of fixing this error on the internet do not seem to be working. Please help, thank you in advance!


optim_cBeta <- function(par) 
{
   


  if ( par[1]< 0 | par[2] < 0)                 

  {
    output_cBeta = -Inf


  } else {

    
    output_cBeta = ( qbeta(0.025,par[1],par[2]) - 0.06)^2 +

                   ( qbeta(0.5,par[1],par[2]) - 0.07 )^2

                   + ( qbeta(0.975,par[1],par[2]) - 0.08 )^2
  
  
  }  

  print(c(output_cBeta, par[1],par[2]))
   
  return(output_cBeta)
    
}

optimize(optim_cBeta, lower = c(0,120), upper = c(100,3000), maximum = FALSE)

I tried to define par[1] = a, par[2] = b inside the function, and replaced those in the objective function "output_cBeta", but keep getting the same error.

Upvotes: 0

Views: 106

Answers (1)

Erwin Kalvelagen
Erwin Kalvelagen

Reputation: 16797

See https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html: you need x0 in the call to optimize.

Upvotes: 0

Related Questions