Skip to content
Prev 118139 / 398498 Next

model.frame: how does one use it?

On 6/15/07, Dirk Eddelbuettel <edd at debian.org> wrote:
I don't know if ?model.frame is the best place page to look. There's a
more detailed description at

http://developer.r-project.org/nonstandard-eval.pdf

but here are the non-standard evaluation rules as I understand them:
given a name in either (1) the formula or (2) ``special'' arguments like
'weights' in this case, or 'subset', try to find the name

1. in 'data'
2. failing that, in environment(formula)
3. failing that, in the enclosing environment, and so on.

By 'name', I mean a symbol, such as 'Age' or 'myweight'.  So
basically, everything is as you would expect if the name is visible in
data, but if not, the search starts in the environment of the formula,
not the environment where the function call is being made (which is
the standard evaulation behaviour).  This is a feature, not a bug
(things would be a lot more confusing if it were the other way round).


With this in mind, either of the following might do what you want:

badFunction <- function(mydata, myformula) {
    mydata$myweight <- abs(rnorm(nrow(mydata)))
    hyp <-
        rpart(myformula,
              data=mydata,
              weights=myweight,
              method="class")
    prev <- hyp
}


badFunction <- function(mydata, myformula) {
    myweight <- abs(rnorm(nrow(mydata)))
    environment(myformula) <- environment()
    hyp <-
        rpart(myformula,
              data=mydata,
              weights=myweight,
              method="class")
    prev <- hyp
}

-Deepayan