Задание 1. Пакет CARET

library(caret)
model_info <- names(getModelInfo())
head(model_info, 20)
##  [1] "ada"         "AdaBag"      "AdaBoost.M1" "adaboost"    "amdai"       "ANFIS"       "avNNet"      "awnb"       
##  [9] "awtan"       "bag"         "bagEarth"    "bagEarthGCV" "bagFDA"      "bagFDAGCV"   "bam"         "bartMachine"
## [17] "bayesglm"    "binda"       "blackboost"  "blasso"
grep("feature|select|filter|wrapper", model_info, value = TRUE, ignore.case = TRUE)
## character(0)
set.seed(123)
x <- matrix(rnorm(50 * 5), ncol = 5)
y <- factor(rep(c("A", "B"), 25))
apply(x, 2, var)
## [1] 0.8572352 0.8198347 0.9787816 0.8667145 0.8975523
jpeg("featurePlot_ellipse.jpg", width = 800, height = 600)
featurePlot(x, y, "ellipse")
dev.off()
## png 
##   2
jpeg("featurePlot_box.jpg", width = 800, height = 600)
featurePlot(x, y, "box")
dev.off()
## png 
##   2
jpeg("featurePlot_pairs.jpg", width = 800, height = 600)
featurePlot(x, y, "pairs")
dev.off()
## png 
##   2

Вывод: признаки сгенерированы независимо из нормального распределения, поэтому классы A и B плохо разделяются. featurePlot() — удобный инструмент визуальной оценки.

Задание 2. FSelector (через FSelectorRcpp)

library(FSelectorRcpp)
data(iris)

weights_ig <- information_gain(Species ~ ., iris)
print(weights_ig)
##     attributes importance
## 1 Sepal.Length  0.4521286
## 2  Sepal.Width  0.2672750
## 3 Petal.Length  0.9402853
## 4  Petal.Width  0.9554360
weights_gr <- information_gain(Species ~ ., iris, type = "gainratio")
print(weights_gr)
##     attributes importance
## 1 Sepal.Length  0.4196464
## 2  Sepal.Width  0.2472972
## 3 Petal.Length  0.8584937
## 4  Petal.Width  0.8713692
weights_su <- information_gain(Species ~ ., iris, type = "symuncert")
print(weights_su)
##     attributes importance
## 1 Sepal.Length  0.4155563
## 2  Sepal.Width  0.2452743
## 3 Petal.Length  0.8571872
## 4  Petal.Width  0.8705214

Задание 3. Пакет arules — дискретизация

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

d_interval  <- arules::discretize(x, method = "interval",  breaks = 3)
d_frequency <- arules::discretize(x, method = "frequency", breaks = 3)
d_cluster   <- arules::discretize(x, method = "cluster",   breaks = 3)
d_fixed     <- arules::discretize(x, method = "fixed",
                                  breaks = c(-Inf, 5.5, 6.5, Inf),
                                  labels = c("Low", "Medium", "High"))

table(d_interval)
## d_interval
## [4.3,5.5) [5.5,6.7) [6.7,7.9] 
##        52        70        28
table(d_frequency)
## d_frequency
## [4.3,5.4) [5.4,6.3) [6.3,7.9] 
##        46        53        51
table(d_cluster)
## d_cluster
##  [4.3,5.45) [5.45,6.46)  [6.46,7.9] 
##          52          63          35
table(d_fixed)
## d_fixed
##    Low Medium   High 
##     52     63     35
par(mfrow = c(2, 2))
hist(x, breaks = 20, main = "interval",  col = "lightgreen")
abline(v = arules::discretize(x, method = "interval",  breaks = 3, onlycuts = TRUE), col = "red", lwd = 2)
hist(x, breaks = 20, main = "frequency", col = "lightyellow")
abline(v = arules::discretize(x, method = "frequency", breaks = 3, onlycuts = TRUE), col = "red", lwd = 2)
hist(x, breaks = 20, main = "cluster",   col = "lightpink")
abline(v = arules::discretize(x, method = "cluster",   breaks = 3, onlycuts = TRUE), col = "red", lwd = 2)
hist(x, breaks = 20, main = "fixed",     col = "lightcyan")
abline(v = c(5.5, 6.5), col = "red", lwd = 2)

par(mfrow = c(1, 1))

Вывод: каждый метод по-своему разбивает переменную на категории.

Задание 4. Пакет Boruta

library(Boruta)
library(randomForest)
library(mlbench)

data(Ozone, package = "mlbench")
ozo <- na.omit(Ozone)

set.seed(123)
boruta_result <- Boruta(V4 ~ ., data = ozo, doTrace = 0)
print(boruta_result)
## Boruta performed 18 iterations in 0.3813851 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, cex.axis = 0.7)

plot(boruta_result, boxplot = TRUE, las = 2, cex.axis = 0.7)

final_decision <- attStats(boruta_result)
print(final_decision)
##         meanImp   medianImp      minImp     maxImp   normHits  decision
## V1   0.40660229  0.39874710  0.37208192 0.50361585 1.00000000 Confirmed
## V2   0.04391890  0.03169822 -0.02604472 0.13137821 0.11111111  Rejected
## V3  -0.03235907 -0.04528594 -0.08039941 0.03178173 0.00000000  Rejected
## V5   0.37961385  0.37663333  0.33809868 0.42147363 1.00000000 Confirmed
## V6   0.04740489  0.05979747 -0.01572443 0.12718160 0.05555556  Rejected
## V7   0.51883205  0.50588169  0.46725744 0.59114026 1.00000000 Confirmed
## V8   0.77086858  0.76889989  0.73251688 0.81327051 1.00000000 Confirmed
## V9   0.86915268  0.86432645  0.84273725 0.92413075 1.00000000 Confirmed
## V10  0.42389058  0.41739463  0.38164901 0.49245967 1.00000000 Confirmed
## V11  0.55143690  0.54721914  0.49482770 0.61161304 1.00000000 Confirmed
## V12  0.66051279  0.65832222  0.60234621 0.73958611 1.00000000 Confirmed
## V13  0.42761454  0.42057856  0.38618408 0.46920282 1.00000000 Confirmed

Вывод: Boruta подтверждает часть признаков как значимые, остальные отвергает.