0

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
user3619937
  • 85
  • 2
  • 6

0 Answers0