####Lack of Fit Test data = read.table('bank.txt',header=F) y = data[,2] x = data[,1] Reduced=lm(y~x) Full=lm(y~as.factor(x)-1) anova(Reduced, Full) ####Box-Cox transformation n = 1000 x = rnorm(n) eps = rnorm(n)/10 sy = x + eps + 6 y = sy^2 logk2 = mean(log(y)) k2 = exp(logk2) lamList = seq(from=-2, to=2, by=0.25) sseList = rep(0,length(lamList)) for(i in 1:length(lamList)){ lam = lamList[i] k1 = 1/lam/k2^(lam-1) if(lam == 0) w = k2*log(y) if(lam != 0) w = k1*(y^lam-1) fit = lm(w~x) sseList[i] = sum((w-fitted(fit))^2) } plot(lamList,sseList) lamList[which.min(sseList)] ###best transformation parameter ###direct function library(MASS) fit = lm(y~x) a = boxcox(fit) a$x[which.max(a$y)] ###best transformation paramete