-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dimensionality_Reduction.Rmd
173 lines (144 loc) · 3.56 KB
/
Dimensionality_Reduction.Rmd
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
title: "Dimensionality_Reduction"
output: html_document
---
GOALS:
- Explore different projections:
+ PCA
+ Screeplot
- MDS
- Different distance metrics
- STRESS
- Shepard Plot
- t-SNE
- LLE
- ISOMAP
- NeRV
- CCA
- ESOM (?)
- Explore other approaches:
- Wrapper:
- RFE
- Genetic
- Filter:
- Correlation based (FCBF package)
```{r setup, include=FALSE, message=FALSE, warning=FALSE}
library(ProjectionBasedClustering)
```
Desired dimensions
```{r}
dimensions <-
c(1:10,
15,
20,
25,
30,
35,
40,
45,
50,
75,
100,
125,
150,
175,
200,
250,
300,
350,
400,
450,
500)
```
PCA function
```{r pca}
optimizePCA <- function(train, test) {
p <- prcomp(train)
summ.p <- summary(p)
var.explained <- summ.p$importance[2,]
gg <- ggplot(data = data.frame(x = 1:50, y = (p$sdev ^ 2)[1:50]),
mapping = aes(x = x, y = y)) + geom_point() + geom_line()
plot(gg + labs(title = "Screeplot",
x = "Components",
y = "Eigenvalues"))
no_of_features <- which(cumsum(var.explained) > 0.8)[1]
print(paste(no_of_features, "components account for 80% of total variance"))
train_return <- data.frame(p$x[, 1:no_of_features])
test_return <-
data.frame(predict(p, newdata = test)[, 1:no_of_features])
return(list("train" = train_return, "test" = test_return))
}
```
PCA ace vaxinpad
```{r}
pca_ace_vaxinpad <- optimizePCA(train = ace_vaxinpad_train, test = ace_vaxinpad_test)
```
MDS functions
```{r mds_funcs}
calculateMDS <- function(original_distances, dimensions) {
stress <- vector("list", length(dimensions))
projected_points <- vector("list", length(dimensions))
for (i in 1:length(dimensions)) {
mds <-
MDS(as.matrix(original_distances), OutputDimension = dimensions[i])
stress[[i]] <- mds$Stress
projected_points[[i]] <- mds$ProjectedPoints
}
return(list(
"stress" = stress,
"points" = projected_points,
"dimensions" = dimensions
))
}
plotScreeplot <- function(stress, dimensions) {
p <-
ggplot(data = data.frame(x = dimensions, y = stress),
mapping = aes(x = x, y = y)) +
geom_point() +
geom_line()
return(
p + labs(
title = "Screeplot",
x = "Dimensions",
y = "STRESS"
)
)
}
determineGoodStressValues <- function(stress, dimensions, cutoff) {
return(dimensions[stress <= cutoff])
}
plotShepardPlot <- function(original_distances, new_distances) {
correlation <- cor(original_distances, new_distances)
p <-
ggplot(
data = data.frame(x = original_distances, y = new_distances),
mapping = aes(x = x, y = y)
) +
geom_point(size = 1, alpha = 0.5) +
geom_abline(intercept = 0, slope = 1, colour = "grey")
return(
p + labs(
title = "Shepard plot",
subtitle = paste("r =", correlation),
x = "Original distances",
y = "Projected distances"
)
)
}
optimizeMDS <- function(data, dimensions) {
distances <- dist(data)
mds <- calculateMDS(distances, dimensions)
plot(plotScreeplot(unlist(mds$stress, use.names = F), dimensions))
opt_stress <-
determineGoodStressValues(unlist(mds$stress, use.names = F), dimensions, cutoff = 0.1)
opt_points <- mds$points[[which(dimensions == opt_stress[1])]]
new_distances <- dist(opt_points)
plot(plotShepardPlot(as.vector(distances), as.vector(new_distances)))
return(opt_points)
}
```
MDS ace vaxinpad
```{r}
mds_points_ace_vaxinpad <- optimizeMDS(ace_vaxinpad, dimensions)
dim(mds_points_ace_vaxinpad)
```