-
Notifications
You must be signed in to change notification settings - Fork 0
/
discriminant analysis.R
78 lines (51 loc) · 1.89 KB
/
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
72
73
74
75
76
77
78
library(tidyverse)
library(mlr)
data(wine, package = "HDclassif")
wineTib <- as_tibble(wine)
wineTib
names(wineTib) <- c("Class", "Alco", "Malic", "Ash", "Alk", "Mag",
"Phe", "Flav", "Non_flav", "Proan", "Col", "Hue",
"OD", "Prol")
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()
#training the model
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()
#now for QDA
qda<-makeLearner("classif.qda")
qdaModel <- train(qda, wineTask)
qdaModelData <- getLearnerModel(qdaModel)
qdapreds <- predict(qdaModelData)$x
head(qdapreds)
#cross validate models
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))
ldaCV$aggr
qdaCV$aggr
#confusion matrix
calculateConfusionMatrix(ldaCV$pred, relative = TRUE)
calculateConfusionMatrix(qdaCV$pred, relative = TRUE)
poisoned <- tibble(Alco = 13, Malic = 2, Ash = 2.2, Alk = 19, Mag = 100,
Phe = 2.3, Flav = 2.5, Non_flav = 0.35, Proan = 1.7,
Col = 4, Hue = 1.1, OD = 3, Prol = 750)
predict(qdaModel, newdata = poisoned)