knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

library(caret)
## Загрузка требуемого пакета: ggplot2
## Загрузка требуемого пакета: lattice
library(FSelectorRcpp)
library(arules)
## Загрузка требуемого пакета: Matrix
## 
## Присоединяю пакет: 'arules'
## Следующий объект скрыт от 'package:FSelectorRcpp':
## 
##     discretize
## Следующие объекты скрыты от 'package:base':
## 
##     abbreviate, write
library(Boruta)
library(mlbench)

set.seed(123)

x <- matrix(rnorm(50*5), ncol = 5)
colnames(x) <- paste0("X", 1:5)

y <- factor(rep(c("A", "B"), 25))

featurePlot(x, y, plot = "density", main = "График плотности")

featurePlot(x, y, plot = "box", main = "Ящик с усами")

featurePlot(x, y, plot = "pairs", main = "Матрица рассеяния")

data(iris)

info_gain <- information_gain(Species ~ ., iris)
relief <- relief(Species ~ ., iris, neighboursCount = 5)

comparison <- data.frame(
  Feature = info_gain$attributes,
  InfoGain = info_gain$importance,
  Relief = relief$importance
)

comparison
##        Feature  InfoGain    Relief
## 1 Sepal.Length 0.4521286 0.1683333
## 2  Sepal.Width 0.2672750 0.1466667
## 3 Petal.Length 0.9402853 0.3457627
## 4  Petal.Width 0.9554360 0.3491667
barplot(info_gain$importance,
        names.arg = info_gain$attributes,
        main = "Information Gain для признаков iris",
        col = "steelblue",
        ylab = "Важность",
        xlab = "Признаки")

data("Ozone")

ozone_clean <- na.omit(Ozone)

set.seed(123)
boruta_result <- Boruta(V4 ~ ., data = ozone_clean, doTrace = 0)

boruta_result
## Boruta performed 24 iterations in 0.8548059 secs.
##  9 attributes confirmed important: V1, V10, V11, V12, V13 and 4 more;
##  3 attributes confirmed unimportant: V2, V3, V6;
plot(boruta_result, 
     las = 2, 
     main = "Boruta - Важность признаков для Ozone")

attStats(boruta_result)
##        meanImp  medianImp     minImp     maxImp  normHits  decision
## V1   9.5563296  9.7071000  8.4255686 10.7247899 1.0000000 Confirmed
## V2   1.1557680  1.1576551 -0.2474598  2.7423660 0.1666667  Rejected
## V3  -0.9877372 -0.7333367 -3.4162909  0.3794342 0.0000000  Rejected
## V5   9.2426781  9.2313179  8.1108460 10.5140883 1.0000000 Confirmed
## V6   0.9886679  1.3615721 -1.1013954  1.9852132 0.0000000  Rejected
## V7  11.7026875 11.5169965 10.5127703 13.4896943 1.0000000 Confirmed
## V8  17.1647491 17.2255744 16.0336735 18.5525852 1.0000000 Confirmed
## V9  19.2281405 19.0627349 17.5889826 20.9190449 1.0000000 Confirmed
## V10  9.8662368  9.7266893  8.6477478 11.3131795 1.0000000 Confirmed
## V11 11.8977619 11.8484607 10.9347533 13.6520570 1.0000000 Confirmed
## V12 14.6326841 14.6095338 13.5595253 16.0775580 1.0000000 Confirmed
## V13  9.4438214  9.5489762  8.1005306 10.7881019 1.0000000 Confirmed
getSelectedAttributes(boruta_result)
## [1] "V1"  "V5"  "V7"  "V8"  "V9"  "V10" "V11" "V12" "V13"