-
Notifications
You must be signed in to change notification settings - Fork 0
/
Classify w: max seperation with Discriminant Analysis.R
71 lines (50 loc) · 1.71 KB
/
Classify w: max seperation with Discriminant Analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
library(mlr)
library(tidyverse)
install.packages("HDclassif")
data(wine, package = "HDclassif")
wineTib <- as_tibble(wine)
wineTib
wineTib$class <- as.factor(wineTib$class)
wineTib
wineUntidy <- gather(wineTib, "Variable", "Value", -class)
ggplot(wineUntidy, aes(class, Value)) +
facet_wrap(~ Variable, scales = "free_y") +
geom_boxplot() +
theme_bw()
wineTask <- makeClassifTask(data = wineTib, target = "class")
lda <- makeLearner("classif.lda")
ldaModel <- train(lda, wineTask)
ldaModelData <- getLearnerModel(ldaModel)
ldaPreds <- predict(ldaModelData)$x
head(ldaPreds)
wineTib %>%
mutate(LD1 = ldaPreds[, 1],
LD2 = ldaPreds[, 2]) %>%
ggplot(aes(LD1, LD2, col = class)) +
geom_point() +
stat_ellipse() +
theme_bw()
wineUntidy
##Extracting model information using getLearnerMode(), and DF for each case using pred()
ldaModelData <- getLearnerModel(ldaModel)
qda <- makeLearner("classif.qda")
qdaModel <- train(qda, wineTask)
kFold <- makeResampleDesc(method = "RepCV", folds = 10, reps = 50,
stratify = TRUE)
ldaCV <- resample(learner = lda, task = wineTask, resampling = kFold,
measures = list(mmce, acc))
qdaCV <- resample(learner = qda, task = wineTask, resampling = kFold,
measures = list(mmce, acc))
qdaCV <- resample(learner = qda, task = wineTask, resampling = kFold,
measures = list(mmce, acc))
ldaCV$aggr
qdaCV$aggr
qdaCV <- resample(learner = qda, task = wineTask, resampling = kFold,
measures = list(mmce, acc))
qdaCV$aggr
calculateConfusionMatrix(ldaCV$pred, relative = TRUE)
calculateConfusionMatrix(qdaCV$pred, relative = TRUE)
poisoned <- tibble(V1 = 13, V2 = 2, V3 = 2.2, V4 = 19, V5 = 100,
V6 = 2.3, V7 = 2.5, V8 = 0.35, V9 = 1.7,
V10 = 4, V11 = 1.1, V12 = 3, V13 = 750)
predict(qdaModel, newdata = poisoned)