[Rcpp-devel] Evaluating Formula's in Rcpp

Dirk Eddelbuettel edd at debian.org
Sat May 4 15:15:31 CEST 2013


Gabriel,

On 3 May 2013 at 21:56, Dirk Eddelbuettel wrote:
| On 3 May 2013 at 22:04, Gabriel Hoffman wrote:
| | Hi,
| |      I any trying to develop an R function for running 1000's of 
| | regressions very fast.  I will omit the technical reasons for this, but 
| | I would like to write code to perform the following:
| | 
| | for(j in 1:ncol(X) ){
| |      fit = myRegression( y ~ age:X[,j] )
| | }
| | 
| | This uses R's convenient 'formula' functionality to evaluate the 
| | interaction term in the regression.
| 
| Interesting. 
| 
| I am not sure you can.  You probably have to look at the code for formula(),
| model.matrix(), ... and redo it. Which will be a royal pain.

One alternative, in case you really just need the age:X[,j] term (and an
intercept), is to create it on the fly.

Create a new C++ matrix 'X' of a column of ones and the product of age and
X[,j], then submit it to fastLm as X along with a proper y.  That will be
very fast as you bypass formula evaluation, plus you access the faster
regression code.

Just be careful and check your result to make sure lm() and the formula
interface agree.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list