[R] Optimizing nested function with nlminb()

Дмитрий Островский cherhavla at gmail.com
Thu Nov 22 22:55:26 CET 2012


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?

R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i686-pc-linux-gnu (32-bit)

--
Best regards,
Dmitry



More information about the R-help mailing list