[Rcpp-devel] Evaluating Formula's in Rcpp
Gabriel Hoffman
gabriel.hoffman at gmail.com
Sat May 4 04:04:52 CEST 2013
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.
The issue is that 'myRegression' is very complicated, high overhead, and
takes over arguments which I have omitted for simplicity. Therefore, I
would like to pass the formula "y ~ age:X[,j]" into a Rcpp function, and
construct the relevant matrices in C++ using Rcpp::Environment, and
Rcpp::Language, where I change the value of j each time. Because, this
would require only one entry into my C++ code, I would not have to incur
the overhead each time. I would like to run my analysis with a call like:
# return p-values from fitting ncol(X) regressions
myRegressionWrapper( y ~ age:X[,j], data=X)
or something like this.
Essentially I would to have the nice functionality of lm() in Rcpp to
evaluate:
mf <- match.call(expand.dots = FALSE)
m <- match(c("formula", "data", "subset", "weights", "na.action",
"offset"), names(mf), 0L)
mf <- mf[c(1L, m)]
mf$drop.unused.levels <- TRUE
mf[[1L]] <- as.name("model.frame")
mf <- eval(mf, parent.frame())
y <- model.response(mf, "numeric")
mt <- attr(mf, "terms")
X <- model.matrix(mt, mf, contrasts)
so I can run my custom regression function on y and X quickly for each
value of j.
Do you know how to implement the this functionality in Rcpp or through
some other method?
Thanks,
Gabriel Hoffman, PhD
Biological Statistics and Computational Biology
Cornell University
More information about the Rcpp-devel
mailing list