-
Notifications
You must be signed in to change notification settings - Fork 1
/
R_code.R
766 lines (579 loc) · 24.1 KB
/
R_code.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
###########################
#Libraries
###########################
library('mclust')
library('cluster')
library('factoextra')
library('plotly')
library('plyr')
library('tree')
library('caret')
library('randomForest')
library('rpart')
library('e1071')
library("Rtsne")
###########################
#Import Dataset
###########################
flag <- read.csv("C:/Users/georg/Dropbox/Business Analytics/Winter-Quarter/Statistics_2/Assignment_2/flag.data", header=FALSE)
####################################################################################
################################ 1. Classiffication ###############################
####################################################################################
#Religion
flag$V7<- as.factor(flag$V7)
flag$V7<-revalue(flag$V7, c("0"="Catholic", "1"="Other Christian", "2"="Muslim", "3"="Buddhist", "4"="Hindu", "5"="Ethnic","6"="Marxist","7"="Others"))
#We use only the characteristics of the flags
flag_class<-flag[,c(7:30)]
#Rename attributes
names(flag_class) <- c("religion","bars","stripes","colours","red","green","blue","gold","white","black","orange","mainhue","circles","crosses","saltires","quarters","sunstars","crescent","triangle","icon","animate","text","topleft","botright")
flag_num<-flag_class
flag_class$religion<-as.factor(flag_class$religion)
flag_class$religion<-revalue(flag_class$religion, c("0"="Catholic", "1"="Other Christian", "2"="Muslim", "3"="Buddhist", "4"="Hindu", "5"="Ethnic","6"="Marxist","7"="Others"))
#Change the types of the characteristics
##Factors
flag_class$religion<-as.factor(flag_class$religion)
flag_class$red<-as.factor(flag_class$red)
flag_class$green<-as.factor(flag_class$green)
flag_class$blue<-as.factor(flag_class$blue)
flag_class$gold<-as.factor(flag_class$gold)
flag_class$white<-as.factor(flag_class$white)
flag_class$black<-as.factor(flag_class$black)
flag_class$orange<-as.factor(flag_class$orange)
flag_class$crescent<-as.factor(flag_class$crescent)
flag_class$triangle<-as.factor(flag_class$triangle)
flag_class$animate<-as.factor(flag_class$animate)
flag_class$text<-as.factor(flag_class$text)
flag_class$topleft<-as.factor(flag_class$topleft)
flag_class$botright<-as.factor(flag_class$botright)
##Integers
flag_class$bars<-as.integer(flag_class$bars)
flag_class$stripes<-as.integer(flag_class$stripes)
flag_class$colours<-as.integer(flag_class$colours)
flag_class$circles<-as.integer(flag_class$circles)
flag_class$crosses<-as.integer(flag_class$crosses)
flag_class$saltires<-as.integer(flag_class$saltires)
flag_class$quarters<-as.integer(flag_class$quarters)
flag_class$sunstars<-as.integer(flag_class$sunstars)
str(flag_class)
###########################
#Descriptive Analysis-Plots
###########################
####Religions
#Sort the religions (for better visualization)
V7<-as.data.frame(table(flag$V7))
V7<-V7[order(V7$Freq, decreasing = TRUE),]
V7$Var1 <- factor(V7$Var1, levels = unique(V7$Var1)[order(V7$Freq, decreasing = TRUE)])
p7<-plot_ly(V7,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(title = "Religions and Flags",
xaxis = list(title = "Religions"),
yaxis = list(title = "Number of Flags"))
####Bars
V8<-as.data.frame(table(flag$V8))
V8<-V8[order(V8$Freq, decreasing = TRUE),]
V8$Var1 <- factor(V8$Var1, levels = unique(V8$Var1)[order(V8$Freq, decreasing = TRUE)])
p8<-plot_ly(V8,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(title = "Vertical Number of bars",
xaxis = list(title = "Bars"),
yaxis = list(title = "Number of flags"))
####Stripes
V9<-as.data.frame(table(flag$V9))
V9<-V9[order(V9$Freq, decreasing = TRUE),]
V9$Var1 <- factor(V9$Var1, levels = unique(V9$Var1)[order(V9$Freq, decreasing = TRUE)])
p9<-plot_ly(V9,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(title = "Number of Stripes",
xaxis = list(title = "Stripes"),
yaxis = list(title = "Number of flags"))
####Colours
V10<-as.data.frame(table(flag$V10))
V10<-V10[order(V10$Freq, decreasing = TRUE),]
V10$Var1 <- factor(V10$Var1, levels = unique(V10$Var1)[order(V10$Freq, decreasing = TRUE)])
p10<-plot_ly(V10,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(title = "Number of Different Colours",
xaxis = list(title = "Number of different colours"),
yaxis = list(title = "Number of flags"))
####Main Hue
V18<-as.data.frame(table(flag$V18))
V18<-V18[order(V18Freq, decreasing = TRUE),]
V18$Var1 <- factor(V18$Var1, levels = unique(V18$Var1)[order(V18$Freq, decreasing = TRUE)])
p18<-plot_ly(V18,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(
title = "Main Hue Color",
xaxis = list(title = "Colours"),
yaxis = list(title = "Number of flags"))
####Circles
V19<-as.data.frame(table(flag$V19))
V19<-V19[order(V19Freq, decreasing = TRUE),]
V19$Var1 <- factor(V19$Var1, levels = unique(V19$Var1)[order(V19$Freq, decreasing = TRUE)])
p19<-plot_ly(V19,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(title = "Number of Cyrcles",
xaxis = list(title = "Number of Circles"),
yaxis = list(title = "Number of flags"))
####Number of Upright Crosses
V20<-as.data.frame(table(flag$V20))
V20<-V20[order(V20Freq, decreasing = TRUE),]
V20$Var1 <- factor(V20$Var1, levels = unique(V20$Var1)[order(V20$Freq, decreasing = TRUE)])
p20<-plot_ly(V20,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(title = "Number of Upright Crosses",
xaxis = list(title = "Number of Crosses"),
yaxis = list(title = "Number of flags"))
####Number of saltires
V21<-as.data.frame(table(flag$V21))
V21<-V21[order(V21Freq, decreasing = TRUE),]
V21$Var1 <- factor(V21$Var1, levels = unique(V21$Var1)[order(V21$Freq, decreasing = TRUE)])
p21<-plot_ly(V21,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(title = "Number of Saltires",
xaxis = list(title = "Number of Diagonial Crosses"),
yaxis = list(title = "Number of flags"))
####Number of Quarters
V22<-as.data.frame(table(flag$V22))
V22<-V22[order(V22Freq, decreasing = TRUE),]
V22$Var1 <- factor(V22$Var1, levels = unique(V22$Var1)[order(V22$Freq, decreasing = TRUE)])
p22<-plot_ly(V22,x=~Var1,
y=~Freq, type = 'bar',
marker = list(color ='rgb(158,202,225)',
line = list(color = 'rgb(255,255,255)',
width = 6.0)
)) %>% layout(title = "Number of Quarters",
xaxis = list(title = "Quarters"),
yaxis = list(title = "Number of flags"))
###########################
#Tree Model (all variables)
###########################
#Create the model
fit1<-tree(flag_class$religion~.,data=flag_class,split="gini")
#Visualize the model
plot(fit1, main="Initial Classification Tree"); text(fit1, cex=0.8, col="dodgerblue3")
#k-Fold Validation
re<-NULL
deiktes<-sample(1:nrow(flag_class))
for (k in c(1,2,3,4,6,8,10)) {
print(k)
omades<- as.integer(nrow(flag_class)/k)
t<-NULL
for (i in 1:omades) {
te<- deiktes[ ((i-1)*k+1):(i*k)]
train <- flag_class[-te,]
test <- flag_class[te,]
cl <- factor(flag_class$religion[-te])
z <- tree(cl~.,data=train,split="gini")
pr<- predict(z, test,type='class')
t<- c(t, sum(flag_class$religion[te] == pr) /dim(test)[1])
}
re<-c(re,mean(t))
}
#Avg final score
mean(re) #0.42
########################################################
#Tree Model (with selected variables from random forest)
########################################################
#######################################
#Variables Selection with Random Forest
#######################################
#Random Forest
rf <- randomForest(religion~.,data=flag_class, trials=100, method='class')
#The general idea is to permute the values of each feature
#and measure how much the permutation decreases the
#accuracy of the model
#Initialize for the search
importance<-rf$importance
importance<-importance[order(importance),]
not.them<-NULL
accur_score<-0
re<-0
p<-1
while (accur_score<=mean(re)){
#Create the list with the variables that we minimize the accuracy of the model
not.them<-c(not.them, importance[p])
accur_score<-mean(re)
re<-NULL
deiktes<-sample(1:nrow(flag_class))
#Cross validation
for (k in c(1,2,3,4,6,8,10,15,20)) {
omades<- as.integer(nrow(flag_class)/k)
t<-NULL
for (i in 1:omades) {
te<- deiktes[ ((i-1)*k+1):(i*k)]
train <- flag_class[-te,]
test <- flag_class[te,]
cl <- factor(flag_class$religion[-te])
z <- tree(cl~.,data=train[,colnames(train)!=not.them],split="gini")
pr<- predict(z, test,type='class')
t<- c(t, sum(flag_class$religion[te] == pr) /dim(test)[1])
}
re<-c(re,mean(t))
}
print(p)
p<-p+1
}
print(not.them)
print(accur_score)
#Create the model
fit3<-tree(flag_class$religion~.,data=flag_class[,colnames(train)!=not.them],split="deviance")
#Remove the less important variables
varImpPlot(rf, sort = T,n.var=ncol(flag_class)-length(not.them)-1, main=" Selected Variables Importance")
#Visualize the model
plot(fit3); text(fit3, cex=0.8, col="dodgerblue3")
##########################################
#Tree model (with rpart Library + pruning)
##########################################
fit2<-rpart(flag_class$religion~.,data=flag_class)
pfit<- prune(fit2, cp=fit2$cptable[which.min(fit2$cptable[,"xerror"]),"CP"])
#Visualize prune tree
plot(fit2); text(fit2, cex=0.8, col="dodgerblue3") #Before pruning
plot(pfit); text(pfit, cex=0.8, col="dodgerblue3") #After pruning
#k-Fold Validation
re<-NULL
deiktes<-sample(1:nrow(flag_class))
for (k in c(1,2,3,4,6,8,10,15,20)) {
print(k)
omades<- as.integer(nrow(flag_class)/k)
t<-NULL
for (i in 1:omades) {
te<- deiktes[ ((i-1)*k+1):(i*k)]
train <- flag_class[-te,]
test <- flag_class[te,]
cl <- factor(flag_class$religion[-te])
fit2<-rpart(cl~.,data=train)
z<- prune(fit2, cp=fit2$cptable[which.min(fit2$cptable[,"xerror"]),"CP"])
pr<- predict(z, test,type='class')
t<- c(t, sum(flag_class$religion[te] == pr) /dim(test)[1])
}
re<-c(re,mean(t))
}
#Avg final score
mean(re)
###############################
#SVM
###############################
####################
#### Attention! ####
####################
#The tune method takes more than 20 minutes to finish!
#Tune the parameters
obj = tune.svm(religion~.,data=flag_class,cost=10:100,gamma=seq(0,3,0.1))
#Train the model
svm_fit<-svm(religion ~ ., data = flag_class, cost = 19,cross=10, gamma = 0.2)
#k-Fold Validation
re<-NULL
accur<-NULL
deiktes<-sample(1:nrow(flag_class))
for (k in c(1,2,3,4,6,8,10,15,20)) {
print(k)
omades<- as.integer(nrow(flag_class)/k)
t<-NULL
for (i in 1:omades) {
te<- deiktes[ ((i-1)*k+1):(i*k)]
train <- flag_class[-te,]
test <- flag_class[te,]
cl <- factor(flag_class$religion[-te])
svm_fit <- svm(cl ~ ., data = train, cost = 19,cross=10, gamma = 0.2)
pr<- predict(svm_fit, test,type='class')
t<- c(t, sum(flag_class$religion[te] == pr) /dim(test)[1])
## compute svm confusion matrix & Accuracy
matrix<-table(pred=pr, true=flag_class$religion[te])
accuracy<-sum(diag(matrix))/sum(matrix)
}
re<-c(re,mean(t))
accur<-c(accur,accuracy)
}
#Avg final score
mean(re) #0.76
mean(accur) #0.67
####################################################################################
################################## 2. Clustering ###################################
####################################################################################
#We use only the characteristics of the flags
flag_clust<-flag[,c(7:30)]
#Rename the Features
names(flag_clust) <- c("religion","bars","stripes","colours","red","green","blue","gold","white","black","orange","mainhue","circles","crosses","saltires","quarters","sunstars","crescent","triangle","icon","animate","text","topleft","botright")
#calculate the distance
gower_dist <- daisy(flag_clust[,-1],
metric = "gower"
)
summary(gower_dist)
# Output most similar pair
gower_mat <- as.matrix(gower_dist)
flag_clust[
which(gower_mat == min(gower_mat[gower_mat != min(gower_mat)]),
arr.ind = TRUE)[1, ],]
#The results are the flags 85, 168 which are the flags of Syria and Iraq
# Output most dissimilar pair
gower_mat <- as.matrix(gower_dist)
flag_clust[
which(gower_mat == max(gower_mat[gower_mat != max(gower_mat)]),
arr.ind = TRUE)[1, ], ]
#The results are the flags 77, 79 which are the flags of Haiti and Hong-Kong
################################################
#Calculate silhouette width for many k using PAM
################################################
sil_width <- c(NA)
for(i in 2:10){
pam_fit <- pam(gower_dist,
diss = TRUE,
k = i)
sil_width[i] <- pam_fit$silinfo$avg.width
}
# Plot sihouette width (higher is better)
plot(1:10, sil_width,
xlab = "Number of clusters",
ylab = "Silhouette Width", col="blue")
lines(1:10, sil_width)
########################
#Hierarchical Clustering
########################
# Euclidean Distance & Ward Linkage
gower_ward <- hclust(gower_dist,method='ward.D')
#Visualization
plot(gower_ward)
rect.hclust(gower_ward, k = 9, border = "red")
summary(gower_ward)
clusterCut <- cutree(gower_ward, 9)
table(actual = flag_clust$V7, predicted = clusterCut)
fviz_silhouette(silhouette(clusterCut,gower_dist ))
########################
#PAM Method
########################
pam_fit <- pam(gower_dist, diss = TRUE, k = 9)
#Summarys statistics for each cluster
pam_results <- flag_clust %>%
dplyr::select(- religion) %>%
mutate(cluster = pam_fit$clustering) %>%
group_by(cluster) %>%
do(the_summary = summary(.))
pam_results$the_summary
###########################
#Clustering visualization
###########################
tsne_obj <- Rtsne(gower_dist, is_distance = TRUE)
tsne_data <- tsne_obj$Y %>%
data.frame() %>%
setNames(c("X", "Y")) %>%
mutate(cluster = factor(pam_fit$clustering),
name = flag_clust$V7)
ggplot(aes(x = X, y = Y), data = tsne_data) +
geom_point(aes(color = cluster))
plot(pam_fit, main="Silhouette Plot")
clusplot(pam_fit)
#Count the number of observation for each cluster
table(pam_fit$clustering)
fviz_silhouette(pam_fit)
pam_fit$clustering
plot_ly( x = ~flag$V19, y = ~flag_class$colours, color = ~as.factor(pam_fit$clustering),type = 'bar')
plot_ly( x = ~flag$V19, y = ~flag_class$colours, color = ~as.factor(pam_fit$clustering),type = 'bar')
#Add a new column with cluster ID
flag_clust$clusters<-as.factor(pam_fit$clustering)
names(flag_clust) <- c("religion","bars","stripes","colours","red","green","blue","gold","white","black","orange","mainhue","circles","crosses","saltires","quarters","sunstars","crescent","triangle","icon","animate","text","topleft","botright", "clusters")
###########################
#Religion Pie
###########################
#Create a prop table for all the clusters
religion_table<-prop.table(table(flag_clust$clusters, flag_clust$religion),2)
plot(religion_table)
#Cluster 1
plot_ly( labels = ~names(religion_table[1,]),
values = ~religion_table[1,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 1')
#Cluster 2
plot_ly( labels = ~names(religion_table[2,]),
values = ~religion_table[2,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 2')
#Cluster 3
plot_ly( labels = ~names(religion_table[3,]),
values = ~religion_table[3,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 3')
#Cluster 4
plot_ly( labels = ~names(religion_table[4,]),
values = ~religion_table[4,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 4')
#Cluster 5
plot_ly( labels = ~names(religion_table[5,]),
values = ~religion_table[5,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 5')
#Cluster 6
plot_ly( labels = ~names(religion_table[6,]),
values = ~religion_table[6,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 6')
#Cluster 7
plot_ly( labels = ~names(religion_table[7,]),
values = ~religion_table[7,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 7')
#Cluster 8
plot_ly( labels = ~names(religion_table[8,]),
values = ~religion_table[8,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 8')
#Cluster 9
plot_ly( labels = ~names(religion_table[9,]),
values = ~religion_table[9,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 9')
###########################
# Continent Pies (landmass)
###########################
#Create a prop table for all the clusters
flag_clust$landmass<-as.factor(flag$V2)
flag_clust$landmass<- revalue(flag_clust$landmass, c( '1'='N.America', '2'='S.America', '3'='Europe', '4'='Africa', '5'='Asia', '6'='Oceania'))
religion_table<-prop.table(table(flag_clust$clusters, flag_clust$landmass),2)
plot(religion_table)
#Cluster 1
plot_ly( labels = ~names(religion_table[1,]),
values = ~religion_table[1,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 1')
#Cluster 2
plot_ly( labels = ~names(religion_table[2,]),
values = ~religion_table[2,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 2')
#Cluster 3
plot_ly( labels = ~names(religion_table[3,]),
values = ~religion_table[3,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 3')
#Cluster 4
plot_ly( labels = ~names(religion_table[4,]),
values = ~religion_table[4,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 4')
#Cluster 5
plot_ly( labels = ~names(religion_table[5,]),
values = ~religion_table[5,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 5')
#Cluster 6
plot_ly( labels = ~names(religion_table[6,]),
values = ~religion_table[6,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 6')
#Cluster 7
plot_ly( labels = ~names(religion_table[7,]),
values = ~religion_table[7,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 7')
#Cluster 8
plot_ly( labels = ~names(religion_table[8,]),
values = ~religion_table[8,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 8')
#Cluster 9
plot_ly( labels = ~names(religion_table[9,]),
values = ~religion_table[9,],
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(line = list(color = '#FFFFFF', width = 1)),
type = 'pie')%>%
layout(title = 'Religions % in Cluster 9')
################################
#Countriers of each cluster
################################
#Create a prop table for all the clusters
flag_clust$name<-as.factor(flag$V1)
countries_table<-table(flag_clust$name, flag_clust$clusters)
#Cluster 1 countries
View(countries_table[countries_table[,1]==1,1])
#Cluster 2 countries
View(countries_table[countries_table[,2]==1,2])
#Cluster 3 countries
View(countries_table[countries_table[,3]==1,3])
#Cluster 4 countries
View(countries_table[countries_table[,4]==1,4])
#Cluster 5 countries
View(countries_table[countries_table[,5]==1,5])
#Cluster 6 countries
View(countries_table[countries_table[,6]==1,6])
#Cluster 7 countries
View(countries_table[countries_table[,7]==1,7])
#Cluster 8 countries
View(countries_table[countries_table[,8]==1,8])
#Cluster 9 countries
View(countries_table[countries_table[,9]==1,9])