|
|
Box-Cox transformation
function(g, i=seq(-2,2,by=0.1)) { y <- g$fit + g$res x <- model.matrix(g) qrf <- lsfit(x, y, int=F)$qr sly <- sum(log(y)) n <- length(y) ll <- rep(0, length(i)) for(j in 1:length(i)) { l <- i[j] if(l == 0) ty <- log(y) else ty <- (y^l - 1)/l cat(dim(qrf$qr$qr)) res <- qr.resid(qrf, ty) ll[j] <- ( - n * log(sum(res^2)/n))/2 + (l - 1) * sly } list(lambda = i, loglik = ll) }Try it out on the savings dataset:
> a <- boxcox(g) > motif() > plot(a$lambda,a$loglik,type="l") > abline(h=max(a$loglik - 0.5*qchisq(0.95,1)))
> plot(a$lambda,a$loglik,type="l") > abline(h=max(a$loglik - 0.5*qchisq(0.95,1))) |