 # Simulation - examine normality for epsilon

################################################################
# One data set

  # Explanatory variables that contribute to the value of y
  set.seed(7129)
  n <- 10
  # n <- 100
  # n <- 1000
  x1 <- round(runif(n = n, min = 1, max = 4), digits = 1)
  x2 <- round(rnorm(n = n, mean = 3, sd = 0.5), digits = 1)
  beta0 <- -5
  beta1 <- 2
  beta2 <- 2

  # Explanatory variable that does not contribute to the value of y
  set.seed(1238)
  x3 <- round(rnorm(n = n, mean = 5, sd = 1), digits = 1)
  beta3 <- 0

  # Variance of epsilon
  sigma <- 1 # standard deviation
  epsilon <- rnorm(n = n, mean = 0, sd = sigma)
  # set.seed(7382)
  # epsilon <- rexp(n = n)  # Examine nonnormality

  # Form y = beta0 + beta1*x1 + beta2*x2 + beta3*x3 + epsilon
  #   While rounding should not be done, it is here so that
  #   future displays of calculations are easier.
  y <- round(beta0 + beta1*x1 + beta2*x2 + beta3*x3 + epsilon, digits = 2)

  set1 <- data.frame(x1, x2, x3, y)
  set1

  # Estimate model
  mod.fit <- lm(formula = y ~ x1 + x2 + x3, data = set1)
  summary(mod.fit)


################################################################
# Plots

  # Histogram of residuals
  hist(x = mod.fit$residuals, main = "Histogram of residuals", xlab = "Residuals", freq = FALSE)
  curve(expr = dnorm(x, mean = mean(mod.fit$residuals), sd = sd(mod.fit$residuals)),
        col = "red", add = TRUE)

  # QQ-Plot of residuals
  prob <- seq(from = 1/(n+1), to = 1-1/(n+1), by = 1/(n+1))
  norm.quant <- qnorm(p = prob, mean = mean(mod.fit$residuals), sd = sd(mod.fit$residuals))
  # Examine what will be plotted
  norm.set <- data.frame(residuals = sort(mod.fit$residuals),
                         prob = prob, norm.quant = norm.quant)
  norm.set[prob > 0.495 & prob < 0.505,]
  # head(norm.set)
  # tail(norm.set)
  plot(x = sort(mod.fit$residuals), y = norm.quant, main = "QQ plot",
     xlab = "Residuals", ylab = "Normal quantiles")
  abline(a = 0, b = 1)

  # Other ways to do a QQ-plot that are similar but not exactly the same
  qqnorm(mod.fit$residuals)
  qqline(mod.fit$residuals)


################################################################
# When epsilon <- rexp(n = n) is used to obtain nonnormality

  # Simulate data again
  set.seed(7129)
  n <- 1000
  x1 <- round(runif(n = n, min = 1, max = 4), digits = 1)
  x2 <- round(rnorm(n = n, mean = 3, sd = 0.5), digits = 1)
  beta0 <- -5
  beta1 <- 2
  beta2 <- 2

  # Explanatory variable that does not contribute to the value of y
  set.seed(1238)
  x3 <- round(rnorm(n = n, mean = 5, sd = 1), digits = 1)
  beta3 <- 0

  # epsilon
  set.seed(7382)
  epsilon <- rexp(n = n)

  # Form y = beta0 + beta1*x1 + beta2*x2 + beta3*x3 + epsilon
  y <- round(beta0 + beta1*x1 + beta2*x2 + beta3*x3 + epsilon, digits = 2)

  set1 <- data.frame(x1, x2, x3, y)
  head(set1)

  # Estimate model - log transformation
  mod.fit2 <- lm(formula = log(y) ~ x1 + x2 + x3, data = set1)
  summary(mod.fit2)

  # Histogram of residuals - This is better but still not good enough
  hist(x = mod.fit2$residuals, main = "Histogram of residuals", xlab = "Residuals", freq = FALSE)
  curve(expr = dnorm(x, mean = mean(mod.fit2$residuals), sd = sd(mod.fit2$residuals)),
        col = "red", add = TRUE)

  # Determine lambda.hat
  save.bc2 <- boxcox(object = mod.fit2, lambda = seq(from = -10, to = 10, by = 0.01))
  lambda.hat2 <- save.bc2$x[save.bc2$y == max(save.bc2$y)]
  lambda.hat2

  # Estimate model - log transformation
  mod.fit3 <- lm(formula = y^lambda.hat2 ~ x1 + x2 + x3, data = set1)
  summary(mod.fit3)

  # Histogram of residuals - This is better but still not good enough
  hist(x = mod.fit3$residuals, main = "Histogram of residuals", xlab = "Residuals", freq = FALSE)
  curve(expr = dnorm(x, mean = mean(mod.fit3$residuals), sd = sd(mod.fit3$residuals)),
        col = "red", add = TRUE)











