#Part 1 - HW3

#install.packages("tidyverse")
#install.packages("scatterplot3d")
#sinstall.packages("tibble")

library(tidyverse)
library(scatterplot3d)
library(tibble)

x <- read.table("uWaveGestureLibrary_X_TRAIN.txt")
y <- read.table("uWaveGestureLibrary_Y_TRAIN.txt")
z <- read.table("uWaveGestureLibrary_Z_TRAIN.txt")

x <- t(x)
y <- t(y)
z <- t(z)

bind1 <- cbind(x[,11], y[,11], z[,11])
bind2 <- cbind(x[,20], y[,20], z[,20])
bind3 <- cbind(x[,4], y[,4], z[,4])
bind4 <- cbind(x[,5], y[,5], z[,5])
bind5 <- cbind(x[,2], y[,2], z[,2])
bind6 <- cbind(x[,1], y[,11], z[,11])
bind7 <- cbind(x[,7], y[,7], z[,7])
bind8 <- cbind(x[,6], y[,6], z[,6])


scatterplot3d(x[,11], y[,11], z[,11], main="Class 1", color = 1)

scatterplot3d(x[,20], y[,20], z[,20], main="Class 2", color = 2)

scatterplot3d(x[,4], y[,4], z[,4], main="Class 3", color = 3)

scatterplot3d(x[,5], y[,5], z[,5], main="Class 4", color = 4)

scatterplot3d(x[,2], y[,2], z[,2], main="Class 5", color = 5)

scatterplot3d(x[,1], y[,1], z[,1], main="Class 6", color = 6)

scatterplot3d(x[,7], y[,7], z[,7], main="Class 7", color = 7)

scatterplot3d(x[,6], y[,6], z[,6], main="Class 8", color = 8)

#Part 2 - HW3

library(openxlsx)
dist=read.xlsx("mesafe.xlsx")

dist[is.na(dist)] <- 0
head(dist)
##   ADANA ADIYAMAN AFYON A–RI AMASYA ANKARA ANTALYA ARTV›N AYDIN BALIKES›R
## 1     0      336   573  966    611    490     557   1034   883       901
## 2   336        0   909  646    632    755     893    755  1219      1237
## 3   573      909     0 1310    589    256     292   1237   346       328
## 4   966      646  1310    0    738   1054    1429    397  1642      1571
## 5   611      632   589  738      0    333     825    696   931       833
## 6   490      755   256 1054    333      0     544    981   598       535
##   B›LEC›K B›NG÷L B›TL›S BOLU BURDUR BURSA «ANAKKALE «ANKIRI «ORUM DEN›ZL›
## 1     770    632    732  677    666   839      1100     575   579     760
## 2    1059    349    412  946   1002  1128      1399     785   696    1096
## 3     212   1100   1292  420    170   277       527     387   497     223
## 4    1360    358    234 1147   1425  1420      1691     986   830    1519
## 5     622    640    832  409    755   682       953     248    92     808
## 6     315    897   1089  191    422   384       655     131   241     475
##   D›YARBAKIR ED›RNE ELAZI– ERZ›NCAN ERZURUM ESK›fiEH›R GAZ›ANTEP G›RESUN
## 1        525   1169    492      678     808       688       212     728
## 2        205   1438    282      547     529       977       150     710
## 3       1098    684    960      939    1129       144       785     865
## 4        441   1639    496      371     184      1287       754     547
## 5        699    901    546      367     557       566       607     324
## 6        908    683    757      683     873       233       671     609
##   G‹M‹fiHANE HAKKAR› HATAY ISPARTA ›«EL ›STANBUL ›ZM›R KARS KASTAMONU KAYSER›
## 1       786     909   191     616   69      939   900 1011       689     333
## 2       680     669   320     952  405     1208  1236  732       885     437
## 3      1006    1482   764     169  565      454   327 1329       501     521
## 4       384     432   950    1375 1035     1409  1633  217       991     813
## 5       434    1139   703     720  639      671   912  757       253     348
## 6       750    1368   681     421  483      453   579 1073       245     318
##   KIRKLAREL› KIRfiEH›R KOCAEL› KONYA K‹TAHYA MALATYA MAN›SA KAHRAMANMARAfi MARD›N
## 1       1150      375     828   356     673     394    884           192    537
## 2       1419      571    1097   692    1009     184   1220           164    296
## 3        665      429     343   223     100     862    311           765   1110
## 4       1620      943    1298  1115    1365     592   1617           810    518
## 5        882      312     560   511     644     466    896           527    794
## 6        664      184     342   258     311     659    563           591    996
##   MU–LA  MUfi NEVfiEH›R N›–DE ORDU R›ZE SAKARYA SAMSUN S››RT S›NOP S›VAS TEK›RDA–
## 1   868  741      287   205  718  927     791    729   708   846   430     1071
## 2  1204  462      518   541  726  856    1060    750   388   890   412     1340
## 3   368 1209      440   459  821 1076     306    669  1281   670   695      586
## 4  1664  245      892   939  591  550    1261    743   331   899   619     1541
## 5   953  749      363   441  280  535     523    131   886   258   220      803
## 6   620 1006      275   348  565  820     305    413  1095   414   439      585
##   TOKAT TRABZON TUNCEL› fiANLIURFA UfiAK  VAN YOZGAT ZONGULDAK AKSARAY BAYBURT
## 1   499     852     626       349  689  895    473       754     265     808
## 2   520     781     416       110 1025  575    612      1023     593     654
## 3   633    1001    1070       922  116 1427    471       489     365    1028
## 4   677     485     424       617 1422  230    843      1207     967     306
## 5   114     460     498       716  701  967    200       469     422     456
## 6   377     745     814       808  368 1224    215       268     225     772
##   KARAMAN KIRIKKALE BATMAN fiIRNAK BARTIN ARDAHAN I–DIR YALOVA KARAB‹K K›L›S
## 1     289       474    621    720    769    1038  1069    893     701   249
## 2     625       684    301    480   1038     759   749   1162     970   209
## 3     336       331   1194   1293    515    1346  1420    341     447   822
## 4    1114       979    369    426   1174     310   143   1363    1105   813
## 5     616       258    799    981    436     779   848    625     367   666
## 6     369        75   1008   1179    283    1090  1164    407     215   730
##   OSMAN›YE D‹ZCE
## 1       87   722
## 2      249   991
## 3      660   375
## 4      879  1192
## 5      632   454
## 6      577   236
dist <- cmdscale(dist, k = 2, eig = TRUE)
x_t1 <- dist$points[, 1]
y_t1 <- dist$points[, 2]

plot(x_t1, y_t1, pch = 19, xlab = "", ylab = "", axes = F, main = "Cities of Turkey")
text(x_t1, y_t1, pos = 4, cex = 0.6, labels = colnames(dist))

#HW4

library(data.table)
library(ggplot2)
library(factoextra)

f <- file.choose()

movies1 = read.csv(f, header=FALSE,sep="|")
colnames(movies1) = c("Title")

d <- file.choose()
moviesdata=read.table(d, header=FALSE, sep="")


mdata <- data.frame(c1 = movies1,  
                   c2 = moviesdata)

distances = dist(mdata[2:100], method = "euclidean")

set.seed(123) 


clusterMovies = hclust(distances, method = "ward.D2")
plot(clusterMovies)

hc.complete=hclust(dist(moviesdata), method="complete")
plot(hc.complete,main="Complete Linkage", xlab="", cex=.9)

fviz_nbclust(moviesdata, kmeans, method = "silhouette")

subgrp <- cutree(clusterMovies, k = 2)
plot(clusterMovies)
rect.hclust(clusterMovies , k = 2, border = 2:6)