简体   繁体   中英

Optimize function with nlm and optim

I have the following function

f <- function(a,b) {
    (a - 1) + 3.2/b + 3*log(gamma(a)) + 3*a*log(b);
}

I want to optimized this function with nlm() ,optim() ect... .

I am trying to do that with:

value <- nlm(f,optim(f))

However, I get an error message. cannot coerce type 'closure' to vector of type 'double' I really would appreciate your answer!

It works ok for me using both optim and nlm . I did a little modification

f <- function(par) {
  a <- par[1]
  b <- par[2]
  (a - 1) + 3.2/b + 3*log(gamma(a)) + 3*a*log(b);
}

> optim(c(1, .3), fn=f)
$par
[1] 1.399685 0.762025

$value
[1] 3.09904

$counts
function gradient 
      55       NA 

$convergence
[1] 0

$message
NULL

> nlm(f, c(1,.3))
$minimum
[1] 3.09904

$estimate
[1] 1.3999515 0.7619307

$gradient
[1] 3.096044e-07 3.907985e-07

$code
[1] 1

$iterations
[1] 17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM