[R] Calling optim and .C

Rolf Turner r.turner at auckland.ac.nz
Thu Nov 6 03:05:56 CET 2008


On 6/11/2008, at 11:39 AM, Armin Meier wrote:

> Hi all,
> I want to optimize a function fn using optim. This function fn calls
> the .C function in it with a function name and arguments given to fn,
> i.e. something like this pseudocode:
>
> fn <- function(par, "some params for fn") {... .C 
> ("Cfunction", ...) ...}
> optim(par, fn, "some params for fn")
>
> In my case this doesn't work, but is it possible in general and I made
> some error?

Yes it is possible in general, so you've goofed somewhere in your code.
Join the club.

The optim() function doesn't care how ``fn'' does its calculations as  
long
as it does them and doesn't crash.  In particular it doesn't care  
whether
``fn'' calls .C() to do all or part of those computations.

If ``fn'' works on its own (i.e. when not called by optim()) then it  
would
seem that the problem is not with your call to .C() but with the  
syntax of
your call to optim().

Remember that the ... arguments must be given in the name=value form.

Remember that optim() usually doesn't like it if fn() returns Inf or  
NA or NaN.

That's about all I can come up with in the way of ideas.  Good luck.

	cheers,

		Rolf Turner

P. S. ``... this doesn't work ...'' is too vague to be useful.  In  
what way
doesn't it work?  What sort of error do you get?  If you give the  
information
that the Posting Guide calls for, you may get a more helpful answer.

		R. T.

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list