Task 1: CARET Feature Plot

# Load libraries
library(caret)
library(ggplot2)

# Show available modeling methods
names(getModelInfo())
##   [1] "ada"                 "AdaBag"              "AdaBoost.M1"        
##   [4] "adaboost"            "amdai"               "ANFIS"              
##   [7] "avNNet"              "awnb"                "awtan"              
##  [10] "bag"                 "bagEarth"            "bagEarthGCV"        
##  [13] "bagFDA"              "bagFDAGCV"           "bam"                
##  [16] "bartMachine"         "bayesglm"            "binda"              
##  [19] "blackboost"          "blasso"              "blassoAveraged"     
##  [22] "bridge"              "brnn"                "BstLm"              
##  [25] "bstSm"               "bstTree"             "C5.0"               
##  [28] "C5.0Cost"            "C5.0Rules"           "C5.0Tree"           
##  [31] "cforest"             "chaid"               "CSimca"             
##  [34] "ctree"               "ctree2"              "cubist"             
##  [37] "dda"                 "deepboost"           "DENFIS"             
##  [40] "dnn"                 "dwdLinear"           "dwdPoly"            
##  [43] "dwdRadial"           "earth"               "elm"                
##  [46] "enet"                "evtree"              "extraTrees"         
##  [49] "fda"                 "FH.GBML"             "FIR.DM"             
##  [52] "foba"                "FRBCS.CHI"           "FRBCS.W"            
##  [55] "FS.HGD"              "gam"                 "gamboost"           
##  [58] "gamLoess"            "gamSpline"           "gaussprLinear"      
##  [61] "gaussprPoly"         "gaussprRadial"       "gbm_h2o"            
##  [64] "gbm"                 "gcvEarth"            "GFS.FR.MOGUL"       
##  [67] "GFS.LT.RS"           "GFS.THRIFT"          "glm.nb"             
##  [70] "glm"                 "glmboost"            "glmnet_h2o"         
##  [73] "glmnet"              "glmStepAIC"          "gpls"               
##  [76] "hda"                 "hdda"                "hdrda"              
##  [79] "HYFIS"               "icr"                 "J48"                
##  [82] "JRip"                "kernelpls"           "kknn"               
##  [85] "knn"                 "krlsPoly"            "krlsRadial"         
##  [88] "lars"                "lars2"               "lasso"              
##  [91] "lda"                 "lda2"                "leapBackward"       
##  [94] "leapForward"         "leapSeq"             "Linda"              
##  [97] "lm"                  "lmStepAIC"           "LMT"                
## [100] "loclda"              "logicBag"            "LogitBoost"         
## [103] "logreg"              "lssvmLinear"         "lssvmPoly"          
## [106] "lssvmRadial"         "lvq"                 "M5"                 
## [109] "M5Rules"             "manb"                "mda"                
## [112] "Mlda"                "mlp"                 "mlpKerasDecay"      
## [115] "mlpKerasDecayCost"   "mlpKerasDropout"     "mlpKerasDropoutCost"
## [118] "mlpML"               "mlpSGD"              "mlpWeightDecay"     
## [121] "mlpWeightDecayML"    "monmlp"              "msaenet"            
## [124] "multinom"            "mxnet"               "mxnetAdam"          
## [127] "naive_bayes"         "nb"                  "nbDiscrete"         
## [130] "nbSearch"            "neuralnet"           "nnet"               
## [133] "nnls"                "nodeHarvest"         "null"               
## [136] "OneR"                "ordinalNet"          "ordinalRF"          
## [139] "ORFlog"              "ORFpls"              "ORFridge"           
## [142] "ORFsvm"              "ownn"                "pam"                
## [145] "parRF"               "PART"                "partDSA"            
## [148] "pcaNNet"             "pcr"                 "pda"                
## [151] "pda2"                "penalized"           "PenalizedLDA"       
## [154] "plr"                 "pls"                 "plsRglm"            
## [157] "polr"                "ppr"                 "pre"                
## [160] "PRIM"                "protoclass"          "qda"                
## [163] "QdaCov"              "qrf"                 "qrnn"               
## [166] "randomGLM"           "ranger"              "rbf"                
## [169] "rbfDDA"              "Rborist"             "rda"                
## [172] "regLogistic"         "relaxo"              "rf"                 
## [175] "rFerns"              "RFlda"               "rfRules"            
## [178] "ridge"               "rlda"                "rlm"                
## [181] "rmda"                "rocc"                "rotationForest"     
## [184] "rotationForestCp"    "rpart"               "rpart1SE"           
## [187] "rpart2"              "rpartCost"           "rpartScore"         
## [190] "rqlasso"             "rqnc"                "RRF"                
## [193] "RRFglobal"           "rrlda"               "RSimca"             
## [196] "rvmLinear"           "rvmPoly"             "rvmRadial"          
## [199] "SBC"                 "sda"                 "sdwd"               
## [202] "simpls"              "SLAVE"               "slda"               
## [205] "smda"                "snn"                 "sparseLDA"          
## [208] "spikeslab"           "spls"                "stepLDA"            
## [211] "stepQDA"             "superpc"             "svmBoundrangeString"
## [214] "svmExpoString"       "svmLinear"           "svmLinear2"         
## [217] "svmLinear3"          "svmLinearWeights"    "svmLinearWeights2"  
## [220] "svmPoly"             "svmRadial"           "svmRadialCost"      
## [223] "svmRadialSigma"      "svmRadialWeights"    "svmSpectrumString"  
## [226] "tan"                 "tanSearch"           "treebag"            
## [229] "vbmpRadial"          "vglmAdjCat"          "vglmContRatio"      
## [232] "vglmCumulative"      "widekernelpls"       "WM"                 
## [235] "wsrf"                "xgbDART"             "xgbLinear"          
## [238] "xgbTree"             "xyf"
# Create synthetic data
set.seed(123)
x <- matrix(rnorm(50*5), ncol = 5)
colnames(x) <- paste0('V', 1:5)
y <- factor(rep(c('A','B'), each = 25))
df <- data.frame(x, Class = y)

# Feature plots
featurePlot(x = x, y = y, plot = 'pairs')

featurePlot(x = x, y = y, plot = "box", auto.key = list(columns = 2), main = "Boxplots by Class")

featurePlot(x = x, y = y, plot = "density", auto.key = list(columns = 2), main = "Density Curves by Class")

library(FSelector)
data(iris)

# Show dataset info
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
table(iris$Species)
## 
##     setosa versicolor  virginica 
##         50         50         50
# Calculate feature importance
ig <- information.gain(Species ~ ., iris)
gr <- gain.ratio(Species ~ ., iris)
chi <- chi.squared(Species ~ ., iris)

res <- data.frame(InformationGain = ig$attr_importance,
                  GainRatio = gr$attr_importance,
                  ChiSquared = chi$attr_importance,
                  row.names = rownames(ig))
print(res)
##              InformationGain GainRatio ChiSquared
## Sepal.Length       0.4521286 0.4196464  0.6288067
## Sepal.Width        0.2672750 0.2472972  0.4922162
## Petal.Length       0.9402853 0.8584937  0.9346311
## Petal.Width        0.9554360 0.8713692  0.9432359
# Plot
barplot(ig$attr_importance[order(ig$attr_importance, decreasing = TRUE)],
        main = 'Information Gain (iris)', las = 3)

library(arules)
data(iris)
vec <- iris$Sepal.Length

summary(vec)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   4.300   5.100   5.800   5.843   6.400   7.900
# 4 discretization methods
int_cut <- discretize(vec, method = 'interval', categories = 3)
freq_cut <- discretize(vec, method = 'frequency', categories = 3)
clust_cut <- discretize(vec, method = 'cluster', categories = 3)
fixed_cut <- discretize(vec, method = 'fixed', breaks = c(4.3, 5.5, 6.5, 7.9))

# Show results
table(int_cut)
## int_cut
## [4.3,5.5) [5.5,6.7) [6.7,7.9] 
##        52        70        28
table(freq_cut)
## freq_cut
## [4.3,5.4) [5.4,6.3) [6.3,7.9] 
##        46        53        51
table(clust_cut)
## clust_cut
##  [4.3,5.42) [5.42,6.39)  [6.39,7.9] 
##          52          56          42
table(fixed_cut)
## fixed_cut
## [4.3,5.5) [5.5,6.5) [6.5,7.9] 
##        52        63        35
# Plots
plot(int_cut, main='interval')

plot(freq_cut, main='frequency')

plot(clust_cut, main='cluster')

plot(fixed_cut, main='fixed')

library(Boruta)
library(mlbench)
data("Ozone")

ozone_clean <- na.omit(Ozone)

set.seed(42)
boruta_res <- Boruta(V4 ~ ., data = ozone_clean, ntree = 500, doTrace = 0)

print(boruta_res)
## Boruta performed 21 iterations in 0.94034 secs.
##  9 attributes confirmed important: V1, V10, V11, V12, V13 and 4 more;
##  3 attributes confirmed unimportant: V2, V3, V6;
plot(boruta_res, xlab = "", xaxt = "n")
axis(side = 1, las = 2, at = 1:ncol(boruta_res$ImpHistory), 
     labels = colnames(boruta_res$ImpHistory), cex.axis = 0.7)