quickly extract response from formula

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

quickly extract response from formula

Andreas Leha
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: quickly extract response from formula

David Winsemius

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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: quickly extract response from formula

Andreas Leha
Hi David,

thanks for your quick answer!

David Winsemius <[hidden email]> writes:

> 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) }

This is what I'll be using.  Thanks again!

[...]

Regards,
Andreas

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: quickly extract response from formula

William Dunlap
You can bullet-proof it a bit by making sure that length(formula)==3
before assuming that formula[[2]] is the response.   If length(formula)==2
then there is no response term, only predictor terms.  E.g., replace
   resp <- frm[[2]]
with
   resp <- if (length(frm)==3) frm[[2]] else NULL
(or call stop(), or warning(), ...)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Andreas Leha
> Sent: Friday, November 01, 2013 2:50 PM
> To: [hidden email]
> Subject: Re: [R] quickly extract response from formula
>
> Hi David,
>
> thanks for your quick answer!
>
> David Winsemius <[hidden email]> writes:
>
> > 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) }
>
> This is what I'll be using.  Thanks again!
>
> [...]
>
> Regards,
> Andreas
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: quickly extract response from formula

Andreas Leha
William Dunlap <[hidden email]> writes:

> You can bullet-proof it a bit by making sure that length(formula)==3
> before assuming that formula[[2]] is the response.   If length(formula)==2
> then there is no response term, only predictor terms.  E.g., replace
>    resp <- frm[[2]]
> with
>    resp <- if (length(frm)==3) frm[[2]] else NULL
> (or call stop(), or warning(), ...)

Will do.  Thanks.

- Andreas

>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
>> -----Original Message-----
>> From: [hidden email] [mailto:[hidden email]] On Behalf
>> Of Andreas Leha
>> Sent: Friday, November 01, 2013 2:50 PM
>> To: [hidden email]
>> Subject: Re: [R] quickly extract response from formula
>>
>> Hi David,
>>
>> thanks for your quick answer!
>>
>> David Winsemius <[hidden email]> writes:
>>
>> > 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) }
>>
>> This is what I'll be using.  Thanks again!
>>
>> [...]
>>
>> Regards,
>> Andreas
>>
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.