[Rd] update.default: fall back on model.frame in case that the data frame is not in the parent environment

From: Thaler, Thorn, LAUSANNE, Applied Mathematics <Thorn.Thaler_at_rdls.nestle.com>
Date: Tue, 02 Aug 2011 13:48:00 +0200


Dear all,

Suppose the following code:

--------------8<--------------

mm <- function(datf) {
  lm(y ~ x, data = datf)
}

mydatf <- data.frame(x = rep(1:2, 10), y = rnorm(20, rep(1:2, 10)))

l <- mm(mydatf)
-------------->8--------------

If I want to update l now without providing the data argument an error occurs:

--------------8<--------------

> update(l, . ~ .)
Error in inherits(x, "data.frame") : object 'datf' not found
-------------->8--------------

and I've to provide the data argument explicitly:
--------------8<--------------

update(l, . ~ ., data = mydatf)
update(l, . ~ ., data = model.frame(l))
-------------->8--------------

While the first work-around is additionally error prone (what if I change the name of mydatf earlier in the file? In the best case I just get an error if mydatf is not defined), both options are kind of semantically questionable (I do not want to _update_ the data argument of the lm object it should remain untouched).

So my suggestion would be that update falls back on the data stored in model.frame in case that the data argument in the lm call cannot be resolved in the parent.frame of update, which can be easily achieved by adding just four lines to update.default:

--------------8<--------------

update.default <- function (object, formula., ..., evaluate = TRUE) {

    call <- object$call
    if (is.null(call))

        stop("need an object with call component")     extras <- match.call(expand.dots = FALSE)$...     if (!missing(formula.))

        call$formula <- update.formula(formula(object), formula.)     if (length(extras)) {

        existing <- !is.na(match(names(extras), names(call)))
        for (a in names(extras)[existing]) call[[a]] <- extras[[a]]
        if (any(!existing)) {
            call <- c(as.list(call), extras[!existing])
            call <- as.call(call)
        }

    }
    if (!is.null(call$data)) {
        if (!exists(as.character(call$data), envir = parent.frame()))
            call$data <- model.frame(object)
    }
    if (evaluate)

        eval(call, parent.frame())
    else call
}
-------------->8--------------

This is just a quick dirty hack which works fine here (with an ugly drawback that in the standard output of lm I now see the lengthy explicit data.frame statement) but I'm sure there are some cracks out there who could take it over from here and beautify this idea.

I don't see any problems with this proposition regarding old code, but if I'm wrong and there are some reasons not to touch update.default in the way I was proposing please let me know. Any other feedback is highly appreciated too.

Thanks for sharing your thoughts with me.

KR,

-Thorn



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 02 Aug 2011 - 12:08:33 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 02 Aug 2011 - 14:20:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive