[Rcpp-devel] Best Practice for Optimize Functions in RCPP

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 23 13:30:56 CET 2014


On Tue, Dec 23, 2014 at 12:04 AM, Simon Riddell <simonruw at gmail.com> wrote:
> Hello,
>
> I have been judiciously using RCPP for six months, and have had my numerous
> questions answered so far from previous threads. I have now run into an
> issue I cannot get a handle on:
>
> I have converted a fairly large (recursive) Kalman filter function from R to
> C++ using RCPP. This function is then called within the R optim() function,
> subject to constraints, which estimates a set of ~30 parameters.
>
> My issue occurs within the C++ Kalman Filter function, where, I use the
> optimize() function to calculate the yield to maturity rate of various
> bonds. I do this by calling back to the R environment to access the
> optimize() function. Below is the R Function I create to be used within the
> Kalman filter, and below this R function is my method for calling it within
> the C++ code. To complicate matters further, the R Function calls a C++
> Function. To clarify: The Kalman Filter C++ code calls an R function, and
> this R Function calls an additional separate C++ function. (Code included
> below)
>
> As I iterate the Kalman filter it runs perfectly for anywhere from 30
> minutes to six hours (and produces correct output when matched to the R
> code), but it inevitably crashes. From past reading I have done, Dirk has
> before mentioned that calling an R function many times within C++ is usually
> not a good idea. As a result I suspect this is my issue (the error codes
> vary, sometimes mentioning numerical errors, other times recursive errors,
> other times random RCPP error codes -- I can document and provide them if
> needed)
>
> My biggest impasse is I cannot figure out a way to complete this without
> calling the R optimize() function, and cannot find any RCPP optimize
> functions to use instead. Thank you for reading.
>

Try this code from R itself:

http://www.sourcecodebrowser.com/r-base-core-ra/1.1.1/fmin_8c_source.html


More information about the Rcpp-devel mailing list