Skip to content
Prev 45616 / 63421 Next

how to control the environment of a formula

On 13-04-19 8:41 AM, Therneau, Terry M., Ph.D. wrote:
Hmmm, it seems to me that your first paragraph contradicts your opinion. 
  If you set the environment of a formula to .GlobalEnv then suddenly 
the way that formula acts depends on all sorts of things that weren't 
there when it was created.

Attaching the formula at the time of creation of a formula means that 
the names within it refer to data that is currently in scope.  That's 
generally a good thing.  It means that code will act the same when you 
run it at the top level or in a function.

For example, consider this:

f <- function() {
    x <- 1:10
    x2 <- x^2
    y <- rnorm(10, mean=x2)
    formula <- y ~ x + x2
    formula
}

fit <- lm(f())
update(fit, . ~ . - x)


This code works fine, all because the formula keeps the environment 
where it was created.  If I modify it like this:

f <- function() {
    x <- 1:10
    x2 <- x^2
    y <- rnorm(10, mean=x2)
    formula <- y ~ x + x2
    environment(formula) <- .GlobalEnv
    formula
}

fit <- lm(f())
update(fit, . ~ . - x)


then I really have no idea what it will produce, because it depends on 
global variables y, x and x2, not the local ones created in the 
function.  If I'm lucky, I'll get an "object not found" error; if I'm 
not lucky, it'll just go find some other variables and use those.

Duncan Murdoch