[R] Compute in C a loop with the lm() function and back to R

From: Javier López de Lacalle Beltrán de Heredia <javlacalle_at_yahoo.es>
Date: Tue 20 Jun 2006 - 02:59:17 EST

I want to do the computations from a loop by means of a C routine that will be called from R. The lm() function is entailed in that loop.

As a guidance, I have taken the following example from http://www.math.mcgill.ca/steele/ (As far as my question is concerned, I get similar information from "writing R extensions" and the book "Econometrics in R" by G.V Farnsworth):

Let convolve.rfun an R function

convolve.rfun <- function(a,b)
  ab <- rep(0, length(a)+length(b)-1)
  for(i in seq(along=a)){
    for(j in seq(along=b))
      ab[i+j-1] <- ab[i+j-1] + a[i]*b[j]   }

then the following C code do the computations of convolve.rfun,

void convolve(double *a, int *na, double *b, int *nb, double *ab)
 int i, j, nab = *na + *nb - 1;

 for(i = 0; i < nab; i = i+1){
   ab[i] = 0.0;

 for(i = 0; i < *na; i = i + 1){
  for(j = 0; j < *nb; j = j + 1){
    ab[i + j] = ab[i+j] + a[i] * b[j];

which after being compiled (R CMD SHLIB convolve.c) can be called from R:

conv <- function(a, b){
  .C("convolve", as.double(a), as.integer(length(a)), as.double(b),


My question is: How should I write the corresponding C code if the lm() R-function is entailed in the computations of the loop in convolve.rfun() . How could I get the OLS estimates in C?. Is there any routine that I should include in the header of the C file? Where to find it?

Thank you.

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jun 20 03:07:11 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 20 Jun 2006 - 04:11:43 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.