[R] quickly extract response from formula

Andreas Leha andreas.leha at med.uni-goettingen.de
Thu Oct 31 21:27:55 CET 2013


Hi all,

what is the recommended way to quickly (and without much burden on the
memory) extract the response from a formula?

The standard way to extract the response from a formula seems to be via
model.frame() or model.extract(), but that is very memory intensive.

Here is a quick example, that (BEWARE) consumes a lot of memory:

--8<---------------cut here---------------start------------->8---
require("ALL")
data("ALL")
y <- pData(ALL)$sex
x <- t(exprs(ALL))
mf <- cbind(as.data.frame(x), y=y)

extractResponse <- function(formula, data)
{
  m <- match.call(expand.dots = FALSE)
  m[[1L]] <- quote(stats::model.frame)
  m <- eval.parent(m)
  y <- model.extract(m, "response")

  y
}
extractResponse(y~., data=mf)

extractResponseFast <- function(formula, data)
{
  y <- eval(as.symbol(as.character(formula)[2]),
            environment(formula))

  y
}
extractResponseFast(y~., data=mf)
--8<---------------cut here---------------end--------------->8---



Or, to put my question differently, is the following approach
robust?

--8<---------------cut here---------------start------------->8---
require("ALL")
data("ALL")
y <- pData(ALL)$sex
x <- t(exprs(ALL))
mf <- cbind(as.data.frame(x), y=y)

extractResponseFast <- function(formula, data)
{
  y <- eval(as.symbol(as.character(formula)[2]),
            environment(formula))

  y
}
extractResponseFast(y~., data=mf)
--8<---------------cut here---------------end--------------->8---


Many thanks in advance!

Regards,
Andreas



More information about the R-help mailing list