1

I'm trying to accomplish something fairly simple. I have a data table created as follows

z = data.table(id = c('a'),x = 1:4,y=c(48,80,44,34))
z
id x  y
1:  a 1 48
2:  a 2 80
3:  a 3 44
4:  a 4 34

Next, I've created a function that does the Box-Cox transform from the package MASS using the function boxcox. The function is shown below.

fn.lm = function(x,y){ 
 fit = lm(y~x)
 z = boxcox(fit)
 lambda = z$x[which.max(z$y)]
 return(lambda)
}

Finally, I want to apply this function to all id's in the data table z as follows.

z[,list(slope = fn.lm(x,y)),by=id]

But when I run this last step I get the following error

Error in eval(expr, envir, enclos) : object 'y' not found

I suspect it has something to do with environments as it says it can't find y. But how do I fix this? Any help appreciated. Thanks in advance.

broccoli
  • 4,738
  • 10
  • 42
  • 54
  • 2
    This is a problem with `boxcox`, not data table. I can replicate the problem with `local({x <- 1:4;y <- c(48,80,44,34);fn.lm(x, y)})`. The problem may actually be with `update.default`, which does: `eval(call, parent.frame())`, despite documentation stating: "Beware of the effect of lazy evaluation: these two functions [sys.parent, parent.frame] look at the call stack at the time they are evaluated, not at the time they are called. **Passing calls to them as function arguments is unlikely to be a good idea**." – BrodieG Mar 18 '14 at 18:58
  • Thanks for verifying and confirming my suspicion. I'm going ahead with a custom function to minimize the log likelihood. – broccoli Mar 19 '14 at 17:09

0 Answers0