library(MASS) attach(wtloss) # #----------------LINEAR MODEL------------------------------------------ # par(pty = "s", oma = c(0., 0., 4., 0.)) par(mfrow = c(1, 1)) plot(Days, Weight, xlab = "Days", ylab = "Weight (kg)") par(mfrow = c(2, 2)) lin.lm <- lm(Weight ~ Days, model = T) print(summary(lin.lm, correlation = F)) plot(lin.lm,which=1:2) matplot(Days, cbind(lin.lm$fitted, Weight), type = "lp", pch = "*") # print(anova(lin.lm, update(lin.lm, ~ . + I(Days^2)), update(lin.lm, ~ . + I(Days^2) + I(Days^3)), update(lin.lm, ~ . + I(Days^2) + I( Days^3) + I(Days^4)))) #--------------FORWARD SELECTION----------------------------------------------- # step.lm <- step(lin.lm, scope = list(upper = ~ . + I(Days^2) + I(Days^ 3) + I(Days^4) + I(Days^5)), direction = "forward", trace = F) print(step.lm$anova) # #--------------STEPWISE SELECTION----------------------------------------------- # step.lm <- step(lin.lm, scope = list(upper = ~ . + I(Days^2) + I(Days^3) + I(Days^4) + I(Days^5)), direction = "both", trace = F) print(step.lm$anova) # #----------------CUBIC MODEL------------------------------------------------ # par(mfrow = c(2, 2)) c.lm <- update(lin.lm, ~ . + I(Days^2) + I(Days^3), model = T) print(summary(c.lm, correlation = F)) plot(c.lm,which=1:2) matplot(Days, cbind(c.lm$fitted, lin.lm$fitted, Weight), type = "llp", pch = "*") boxcox(c.lm,lambda=seq(0,5,.1)) par(mfrow=c(2,2)) c2.lm <- update(c.lm, Weight^2 ~ .,model=T) print(summary(c2.lm, correlation = F)) plot(c2.lm,which=1:2) matplot(Days, cbind(sqrt(c2.lm$fitted), c.lm$fitted, Weight), type = "llp", pch = "*")