About nlminb function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

About nlminb function

Freddy Hernández
Hello

I want to use the nlminb function but I have the objective function like characters. I can summarize the problem using the first example in the nlminb documentation.

x <- rnbinom(100, mu = 10, size = 10)
hdev <- function(par)   -sum(dnbinom(x, mu = par[1], size = par[2], log = TRUE))
nlminb(c(9, 12), objective=hdev)

With the last instructions we obtain appropriate results. If I have the name of the objective function stored in the element name, how can modify the next two instructions to obtain the same above results?

name <- 'hdev'
nlminb(c(9, 12), objective=name)

Thanks for the reply.

Freddy.
Reply | Threaded
Open this post in threaded view
|

Re: About nlminb function

Jorge I Velez
Dear Freddy,

Thank you for the explanation and the reproducible example.   You can use
get() as follows:

nlminb(c(9, 12), objective=get(name))

Regards,
Jorge.-


On Wed, Jul 4, 2012 at 12:37 PM, Freddy Hernández <> wrote:

> Hello
>
> I want to use the nlminb function but I have the objective function like
> characters. I can summarize the problem using the first example in the
> nlminb documentation.
>
> x <- rnbinom(100, mu = 10, size = 10)
> hdev <- function(par)   -sum(dnbinom(x, mu = par[1], size = par[2], log =
> TRUE))
> nlminb(c(9, 12), objective=hdev)
>
> With the last instructions we obtain appropriate results. If I have the
> name
> of the objective function stored in the element name, how can modify the
> next two instructions to obtain the same above results?
>
> name <- 'hdev'
> nlminb(c(9, 12), objective=name)
>
> Thanks for the reply.
>
> Freddy.
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/About-nlminb-function-tp4635421.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: About nlminb function

Rui Barradas
In reply to this post by Freddy Hernández
Hello,

Try


makeFunction <- function(x) eval( parse(text=x) )

name <- 'hdev'
nlminb(c(9, 12), objective = makeFunction(name))

Hope this helps,

Rui Barradas

Em 04-07-2012 17:37, Freddy Hernández escreveu:

> Hello
>
> I want to use the nlminb function but I have the objective function like
> characters. I can summarize the problem using the first example in the
> nlminb documentation.
>
> x <- rnbinom(100, mu = 10, size = 10)
> hdev <- function(par)   -sum(dnbinom(x, mu = par[1], size = par[2], log =
> TRUE))
> nlminb(c(9, 12), objective=hdev)
>
> With the last instructions we obtain appropriate results. If I have the name
> of the objective function stored in the element name, how can modify the
> next two instructions to obtain the same above results?
>
> name <- 'hdev'
> nlminb(c(9, 12), objective=name)
>
> Thanks for the reply.
>
> Freddy.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/About-nlminb-function-tp4635421.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.