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.