r - Weird behaviour of the car::boxCox() function when wrap into a homemade function -
i'm trying wrap car::boxcox function homemade function can mapply list of datasets. i'm using boxcox function car package , not mass package because want use family="yjpower". problem weird , it's either fondamental don't understand or kind of bug. here reproducible example:
library(car) le.mod <- function(val.gold,val.bad){ donn <- data.frame(val.gold,val.bad) res.lm <- lm(val.gold ~ val.bad, data=donn) bcres <- boxcox(res.lm, family="yjpower", plotit=f) lambda <- bcres$x[which.max(bcres$y)] donn$val.bad.t <- donn$val.bad^lambda res.lm <- lm(val.gold ~ val.bad.t, data=donn) list(res.lm=res.lm, lambda = lambda) } xx <- runif(1000,1,100) xxt1 <- xx^0.6 + runif(1000,1,10) yy <- 2*xx + 10 + rnorm(1000,0,2) le.mod(yy,xxt1)
this gives me error message:
## error in is.data.frame(data) : object 'donn' not found
i pin-pointed problem line:
bcres <- boxcox(res.lm, family="yjpower", plotit=f)
boxcox suppose able take lm class object, doesn't find associated data created 2 lines before.
it works outside of function le.mod(). it's problem related environment management, boxcox fonction looking "donn" in global environment not finding , reason ignore not looking in function specific environment.
anybody have idea fix or explain me don't understand here? i've been turning head on problem days , can't working.
thanks
i've found answer (!), can't understand reason of behaviour if have explanation, don't hesitate post it.
the solution adding y=true
in second line of function:
res.lm <- lm(val.gold ~ val.bad, data=donn,y=true)
for reasons, allows throught.
Comments
Post a Comment