library(mlbench)
data(Glass)
str(Glass)
## 'data.frame':    214 obs. of  10 variables:
##  $ RI  : num  1.52 1.52 1.52 1.52 1.52 ...
##  $ Na  : num  13.6 13.9 13.5 13.2 13.3 ...
##  $ Mg  : num  4.49 3.6 3.55 3.69 3.62 3.61 3.6 3.61 3.58 3.6 ...
##  $ Al  : num  1.1 1.36 1.54 1.29 1.24 1.62 1.14 1.05 1.37 1.36 ...
##  $ Si  : num  71.8 72.7 73 72.6 73.1 ...
##  $ K   : num  0.06 0.48 0.39 0.57 0.55 0.64 0.58 0.57 0.56 0.57 ...
##  $ Ca  : num  8.75 7.83 7.78 8.22 8.07 8.07 8.17 8.24 8.3 8.4 ...
##  $ Ba  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Fe  : num  0 0 0 0 0 0.26 0 0 0 0.11 ...
##  $ Type: Factor w/ 6 levels "1","2","3","5",..: 1 1 1 1 1 1 1 1 1 1 ...
#separate predictors from response
glassX <- Glass[, -10]

#histograms
library(lattice)
for(i in 1:ncol(glassX)){
  print(histogram(glassX[,i], xlab = names(glassX)[i], type = "count", main = paste("Histogram of", names(glassX)[i])))
}

#boxplots
par(mfrow = c(1, 3))
for(i in 1:ncol(glassX)){
  boxplot(glassX[,i], main = names(glassX)[i], col = "blue")
}

par(mfrow = c(1, 1))

#correlation plot to understand relationships among predictors
library(corrplot)
## corrplot 0.95 loaded
glassCorr <- cor(glassX)
corrplot(glassCorr, order = "hclust", tl.cex = .75)

#It looks like most of the predictors have low correlation there are some pairs
#that do correlate though, such as Si-RI which have negative correlation, RI-Ca
#with positive correlation, and Mg-Al and Mg-Ba with negative correlation.
#
#There are even some with no correlation whatsoever such as Al-Si, RI-Ba, and K-Fe.

#scatter plot matrix colored by glass type
splom(glassX, groups = Glass$Type, auto.key = list(columns = 3), type = c("p", "g"))

#calculate skewness for each predictor
library(e1071)
skewValues <- apply(glassX, 2, skewness)

skewValues
##         RI         Na         Mg         Al         Si          K         Ca 
##  1.6027151  0.4478343 -1.1364523  0.8946104 -0.7202392  6.4600889  2.0184463 
##         Ba         Fe 
##  3.3686800  1.7298107
cat("\n")
#if max/min > 20 means it is likely, but not certainly skewed.
for(i in 1:ncol(glassX)){
  if(min(glassX[,i])>0){ #division by zero
    ratio <- max(glassX[,i])/min(glassX[,i])
    cat(names(glassX)[i], ": max/min ratio =", round(ratio, 2), "\n")
  } else{
    cat(names(glassX)[i], ": contains zero as values\n")
  }
}
## RI : max/min ratio = 1.02 
## Na : max/min ratio = 1.62 
## Mg : contains zero as values
## Al : max/min ratio = 12.07 
## Si : max/min ratio = 1.08 
## K : contains zero as values
## Ca : max/min ratio = 2.98 
## Ba : contains zero as values
## Fe : contains zero as values
#None of the min/max ratios exceed 20, however we can see that Al has the highest min/max ration which means that the range
#of values is larger than the rest, alongside it being relatively uniform over that range.
#
#It seems that most of the elements are right skewed with the exception of Mg, which is left skewed.
#
#Elements which are significantly skewed are RI, Mg, K, Ca, Ba, and Fe. Where K is the most skewed with a skewness of 6.46.
#
#Elements which are relatively uniform are Na, Al, and Si. Where Na is the least skewed.
library(caret)
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:e1071':
## 
##     element
#box-Cox transformations for predictors with strictly positive values
for(i in 1:ncol(glassX)){
  if(min(glassX[,i]) > 0){ #all values must be > 0
    cat("\n-----------------------------------------")
    cat("\nBox-Cox for", names(glassX)[i], "\n")
    bc <- BoxCoxTrans(glassX[,i])
    print(bc)
    transformed <- predict(bc, glassX[,i])
    
    cat("Skewness before:", round(skewness(glassX[,i]), 3), "\n")
    cat("Skewness after:", round(skewness(transformed), 3), "\n")
    
    #histogram comparisons before and after transformation
    print(histogram(glassX[,i], xlab = "Natural Units", type = "count", 
                    main = paste("Original:", names(glassX)[i])))
    print(histogram(transformed, xlab = "Box-Cox Units", type = "count", 
                    main = paste("Box-Cox:", names(glassX)[i])))
  } else {
    cat("\n-----------------------------------------")
    cat("\n", names(glassX)[i], 
        "contains zero or negative values\n")
  }
}
## 
## -----------------------------------------
## Box-Cox for RI 
## Box-Cox Transformation
## 
## 214 data points used to estimate Lambda
## 
## Input data summary:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.511   1.517   1.518   1.518   1.519   1.534 
## 
## Largest/Smallest: 1.02 
## Sample Skewness: 1.6 
## 
## Estimated Lambda: -2 
## 
## Skewness before: 1.603 
## Skewness after: 1.566

## 
## -----------------------------------------
## Box-Cox for Na 
## Box-Cox Transformation
## 
## 214 data points used to estimate Lambda
## 
## Input data summary:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10.73   12.91   13.30   13.41   13.82   17.38 
## 
## Largest/Smallest: 1.62 
## Sample Skewness: 0.448 
## 
## Estimated Lambda: -0.1 
## With fudge factor, Lambda = 0 will be used for transformations
## 
## Skewness before: 0.448 
## Skewness after: 0.034

## 
## -----------------------------------------
##  Mg contains zero or negative values
## 
## -----------------------------------------
## Box-Cox for Al 
## Box-Cox Transformation
## 
## 214 data points used to estimate Lambda
## 
## Input data summary:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.290   1.190   1.360   1.445   1.630   3.500 
## 
## Largest/Smallest: 12.1 
## Sample Skewness: 0.895 
## 
## Estimated Lambda: 0.5 
## 
## Skewness before: 0.895 
## Skewness after: 0.091

## 
## -----------------------------------------
## Box-Cox for Si 
## Box-Cox Transformation
## 
## 214 data points used to estimate Lambda
## 
## Input data summary:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   69.81   72.28   72.79   72.65   73.09   75.41 
## 
## Largest/Smallest: 1.08 
## Sample Skewness: -0.72 
## 
## Estimated Lambda: 2 
## 
## Skewness before: -0.72 
## Skewness after: -0.651

## 
## -----------------------------------------
##  K contains zero or negative values
## 
## -----------------------------------------
## Box-Cox for Ca 
## Box-Cox Transformation
## 
## 214 data points used to estimate Lambda
## 
## Input data summary:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   5.430   8.240   8.600   8.957   9.172  16.190 
## 
## Largest/Smallest: 2.98 
## Sample Skewness: 2.02 
## 
## Estimated Lambda: -1.1 
## 
## Skewness before: 2.018 
## Skewness after: -0.194

## 
## -----------------------------------------
##  Ba contains zero or negative values
## 
## -----------------------------------------
##  Fe contains zero or negative values
#As you can see in the output. BoxCox decreases the skewness of every element. Some decreases are very large,
#even overshooting as seen where Ca skewness goes from 2.018 (right) to -0.194 (left)
#
#Then some are barely changed at all, still improved but extremely minor. Such as RI and Si.
#
#
cat("\n-----------------------------------------\n")
## 
## -----------------------------------------
#preprocess BoxCox, center, and scale to all predictors
glassPP <- preProcess(glassX, method = c("BoxCox", "center", "scale"))
glassPP
## Created from 214 samples and 9 variables
## 
## Pre-processing:
##   - Box-Cox transformation (5)
##   - centered (9)
##   - ignored (0)
##   - scaled (9)
## 
## Lambda estimates for Box-Cox transformation:
## -2, -0.1, 0.5, 2, -1.1
glassTransformed <- predict(glassPP, glassX)

#check near zero variance predictors
nearZeroVar(glassTransformed)
## integer(0)
#check highly correlated predictors
glassCorr2 <- cor(glassTransformed)
highCorr <- findCorrelation(glassCorr2, .75)
highCorr
## [1] 7
if(length(highCorr) > 0){
  names(glassTransformed)[highCorr]
}
## [1] "Ca"
#correlation plot after transformation
corrplot(glassCorr2, order = "hclust", tl.cex = .75)

library(kernlab)
## 
## Attaching package: 'kernlab'
## The following object is masked from 'package:ggplot2':
## 
##     alpha
set.seed(67)

sigDist <- sigest(Type~ ., data = Glass, frac = 1)
sigDist
##        90%        50%        10% 
## 0.02176819 0.06906005 0.83586779
svmTuneGrid <- data.frame(sigma = as.vector(sigDist)[1], C = 2^(-2:10))
svmTuneGrid
##         sigma       C
## 1  0.02176819    0.25
## 2  0.02176819    0.50
## 3  0.02176819    1.00
## 4  0.02176819    2.00
## 5  0.02176819    4.00
## 6  0.02176819    8.00
## 7  0.02176819   16.00
## 8  0.02176819   32.00
## 9  0.02176819   64.00
## 10 0.02176819  128.00
## 11 0.02176819  256.00
## 12 0.02176819  512.00
## 13 0.02176819 1024.00
library(AppliedPredictiveModeling)
library(caret)
set.seed(67)

svmFit <- train(Type~ ., data = Glass, method = "svmRadial",
preProc = c("center", "scale"),tuneGrid = svmTuneGrid,
trControl = trainControl(method = "repeatedcv", repeats = 5))

plot(svmFit, scales = list(x = list(log = 2)))