Strangely different results doing Box Cox transform with R Optim and with MASS package

39 views Asked by At

using optim in R, We don't uderstand why We have not the same result than lambda Box Cox in Mass package ? Is the problem whith the linear model ?

Please find the code below.

Do you have any idea?

Thanks a lot for your help. Have a nice day.

#generating sample
n=100
bUn= runif(n, min = 2, max = 8)
dat=bUn


max.LL <- function(data, par)
{
  print(par)
  BC=(data^par-1)/par
  res=lm(BC ~ 1)
  LL=logLik(res)[1]
  print(-LL)
  return(-LL)
}

result <- optim(par = 3, fn = max.LL, data = dat, method = "Brent",lower = -2, upper = 2)
result

library(MASS)
lambda = boxcox(lm(bUn ~ 1))
lambda <- lambda$x[which.max(lambda$y)]
lambda
0

There are 0 answers