[R] quickly extract response from formula

David Winsemius dwinsemius at comcast.net
Thu Oct 31 22:57:44 CET 2013


On Oct 31, 2013, at 1:27 PM, Andreas Leha wrote:

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

If you want its expression value its just form[[2]]

If you wnat it evaluated in the environment of a dataframe then this should be fairly efficient:

x <- stats::runif(20)
y <- stats::runif(20)
dfrm <- data.frame(x=x,y=y)
extractResponse <- function(frm, dat) { resp <- frm[[2]]; print(resp) # that's optional
                                         fdat <- eval(resp, envir=dat); return(fdat) }
> extractResponse(y ~. , dat=dfrm)
y
 [1] 0.80458147 0.90447989 0.54874785 0.04227895 0.11540969 0.98003767 0.37372573 0.58013515
 [9] 0.47227247 0.22361616 0.45076628 0.57091106 0.36290661 0.69673890 0.87650224 0.96496587
[17] 0.14923759 0.25083936 0.32139801 0.91958308

> 
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list