-
Notifications
You must be signed in to change notification settings - Fork 4
/
s3_anwers.Rmd
211 lines (150 loc) · 4.01 KB
/
s3_anwers.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
title: "Introduction to Dimensional Reduction in R - UseR!22 (Section III)"
author: "Isabella Bicalho Frazeto"
date: '2022-06-19'
output: html_document
---
# Section 3
## Imports
```{r}
library(tidyverse)
library(ggplot2)
library(vegan)
library(MASS)
library(ggforce)
library(cowplot)
```
## Getting to know our dataset
```{r}
data("swiss")
head(swiss)
skimr::skim(swiss)
```
```{r}
swiss %>%
ggplot(aes(x=Education, y = Examination)) +
geom_point() +
theme_minimal_grid() +
panel_border(color = "black")
```
```{r}
swiss %>%
ggplot(aes(x=Fertility, y = Agriculture)) +
geom_point() +
theme_minimal_grid() +
panel_border(color = "black")
```
# Base R
We can calculate the Principal Coordinate Analysis (i.e Metric Multidimensional Scaling or Classical multidimensional scaling) using the cmdscale function in R.
```{r}
distance_matrix_euclidean <- swiss %>% dist(method = "euclidean")
euclidean_MDS <- distance_matrix_euclidean %>%
cmdscale()
tibble_euclidean_MDS <- tibble(
MDS_1 = euclidean_MDS[,1],
MDS_2 = euclidean_MDS[,2],
city = rownames(euclidean_MDS)
)
tibble_euclidean_MDS %>%
ggplot(aes(x=MDS_1, y = MDS_2, label = city)) +
geom_text(check_overlap = TRUE) +
theme_minimal_grid() +
panel_border(color = "black")
```
## Let's do it using another distance
```{r}
distance_matrix_manhattan <- swiss %>% dist(method = "manhattan")
m_MDS <- distance_matrix_manhattan %>%
cmdscale()
tibble_m_MDS <- tibble(
MDS_1 = m_MDS[,1],
MDS_2 = m_MDS[,2],
city = rownames(m_MDS)
)
tibble_m_MDS %>%
ggplot(aes(x=MDS_1, y = MDS_2, label = city)) +
geom_text(check_overlap = TRUE) +
theme_minimal_grid() +
panel_border(color="black")
```
## The vegan package also has a similar fuction (metaMDS)
```{r}
library(vegan)
vegan_mds <- metaMDS(comm = swiss,
distance = "jaccard",
trace = FALSE,
autotransform = FALSE)
```
Similarly, we can plot it.
```{r}
plot(vegan_mds$points)
```
It is not super informative. We can make it better. Let's start by creating a dataframe
```{r}
df_MDS <- data.frame(vegan_mds$points)
```
We need to make sure our names are matching
```{r}
#sanity check
rownames(df_MDS) == rownames(swiss)
```
```{r}
df_MDS$Education <- swiss$Education
df_MDS$Fertility <- swiss$Fertility
df_MDS$inf_mort <- swiss$Infant.Mortality
df_MDS %>%
ggplot(aes(x=MDS1, y=MDS2, color = Fertility)) +
geom_point() +
theme_minimal_grid() +
panel_border("black")
```
## Education
```{r}
df_MDS %>%
ggplot(aes(x=MDS1, y=MDS2, color = Education)) +
geom_point() +
theme_minimal_grid() +
panel_border(color="black")
```
## What about infant mortality?
```{r}
df_MDS %>%
ggplot(aes(x=MDS1, y=MDS2, label = rownames(df_MDS), color = inf_mort)) +
geom_text(check_overlap = TRUE) +
theme_minimal_grid() +
panel_border(color = "black")
```
## Finally, we can check the stress
```{r}
vegan_mds$stress
```
### Non-parametric multidimensional scaling
```{r}
library(MASS)
non_metric_mds <- swiss %>%
dist("canberra") %>%
isoMDS(k=5)
```
Let's explore our results. We will extract the point and pass it as data.frame
```{r}
non_metric_mds_df <- non_metric_mds$points
colnames(non_metric_mds_df) <- c(paste0("DIM", 1:5))
non_metric_mds_df
```
We also plot the dimensions like we did for PCA and ICA
```{r}
non_metric_mds_df %>%
as_tibble() %>%
mutate(
city = rownames(non_metric_mds_df)
) %>%
ggplot(aes(x = .panel_x, y = .panel_y)) +
geom_point(alpha = 0.4, size = 0.5) +
geom_autodensity(alpha = .3) +
facet_matrix(vars(-city), layer.diag = 2) +
scale_color_brewer(palette = "Dark2") +
scale_fill_brewer(palette = "Dark2") +
theme_minimal_grid() +
panel_border(color = "black") +
theme(legend.position = "bottom")
```