8

I'm going to find the parameters for a rank-logit model. But the error always shows that there are non-finite finite-difference value. If I change the "b0<-rep(0,5)" to "b0<-rep(-1,5)", the number after non-finite finite-difference value changes from 2 to 1. If you need the dataset, I will send it to you by email.

cjll <- function(b){

U <- X%*%b
lSU <- csm%*%exp(U)
   lSU <- (lSU!=0)*lSU+(lSU==0)
LL <- sum(Ccsm%*%U-log(lSU))

return(LL)
}

b0 <- rep(0,5)
res <- optim(b0,cjll,method="BFGS",hessian=TRUE,control=list(fnscale=-1))
#Error in optim(b0, cjll, method = "BFGS", hessian = TRUE, control = list(fnscale = -1)) :
#  non-finite finite-difference value [2]

b <- res$par
#Error: object 'res' not found
thelatemail
  • 91,185
  • 12
  • 128
  • 188
Jiawen Jiang
  • 109
  • 1
  • 1
  • 6
  • I tried it, the value changed from 2 to 1 but still unsolved 'res<-optim(b0,cjll,method="Nelder-Mead",hessian=TRUE,control=list(fnscale=-1)) Error in optim(b0, cjll, method = "Nelder-Mead", hessian = TRUE, control = list(fnscale = -1)) : non-finite finite-difference value [1]' – Jiawen Jiang Jan 28 '15 at 05:47

1 Answers1

11

BFGS requires the gradient of the function being minimized. If you don't pass one it will try to use finite-differences to estimate it. Looking at your likelihood function, it could be that the fact that you "split" it by elements equal to 0 and not equal to 0 creates a discontinuity that prevents the numerical gradient from being properly formed. Try using method = "Nelder-Mead" and setting Hessian to FALSE and see if that works. If it does, you can then use the numDeriv package to estimate the gradient and Hessian at the point of convergence if you need them.

Avraham
  • 1,655
  • 19
  • 32
  • It works!!!! thank you so much! I will take a look at the numDeriv package! Thanks a lot – Jiawen Jiang Jan 28 '15 at 05:51
  • Now I can get the value of b , but when I try to print tval, it appears error like this> tval<-b/sqrt(-diag(solve(res$hessian))) Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), : 'data' must be of a vector type, was 'NULL' – Jiawen Jiang Jan 28 '15 at 05:55
  • @JiawenJiang, since Hessian is false, there is no res$Hessian. You have to estimate it analytcially. The `hessian` function in the [numDeriv](http://cran.r-project.org/web/packages/numDeriv/index.html) package *may* be able to help you. It may well be that your likelihood function is discontinuous enough that there is no valid Hessian at the point of convergence. You'll have to try it. – Avraham Jan 28 '15 at 05:58
  • 1
    If using the `fitdistrplus` package you can change the optimization method by `fitdist(data, "gamma", optim.method="Nelder-Mead")`. The help for the `fitdistr` method has more info. – Bar Jun 29 '16 at 21:27