# Trees data example in the MASS package

library(MASS)
head(trees)

mod.fit <- lm(formula = Volume ~ Height, data = trees)
summary(mod.fit)

#Plot of Y vs. x with estimated model
plot(x = trees$Height, y = trees$Volume, xlab = "Height", ylab = "Volume", main = "Volume vs. Height", 
       panel.first = grid(col = "gray", lty = "dotted"))
curve(expr = predict(object = mod.fit, newdata = data.frame(Height = x)),
        col = "red", lty = "solid", lwd = 1, add = TRUE, xlim = c(min(trees$Height), max(trees$Height)))

#e vs. yhat
plot(x = mod.fit$fitted.values, y = mod.fit$residuals, xlab = expression(hat(y)), ylab = "e",
     main = expression(paste("e vs. ", hat(y))), panel.first = grid(col = "gray", lty = "dotted"))
abline(h = 0, col = "red")

resid.set <- data.frame(trees, yhat = round(mod.fit$fitted.values,2), e = round(mod.fit$residuals,2))
resid.set
# Observation 20 is the (11.65, 13.25) on the plot
resid.set[mod.fit$fitted.values < 12,]


################################################################################
# Box-Cox

  # Find lambda.hat
  save.bc <- boxcox(object = mod.fit, lambda = seq(from = -2, to = 2, by = 0.01))
  lambda.hat <- save.bc$x[save.bc$y == max(save.bc$y)]
  lambda.hat
    
#####################################################################################
# Try transformation with lambda.hat

  mod.fit2 <- lm(formula = Volume^lambda.hat ~ Height, data = trees)
  summary(mod.fit2)
  # mod.fit$fitted.values^0.19

  plot(x = mod.fit2$fitted.values, y = mod.fit2$residuals, xlab = expression(hat(Y)^{-0.19}), ylab = "Residual", 
       main = expression(paste("Residuals vs. ", hat(Y)^{-0.19})), panel.first = grid(col = "gray", lty = "dotted"))
  abline(h = 0, col = "red")

#####################################################################################
# Try natural log transformation because 0 was in the interval

  mod.fit3 <- lm(formula = log(Volume) ~ Height, data = trees)
  summary(mod.fit3)

  plot(x = mod.fit3$fitted.values, y = mod.fit3$residuals, xlab = expression(log(hat(Y))), ylab = "Residual", 
       main = expression(paste("Residuals vs.", log(hat(Y)))), panel.first = grid(col = "gray", lty = "dotted"))
  abline(h = 0, col = "red")


#####################################################################################
# Perform hypothesis tests for nonconsant variance

  #Levene's test
  library(car)  
  group <- ifelse(test = trees$Height < median(trees$Height), yes = 1, no = 2)
  leveneTest(y = mod.fit$residuals, group = as.factor(group))

  # Breusch Pagan test
  library(lmtest)
  bptest(formula = Volume ~ Height, data = trees, studentize = FALSE)


  #Do same tests for transformed Y
  leveneTest(y = mod.fit2$residuals, group = as.factor(group))
  bptest(formula = Volume^lambda.hat ~ Height, data = trees, studentize = FALSE)
  leveneTest(y = mod.fit3$residuals, group = as.factor(group))
  bptest(formula = log(Volume) ~ Height, data = trees, studentize = FALSE)







#
