[R] Could I use R function lm or nlm in C code?

Dirk Eddelbuettel edd at debian.org
Sat Apr 23 15:41:11 CEST 2011

On 23 April 2011 at 18:25, Shuguang Sun wrote:
| I'm doing some simulation work and it takes me a lot of time to do it
| in R. So I try to implement it in C code, but I want to use some R
| functions directly for my lazy and the robustness of code. For
| example, I will use lm and nlm in my program. How could I use R's lm
| and nlm function directly? I thinks these functions are not included
| in the R's include directory. Do I need to get full R's source first?
| If I have get all R's source, is it enough that I only include the
| source file for function lm or nlm?

The CRAN packages RcppArmadillo and RcppGSL both implement a function
fastLM() that does just that: faster linear model fits.

Two caveats, though.

First, these functions are provided to illustrate how to do something like
this more easily in C/C++ from R thanks to Rcpp and Rcpp extensions. But as
Doug Bates will remind you, one should not necessarily do linear model fits
this way R does internally is different, and more robust in corner cases.
See help(fastLM) in RcppArmadillo for a worked example.

Second, as Gabor said, there are also easier way to get faster lm fits so
make sure you have those options exhausted.


Gauss has an Erdos number of -1.
   -- #63 at http://www.gaussfacts.com

More information about the R-help mailing list