[R] MASS::boxcox "object not found"

Jinsong Zhao j@zh@o @ending from ye@h@net
Sun Aug 5 14:36:21 CEST 2018

Hi there,

I wrote a function that wraps MASS::boxcox as:

bc <- function(vec) {
   lam <- boxcox(lm(vec ~ 1))
   lam <- lam$x[which.max(lam$y)]
   (vec^lam - 1)/lam

When I invoke it as:

> x <- runif(20)
> bc(x)
Error in eval(predvars, data, env) : object 'vec' not found

I have googled, and rewrote the above function as:

bc <- function(vec) {
   dat <<- data.frame(vec = vec)
   lam <- boxcox(lm(vec ~ 1, dat))
   lam <- lam$x[which.max(lam$y)]
   rm(dat, envir = .GlobalEnv)
   (vec^lam - 1)/lam

It works. But, I am wondering why MASS::boxcox have to wrap in such way that have to use the data in .GlobalEnv.

	[[alternative HTML version deleted]]

More information about the R-help mailing list