-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlu.R
483 lines (304 loc) · 13.6 KB
/
mlu.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
## ----setup, include=TRUE, results='hide', message = FALSE, warning = FALSE, cache=FALSE----
library(checkpoint)
checkpoint("2018-09-28", R.version = "3.5.1",
project = book_directory,
checkpointLocation = checkpoint_directory,
scanForPackages = FALSE,
scan.rnw.with.knitr = TRUE, use.knitr = TRUE)
library(ggplot2)
library(cowplot)
library(viridis)
library(scales)
library(readxl)
library(data.table)
library(ape)
library(MASS)
library(matrixStats)
options(width = 70, digits = 2)
## ----eval = FALSE--------------------------------------------------------
##
## source("https://bioconductor.org/biocLite.R")
## biocLite("pcaMethods")
##
## ----echo=TRUE, message=FALSE, results = "hide"--------------------------
library(pcaMethods)
## ------------------------------------------------------------------------
## Note: download Excel file from publisher website first
dRaw <- read_excel("Gender_StatsData_worldbank.org_ccby40.xlsx")
dRaw <- as.data.table(dRaw) # convert data to data.table format.
## ------------------------------------------------------------------------
str(dRaw)
summary(dRaw)
unique(dRaw$CountryName)
unique(dRaw$IndicatorCode)
## ------------------------------------------------------------------------
dRaw[,`Indicator Name`:= NULL]
## ------------------------------------------------------------------------
## collapse columns into a super long dataset
## with Year as a new variable
d <- melt(dRaw, measure.vars = 3:20, variable.name = "Year")
head(d)
str(d)
## finally cast the data wide again
## this time with separate variables by indicator code
## keeping a country and time (Year) variable
d <- dcast(d, CountryName + Year ~ IndicatorCode)
head(d)
str(d)
## ------------------------------------------------------------------------
## rename columns with shortened, unique names
x<-colnames(d)
x<-gsub("[[:punct:]]", "", x)
(y <- abbreviate(x, minlength = 4, method = "both.sides"))
names(d) <- y
## shorten regional names to abbreviations.
d$CntN<-abbreviate(d$CntN, minlength = 5,
method = "left.kept")
## ------------------------------------------------------------------------
summary(d)
str(d)
d[, Year := as.character(Year)]
## ----mlu-gnpadoplot1, fig.width=7, fig.height=10, out.width='.6\\linewidth', fig.pos="!ht", fig.cap = "Plot of Gross National Product Per Capita and Adolescent Fertility Rate per 1,000 women."----
## ggplot2 plot object indicating x and y variables
p1 <- ggplot(d, aes(NYGN, SPAD))
## make a grid of two plots
plot_grid(
## first plot data points only
p1 + geom_point(),
## data poins colored by year
p1 + geom_point(aes(colour = Year)) +
scale_colour_viridis(discrete = TRUE),
ncol = 1
)
## ----mlu-kmeans1, fig.width=9, fig.height=9, out.width='.8\\linewidth', fig.pos="!ht", fig.cap = "Plot of Gross National Product Per Capita and Adolescent Fertility Rate per 1,000 women for different numbers of k clusters."----
set.seed(2468)
wgss <- vector("numeric", 8)
plots <- vector("list", 9)
p1 <- ggplot(d, aes(NYGN, SPAD))
for(i in 2:9) {
km <- kmeans(d[, .(NYGN, SPAD)],
centers = i)
wgss[i - 1] <- km$tot.withinss
plots[[i - 1]] <- p1 +
geom_point(aes_(colour = factor(km$cluster))) +
scale_color_viridis(discrete = TRUE) +
theme(legend.position = "none") +
ggtitle(paste("kmeans centers = ", i))
}
plots[[9]] <- ggplot() +
geom_point(aes(x = 2:9, y = wgss)) +
xlab("Number of Clusters") +
ylab("Within SS") +
ggtitle("Scree Plot")
do.call(plot_grid, c(plots, ncol = 3))
## ------------------------------------------------------------------------
summary(d[,.(NYGN, SPAD)])
## ------------------------------------------------------------------------
x <- scale(d[,.(NYGN, SPAD)])
summary(x)
## ----mlu-kmeans2, fig.width=9, fig.height=9, out.width='.8\\linewidth', fig.pos="!ht", fig.cap = "Plot of Gross National Product Per Capita and Adolescent Fertility Rate per 1,000 women for different numbers of k clusters."----
set.seed(2468)
wgss <- vector("numeric", 8)
plots <- vector("list", 9)
p1 <- ggplot(d, aes(NYGN, SPAD))
for(i in 2:9) {
km <- kmeans(x, centers = i)
wgss[i - 1] <- km$tot.withinss
plots[[i - 1]] <- p1 +
geom_point(aes_(colour = factor(km$cluster))) +
scale_color_viridis(discrete = TRUE) +
theme(legend.position = "none") +
ggtitle(paste("kmeans centers = ", i))
}
plots[[9]] <- ggplot() +
geom_point(aes(x = 2:9, y = wgss)) +
xlab("Number of Clusters") +
ylab("Within SS") +
ggtitle("Scree Plot")
do.call(plot_grid, c(plots, ncol = 3))
## ----mlu-kmeans3, fig.width=9, fig.height=9, out.width='.8\\linewidth', fig.pos="!ht", fig.cap = "Plot of Gross National Product Per Capita and Adolescent Fertility Rate per 1,000 women for different numbers of iterations."----
set.seed(2468)
plots <- vector("list", 9)
p1 <- ggplot(d, aes(NYGN, SPAD))
for(i in 6:14) {
km <- kmeans(x, centers = 6, iter.max = i)
plots[[i - 5]] <- p1 +
geom_point(aes_(colour = factor(km$cluster))) +
scale_color_viridis(discrete = TRUE) +
theme(legend.position = "none") +
ggtitle(paste("kmeans iters = ", i))
}
do.call(plot_grid, c(plots, ncol = 3))
## ----mlu-kmeans4, fig.width=9, fig.height=9, out.width='.8\\linewidth', fig.pos="!ht", fig.cap = "Plot of Gross National Product Per Capita and Adolescent Fertility Rate per 1,000 women for different nstart values."----
set.seed(2468)
plots <- vector("list", 9)
p1 <- ggplot(d, aes(NYGN, SPAD))
for(i in 1:9) {
km <- kmeans(x, centers = 6, iter.max = 10, nstart = i)
plots[[i]] <- p1 +
geom_point(aes_(colour = factor(km$cluster))) +
scale_color_viridis(discrete = TRUE) +
theme(legend.position = "none") +
ggtitle(paste("kmeans nstarts = ", i))
}
do.call(plot_grid, c(plots, ncol = 3))
## ----mlu-kmeans5, fig.width=6, fig.height=6, out.width='.8\\linewidth', fig.pos="!ht", fig.cap = "Scree Plot for all ."----
x <- scale(d[,-c(1,2)])
wgss<-0
set.seed(2468)
for( i in 1:11){
km <- kmeans(x, centers = i)
wgss[i]<-km$tot.withinss
}
ggplot() +
geom_point(aes(x = 1:11, y = wgss)) +
xlab("Number of Clusters") +
ylab("Within SS") +
ggtitle("Scree Plot - All Variables")
## ------------------------------------------------------------------------
kmAll <- kmeans(x, centers = 4, nstart = 25)
x <- cbind(d[, c(1,2)], x,
Cluster = kmAll$cluster)
tail(x)
## ------------------------------------------------------------------------
xtabs(~ CntN + Cluster, data = x)
## ------------------------------------------------------------------------
unique(x[
order(CntN, Year, Cluster),
.(CntN, Year, Cluster)][
CntN=="EsA&P"])
unique(x[
order(CntN, Year, Cluster),
.(CntN, Year, Cluster)][
CntN == "ErpnU"])
## ------------------------------------------------------------------------
hdist <- dist(d[,.(NYGN, SPAD)])
str(hdist)
## ----mlu-hclust1, fig.width=14, fig.height=10, out.width='.8\\linewidth', fig.pos="!ht", fig.cap = "Cluster Dendrogram with row numbers."----
hclust <- hclust(hdist)
plot(hclust)
## ----mlu-hclust2, fig.width=14, fig.height=12, out.width='.8\\linewidth', fig.pos="!ht", fig.cap = "Cluster Dendrogram with country names and year."----
x <- d[, .(CntN, Year, NYGN, SPAD)]
x[, Key := paste(CntN, Year)]
x[, CntN := NULL]
x[, Year := NULL]
hdist <- dist(x[,.(NYGN, SPAD)])
hclust <- hclust(hdist)
plot(hclust, labels = x$Key)
## ----mlu-hclust3, fig.width=14, fig.height=12, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "Cluster Dendrogram with country names and year and a height line."----
plot(hclust, labels = x$Key)
abline(h = 30000, col = "blue")
## ------------------------------------------------------------------------
summary(x)
d[, mean(NYGN), by = CntN][order(V1)]
d[, mean(SPAD), by = CntN][order(V1)]
## ----mlu-hclust4, fig.width=14, fig.height=12, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "Cluster Dendrogram with country names and year and another height line."----
plot(hclust, labels = x$Key)
abline(h = 20000, col = "blue")
## ----mlu-hclust5, fig.width=14, fig.height=12, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "Cluster Dendrogram with country names and year and all dimensions of data."----
x <- copy(d)
x[, Key := paste(CntN, Year)]
x[, CntN := NULL]
x[, Year := NULL]
hdist <- dist(x[, -12])
hclust <- hclust(hdist)
plot(hclust, labels = x$Key)
## ----mlu-hclust6, fig.width=14, fig.height=12, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "Cluster Dendrogram using the ward.D2 method."----
hclust <- hclust(hdist, method = "ward.D2")
plot(hclust, labels = x$Key)
## ----mlu-hclust7, fig.width=14, fig.height=12, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "Cluster Dendrogram with scaling."----
x <- scale(d[,-c(1,2)])
row.names(x) <- paste(d$CntN, d$Year)
hdist <- dist(x)
hclust <- hclust(hdist)
plot(hclust, labels = paste(d$CntN, d$Year))
abline(h = 6, col = "blue")
## ------------------------------------------------------------------------
cut_hclust <- cutree(hclust, h = 6)
unique(cut_hclust)
## ------------------------------------------------------------------------
dcopy <- as.data.table(copy(d))
dcopy[, cluster:= NA_integer_]
dcopy$cluster <- cutree(hclust, k = 3)
tail(dcopy)
## ----mlu-hclust8, fig.width=14, fig.height=14, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "Variations on Dendrogram via ape package."----
plot(as.phylo(hclust), type = "cladogram")
## ----mlu-hclust9, fig.width=14, fig.height=14, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "Variations on Dendrogram via ape package."----
plot(as.phylo(hclust), type = "fan")
## ----mlu-hclust10, fig.width=14, fig.height=14, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "Variations on Dendrogram via ape package."----
plot(as.phylo(hclust), type = "radial")
## ----mlu-hclust11, fig.width=14, fig.height=14, out.width='.9\\linewidth', fig.pos="!ht", fig.cap = "unrooted type on 4 clusters."----
hclust4 <- cutree(hclust, k = 4)
plot(as.phylo(hclust), type = "unrooted", label.offset = 1,
tip.color = hclust4, cex = 0.8)
## ----mlu-pca1, fig.width=6, fig.height=6, out.width='.6\\linewidth', fig.pos="!ht", fig.cap = "A highly correlated plot - are there really two dimensions here?"----
cor(d$NYGD, d$NYGN)
summary(d[,.(NYGD, NYGN)])
ggplot(d, aes(NYGD, NYGN)) +
geom_point()
## ----mlu-pca2, fig.width=6, fig.height=6, out.width='.6\\linewidth', fig.pos="!ht", fig.cap = "A highly correlated plot - are there really two dimensions here?"----
ggplot(d, aes(NYGD, SESC)) +
geom_point()
cor(d$NYGD, d$SESC)
## ------------------------------------------------------------------------
x <- d[,.( NYGN, SPAD)]
res <- pca(x, method="svd", center=TRUE, scale = "uv")
summary(res)
## ----mlu-pca3, fig.width=7, fig.height=7, out.width='.8\\linewidth', fig.pos="!ht", fig.cap = "comparison of raw data and pca"----
biplot(res, main = "Biplot of PCA")
## ----mlu-pca4, fig.width=5, fig.height=5, out.width='.5\\linewidth', fig.pos="!ht", fig.cap = "Scree plot for traditional PCA on all features in the data."----
x <- d[, -c(1,2)]
res <- pca(x, method="svd", center=TRUE, scale = "uv",
nPcs = ncol(x))
summary(res)
## reverse scree plot
ggplot() +
geom_bar(aes(1:11, cumsum(res@R2)),
stat = "identity") +
scale_x_continuous("Principal Component", 1:11) +
scale_y_continuous(expression(R^2), labels = percent) +
ggtitle("Scree Plot") +
coord_cartesian(xlim = c(.5, 11.5), ylim = c(.5, 1),
expand = FALSE)
## ----mlu-pca5, fig.width=6, fig.height=6, out.width='.7\\linewidth', fig.pos="!ht", fig.cap = "Biplot for first two principal components."----
biplot(res, choices = c(1, 2))
## ------------------------------------------------------------------------
head(scores(res))
round(cor(scores(res)),2)
## ----mlu-pca6, fig.width=5, fig.height=10, out.width='.6\\linewidth', fig.pos="!ht", fig.cap = "Plot loadings from PCA models with and without outliers using traditional, SVD, PCA and robust PCA."----
x <- d[, -c(1,2)]
x <- prep(x, center = TRUE, scale = "uv")
xout <- copy(x)
xout[1:5, "NYGD"] <- (-10)
res1 <- pca(x, method = "svd",
center = FALSE, nPcs = 4)
res2 <- pca(xout, method = "svd",
center = FALSE, nPcs = 4)
res1rob <- pca(x, method = "robustPca",
center = FALSE, nPcs = 4)
res2rob <- pca(xout, method = "robustPca",
center = FALSE, nPcs = 4)
plot_grid(
ggplot() +
geom_point(aes(
x = as.numeric(loadings(res1)),
y = as.numeric(loadings(res2)))) +
xlab("Loadings, SVD, No Outliers") +
ylab("Loadings, SVD, Outliers"),
ggplot() +
geom_point(aes(
x = as.numeric(loadings(res1rob)),
y = as.numeric(loadings(res2rob)))) +
xlab("Loadings, Robust PCA, No Outliers") +
ylab("Loadings, Robust PCA, Outliers"),
ncol = 1)
## ------------------------------------------------------------------------
x <- scale(d[, -c(1,2)])
row.names(x) <- paste(d$CntN, d$Year)
head(x)
sdist <- dist(x)
xSammon <- sammon(sdist, k = 2)
head(xSammon$points)
## ----mlu-sam1, fig.width=5, fig.height=5, out.width='.6\\linewidth', fig.pos="!ht", fig.cap = "Plot of Sammon points with text labels"----
plot(xSammon$points, type = "n")
text(xSammon$points, labels = row.names(x) )