[R] Help with integrating R and c/c++

Douglas Bates bates at stat.wisc.edu
Sun Feb 6 18:04:31 CET 2011


On Sun, Feb 6, 2011 at 10:56 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Feb 6, 2011, at 10:28 AM, Rohit Pandey wrote:
>
>> Hi,
>>
>> I have been using R for close to two years now and have grown quite
>> comfortable with the language. I am presently trying to implement an
>> optimization routine in R (Newton Rhapson). I have some R functions that
>> calculate the gradient and hessian (pre requisite matrices) fairly
>> efficiently. Now, I have to call this function iteratively until some
>> convergance criterion is reached. I think the standard method of doing
>> this
>> in most programming languages is a while loop. However, I know R can get
>> pretty slow when you use loops. In order to make this efficient, I want to
>> transfer this part of my code to a more efficient programming language
>> like
>> c++ or c. However, I have been trying to learn this all day without any
>> luck. I found a package called Rcpp that makes this easier. However, it
>> seems some functional knowledge of writing R packages is a pre requisite.
>> I
>> tried to follow the standard manual for doing this, but could not find a
>> simple example to get me started. I know I am supposed to make a cpp file
>> and put it some where before it can be called from R, but I'm confused as
>> to
>> how this can be done.
>
> Dirk Eddelbuettel has 8 vignettes linked from his Rcpp page:
>
> http://dirk.eddelbuettel.com/code/rcpp.html
>
> And reading further you will see about 20 packages written with Rcpp.

Including one called RcppDE which does exactly what you want to do,
implementing an optimizer in C++ to be called from R.

Another package to look at is RcppArmadillo which provides a linkage
between Rcpp and the Armadillo C++ linear algebra library (although it
still doesn't do as much as I would like it to for writing
optimizers).

> And there is a mailing list link which I had originally sought on the main
> Mailing List page (unsuccessfully):
>
> http://lists.r-forge.r-project.org/pipermail/rcpp-devel/
>
> --
> David.
>>
>> My requirement is to start with a parameter vector, update it according to
>> the gradient and hessian, check if the parameter satisfies some
>> convergance
>> criterion and continue doing this until it does. Is there a way to
>> efficiently do this through an R function (replicate?). The problem is
>> that
>> the number of iterations is not fixed. If there is no function in R, is
>> there a way I can quickly use Rcpp or some thing to have this last part of
>> my code in a C or C++ program which repeatedly calls my R functions for
>> updating the parameters?
>>
>> --
>> Thanks in advance,
>> Rohit
>> Mob: 91 9819926213
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list