Hi All, I am trying to do an ML estimation in R. My likelihood function has several nested loops and so it takes a lot of time (days when I use the genetic algorithm for optimization) for the optimization to finish. Unable to avoid loops, I am thinking of writing the likelihood function in C++ and calling it from within R when using *optim()*. I found that one can call C functions (once they have been compiled) from within R with> dyn.load("file.so")and> .C("function", ...)Can the same be done for C++ code? Deepankar
Yes, this can be done and the call is the same, however the C++ needs to be indicated as <external c>, see details in the writing R extensions manual. hth, Ingmar On 6 Nov 2007, at 17:05, Deepankar Basu wrote:> Hi All, > > I am trying to do an ML estimation in R. My likelihood function has > several nested loops and so it takes a lot of time (days when I use > the > genetic algorithm for optimization) for the optimization to finish. > Unable to avoid loops, I am thinking of writing the likelihood > function > in C++ and calling it from within R when using *optim()*. I found that > one can call C functions (once they have been compiled) from within R > with > >> dyn.load("file.so") > > and > >> .C("function", ...) > > Can the same be done for C++ code? > > > Deepankar > > ______________________________________________ > R-help@r-project.org 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.Ingmar Visser Department of Psychology, University of Amsterdam Roetersstraat 15 1018 WB Amsterdam The Netherlands t: +31-20-5256723 [[alternative HTML version deleted]]
On Tue, 6 Nov 2007, Deepankar Basu wrote:> Hi All, > > I am trying to do an ML estimation in R. My likelihood function has > several nested loops and so it takes a lot of time (days when I use the > genetic algorithm for optimization) for the optimization to finish. > Unable to avoid loops, I am thinking of writing the likelihood function > in C++ and calling it from within R when using *optim()*. I found that > one can call C functions (once they have been compiled) from within R > with > >> dyn.load("file.so") > > and > >> .C("function", ...) > > Can the same be done for C++ code?Yes, and the manual about C ('Writing R Extensions') discusses C++ as well. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Deepankar Basu wrote:> > I am trying to do an ML estimation in R. My likelihood function has > several nested loops and so it takes a lot of time (days when I use the > genetic algorithm for optimization) for the optimization to finish. > Unable to avoid loops, I am thinking of writing the likelihood function > in C++ and calling it from within R when using *optim()*. I found > that one can call C functions (once they have been compiled) from > within R with > > > dyn.load("file.so") > > and > > > .C("function", ...) > > Can the same be done for C++ code? >If it can be done in C, it probably (P > 99%) can be done in C++, because C++ can call C functions. Just write the C++ code as: extern "C" int myfunction(int x1, int x2) { // write C++ code here return rval; } Alberto Monteiro
Thanks for the help. On Tue, 2007-11-06 at 14:48 -0200, Alberto Monteiro wrote:> Deepankar Basu wrote: > > > > I am trying to do an ML estimation in R. My likelihood function has > > several nested loops and so it takes a lot of time (days when I use the > > genetic algorithm for optimization) for the optimization to finish. > > Unable to avoid loops, I am thinking of writing the likelihood function > > in C++ and calling it from within R when using *optim()*. I found > > that one can call C functions (once they have been compiled) from > > within R with > > > > > dyn.load("file.so") > > > > and > > > > > .C("function", ...) > > > > Can the same be done for C++ code? > > > If it can be done in C, it probably (P > 99%) can be done in C++, > because C++ can call C functions. Just write the C++ code as: > > extern "C" int myfunction(int x1, int x2) > { > // write C++ code here > return rval; > } > > Alberto Monteiro >