# Year: 2016
# Module: R Stats
# Title: Visualising multiple regression
# Author: Jacob Cybulski 
#
# Data: http://archive.ics.uci.edu/ml/datasets/Automobile
# Data consist of the following entities: 
# (a) the specification of an auto in terms of various characteristics, 
# (b) its assigned insurance risk rating, i.e.
#     degree to which the auto is more risky than its price indicates
# (c) its normalized losses in use as compared to other cars, i.e.
#     the relative average loss payment per insured vehicle year.
# We will be predicting price based on car specifications

##### Activate libraries

# install.packages("Hmisc", dependencies = TRUE)
# install.packages("psych", dependencies = TRUE)
# install.packages("car", dependencies = TRUE)
# install.packages("ggplot2", dependencies = TRUE)
# install.packages("rgl", dependencies = TRUE)
library(Hmisc)
library(psych)
library(car)
library(ggplot2)
library(rgl)

##### Develop the model created in the previous lesson - quickly

### Set a working directory and read the Automobile.csv data set
setwd("C:/Users/jacob_000/Dropbox/Dev/R Projects/R Intro")
setwd("C:/MyDocs/Dropbox/Dev/R Projects/R Intro")
auto <- read.csv(file = "Data/automobile.csv", header=TRUE, na.strings="?")

### Eliminate missing values
auto$Price <- as.numeric(impute(auto$Price, mean))
auto$Normalized.losses <- as.numeric(impute(auto$Normalized.losses, mean))
auto$Num.of.doors <- as.numeric(impute(auto$Num.of.doors, median))
auto$Horsepower <- as.numeric(impute(auto$Horsepower, mean))
auto$Peak.rpm <- as.numeric(impute(auto$Peak.rpm, mean))
auto$Bore <- as.numeric(impute(auto$Bore, mean))
auto$Stroke <- as.numeric(impute(auto$Stroke, mean))

### Select variables for regression modelling
auto.sel <- subset(auto, select = c(Horsepower, City.mpg, Peak.rpm, Curb.weight, Num.of.doors, Price))

### Transform non-linear vars
auto.sel$Price <- log10(auto.sel$Price)
auto.sel$Horsepower <- log10(auto.sel$Horsepower)

### Split data into training and validation samples
set.seed(2017)
train.size <- 0.8 
train.index <- sample.int(length(auto.sel$Price), round(length(auto.sel$Price) * train.size))
train.sample <- auto.sel[train.index,]
valid.sample <- auto.sel[-train.index,]

### Eliminate observations known to be extreme for the final linear model
train.sample <- train.sample[-which(rownames(train.sample)
   %in% c("74", "91", "167", "22", "114", "197", "35", "44", "125")),]  

### Create a model (6) which avoids multi-collinearity and checks the p-values
fit <- lm(Price ~ Curb.weight+Peak.rpm, data=train.sample)
summary(fit) # R2=87.7%
vif(fit)

# At this point we finished and validated the model with correlation^2, RME and MAE
# Training set: 0.8836 2670.0000 1759.0000
# Validation set: 0.7396 5723.0000 3334.0000

##### Now explore the model and results (we will be using auto.sel)

auto.explore <- subset(auto.sel, select=c(Peak.rpm, Curb.weight, Price))
pairs.panels(auto.explore, col="red")

#### Let us see if Peak.rpm could help Curb.weight to boost Prices and how

# Define level of Price reliance on Curb.weight 
mstats <- summary(auto.explore$Peak.rpm)
mstats
PrpmMin = as.numeric(mstats[1]) # Just want numbers not data frames
Prpm1Q = as.numeric(mstats[2])
PrpmMedian = as.numeric(mstats[3])
Prpm3Q = as.numeric(mstats[5])
PrpmMax = as.numeric(mstats[6])

# Generate some x-axis points in Horsepower range
cwstats <- summary(auto.explore$Curb.weight)
cwstats
CWRange = seq(from = 1400, to = 4100, by = 100)

# let us try two different linear models
fit <- lm(Price ~ Curb.weight+Peak.rpm, data=train.sample)
fit <- loess(Price ~ Curb.weight+Peak.rpm, data=train.sample)

# Predict Prices at different Curb.weight levels
PredPriceMin = predict(fit, newdata=data.frame(Peak.rpm=PrpmMin, Curb.weight=CWRange))
PredPrice1Q = predict(fit, newdata=data.frame(Peak.rpm=Prpm1Q, Curb.weight=CWRange))
PredPriceMedian = predict(fit, newdata=data.frame(Peak.rpm=PrpmMedian, Curb.weight=CWRange))
PredPrice3Q = predict(fit, newdata=data.frame(Peak.rpm=Prpm3Q, Curb.weight=CWRange))
PredPriceMax = predict(fit, newdata=data.frame(Peak.rpm=PrpmMax, Curb.weight=CWRange))

# Plot it in transformed Price units (recall log10 transform)
ggplot(auto.explore, aes(x=Curb.weight, y=Price)) + 
  ggtitle("Influence of Curb Weight and Peak RPM on Price\n(Multiple regression)") +
  labs(x="Curb Weight + Peak RPM Levels (lb)", y="Price (log10 $)") +
  xlim(min(CWRange), max(CWRange)) +
  geom_point(color="gray", size=1, pch=19) +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=PredPriceMin), color="blue") +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=PredPrice1Q), color="green") +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=PredPriceMedian), color="yellow") +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=PredPrice3Q), color="darkorange") +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=PredPriceMax), color="red")

# Plot it in $ Price units - does Curb.weight really matter?
ggplot(data.frame(Curb.weight=auto.explore$Curb.weight, Price=10 ^ auto.explore$Price), 
       aes(x=Curb.weight, y=Price)) + 
  ggtitle("Influence of Curb Weight and Peak RPM on Price\n(Multiple regression)") +
  labs(x="Curb Weight + Peak RPM Levels (lb)", y="Price ($)") +
  xlim(min(CWRange), max(CWRange)) +
  geom_point(color="gray", size=1, pch=19) +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=10 ^ PredPriceMin), color="blue") +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=10 ^ PredPrice1Q), color="green") +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=10 ^ PredPriceMedian), color="yellow") +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=10 ^ PredPrice3Q), color="darkorange") +
  geom_line(data=data.frame(Curb.weight=CWRange, Price=10 ^ PredPriceMax), color="red")

# Consider glm and gls, especially for logistic regression

##### Coplots
#     As we can see a single 2D plot is hampering our ability to see the details
#     Plot with Curb.weight variable as conditioning var
#     We plot with Curb.weight in different ranges and multiple plots

# Add regression lines to the plot
panel.lm = function(x, y, ...) {
  tmp <- lm(y ~ x, na.action = na.omit)
  if(length(x)>4) {
    points(x, y, ...)
    abline(tmp, col="blue")
    panel.smooth(x, y, col.smooth="red", ...)
  } else {
    points(x, y, ...)
  }
}

# Plot with panels featuring regression and loess lines
coplot(Price ~ Curb.weight | Peak.rpm, data=auto.explore, 
       col="orange", panel=panel.lm)


##### Let's try some really cool multiple regression 3D plots

### Let us check the impact of variable selection on the model
scatter3d(x=auto$Curb.weight, z=auto$Peak.rpm, y=auto$Price)
scatter3d(x=auto$Horsepower, z=auto$City.mpg, y=auto$Price)
scatter3d(x=auto$Horsepower, z=auto$Curb.weight, y=auto$Normalized.losses)

### Compare the effect of data cleanup on the model, use only training data

# Raw data but without missing values
scatter3d(x=auto[train.index,]$Curb.weight, 
          z=auto[train.index,]$Peak.rpm, 
          y=auto[train.index,]$Price)

# Data with transformed variables
scatter3d(x=auto.sel[train.index,]$Curb.weight, 
          z=auto.sel[train.index,]$Peak.rpm, 
          y=auto.sel[train.index,]$Price)

# Data without extreme values
scatter3d(x=train.sample$Curb.weight, 
          z=train.sample$Peak.rpm, 
          y=train.sample$Price)


### Can we differentiate regression depending on the Price bracket, before data prep and after
#   Note that variables which were log transformed need to be delogged

# Define price breaks, row and cleaned up
Price.class <- ifelse(auto$Price < 10000, "Low", ifelse(auto$Price < 30000, "Medium", "High"))
Price.train.class <- ifelse(10^train.sample$Price < 10000, "Low", ifelse(10^train.sample$Price < 30000, "Medium", "High"))

# Show multiple linear regression
scatter3d(Price ~ Peak.rpm * Curb.weight, data=auto, groups=factor(Price.class))
scatter3d(10^Price ~ Peak.rpm * Curb.weight, data=train.sample, groups=factor(Price.train.class))

# Show Loess plane fitting
scatter3d(Price ~ Peak.rpm * Curb.weight, data=auto, groups=factor(Price.class), fit="smooth")
scatter3d(10^Price ~ Peak.rpm * Curb.weight, data=train.sample, groups=factor(Price.train.class), fit="smooth")

# Show grouping and orientation of price groups
scatter3d(Price ~ Peak.rpm * Curb.weight, data=auto, groups=factor(Price.class), fit="smooth", surface=FALSE, ellipsoid=TRUE, grid=FALSE)
scatter3d(10^Price ~ Peak.rpm * Curb.weight, data=train.sample, groups=factor(Price.train.class), fit="smooth", surface=FALSE, ellipsoid=TRUE, grid=FALSE)

# Save the last plot to a JPG file
# rgl.snapshot(filename = "last-plot.jpg")

# Thank you