[R] Optimizing nested function with nlminb()

Berend Hasselman bhh at xs4all.nl
Fri Nov 23 09:10:38 CET 2012


On 22-11-2012, at 22:55, Дмитрий Островский wrote:

> I am trying to optimize custom likelyhood with nlminb()
> Arguments h and f are meant to be fixed.
> 
> example.R:
> 
> compute.hyper.log.likelyhood <- function(a, h, f) {
>  a1 <- a[1]
>  a2 <- a[2]
>  l <- 0.0
>  for (j in 1:length(f)) {
>    l <- l + lbeta(a1 + f[j], a2 + h - f[j]) - lbeta(a1, a2)
>  }
>  return(l)
> }
> 
> compute.optimal.hyper.params <- function(start, limits, h_, f_) {
>  result <- nlminb(start,
>                   compute.hyper.log.likelyhood,
>                   h=h_,
>                   f=f_,
>                   scale = -1,
>                   lower = c(limits[1],limits[1]),
>                   upper = c(limits[2],limits[2]))
>  return (result[[1]])
> }
> 
> Console launch:
> 
> source('~/Desktop/Dropbox/example.R')
> h <- 1000
> start <- c(3,3)
> limits <- c(0.01,100)
> f <- c(40,30,50)
> compute.optimal.hyper.params(start,limits,h,f)
> 
> 
> produces the following:
> 
> Error in a2 + h : 'h' is missing
> 
> 
> Could you please explain me why?


You appear to being bitten by partial argument matching.
See section 8.1.20 of the R inferno (http://www.burns-stat.com/pages/Tutor/R_inferno.pdf).

Either call nlminb with an explicit hessian=NULL or change the name of argument  h in compute.hyper.log.likelyhood to something else such as hv.

Berend




More information about the R-help mailing list