# Load necessary libraries
library(cluster)
## Warning: package 'cluster' was built under R version 4.3.3
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.3
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(DataExplorer)
## Warning: package 'DataExplorer' was built under R version 4.3.3
data<-read.csv(file.choose())
View(data)
# Assign column names to the 'data' object (assuming it's a data frame or matrix)
names(data) <- c('T_xacc','T_yacc','T_zacc','T_xgyro','T_ygro','T_zgyro','T_xmag','T_ymag','T_zmag',
                 'RA_xacc','RA_yacc','RA_zacc','RA_xgyro','RA_ygro','RA_zgyro','RA_xmag','RA_ymag','RA_zmag',
                 'LA_xacc','LA_yacc','LA_zacc','LA_xgyro','LA_ygro','LA_zgyro','LA_xmag','LA_ymag','LA_zmag',
                 'RL_xacc','RL_yacc','RL_zacc','RL_xgyro','RL_ygro','RL_zgyro','RL_xmag','RL_ymag','RL_zmag',
                 'LL_xacc','LL_yacc','LL_zacc','LL_xgyro','LL_ygro','LL_zgyro','LL_xmag','LL_ymag','LL_zmag')

head(data)
##   T_xacc T_yacc T_zacc   T_xgyro   T_ygro   T_zgyro   T_xmag    T_ymag  T_zmag
## 1 8.1305 1.0202 5.3843 -0.009368 0.023485  0.001953 -0.78717 -0.068275 0.15890
## 2 8.1604 1.0201 5.3622  0.015046 0.014330  0.000204 -0.78664 -0.068277 0.15879
## 3 8.1603 1.0052 5.3770  0.006892 0.018045  0.005649 -0.78529 -0.069849 0.15912
## 4 8.1605 1.0275 5.3473  0.008811 0.030433 -0.005346 -0.78742 -0.068796 0.15916
## 5 8.1454 1.0201 5.3919  0.007001 0.029523 -0.008080 -0.78716 -0.070603 0.15905
## 6 8.1454 1.0495 5.4518 -0.005710 0.014217 -0.021708 -0.78720 -0.067744 0.15836
##   RA_xacc RA_yacc RA_zacc RA_xgyro   RA_ygro  RA_zgyro  RA_xmag  RA_ymag
## 1 0.71829  5.6005  7.9712 0.012862 -0.005241 -0.003029 -0.57306 -0.55684
## 2 0.69849  5.6612  7.9568 0.021639 -0.010421  0.011992 -0.57357 -0.55788
## 3 0.72799  5.6393  7.9763 0.016440  0.001875  0.002322 -0.60463 -0.56833
## 4 0.71572  5.6441  7.9714 0.000286 -0.003408  0.002425 -0.56864 -0.55990
## 5 0.71323  5.6611  7.9714 0.012768 -0.015835 -0.002188 -0.56927 -0.55966
## 6 0.71813  5.6466  7.9617 0.016329 -0.005132  0.009398 -0.56549 -0.55804
##    RA_zmag LA_xacc LA_yacc LA_zacc  LA_xgyro   LA_ygro  LA_zgyro  LA_xmag
## 1 -0.21335  3.4340 -8.3708  3.8537 -0.014302 -0.001877 -0.004997 -0.64838
## 2 -0.21336  3.4291 -8.3757  3.8757  0.000126 -0.008308 -0.012062 -0.64821
## 3 -0.24523  3.4437 -8.3271  3.8286  0.000092  0.003542 -0.011145 -0.64841
## 4 -0.21383  3.4486 -8.3709  3.8466 -0.012524  0.003591 -0.004980 -0.64909
## 5 -0.21335  3.4437 -8.3563  3.8609 -0.007098 -0.000094 -0.008514 -0.64811
## 6 -0.21032  3.4389 -8.3903  3.8492  0.000106 -0.014629 -0.010307 -0.64737
##   LA_ymag  LA_zmag RL_xacc RL_yacc  RL_zacc  RL_xgyro   RL_ygro  RL_zgyro
## 1 0.34240 0.074865 -3.5386  9.0812 -0.93332  0.014458  0.002668 -0.011870
## 2 0.34099 0.073155 -3.5385  9.0662 -0.92580 -0.009606  0.010623 -0.008289
## 3 0.34125 0.074143 -3.5385  9.0587 -0.94080  0.000949  0.007971 -0.003015
## 4 0.34152 0.075607 -3.5311  9.0812 -0.93317 -0.009898 -0.002577  0.003975
## 5 0.34200 0.075141 -3.5608  9.0585 -0.91873  0.000952  0.013264 -0.002976
## 6 0.34107 0.074829 -3.5310  9.0662 -0.93315  0.007318  0.006195 -0.010084
##   RL_xmag  RL_ymag   RL_zmag LL_xacc LL_yacc LL_zacc  LL_xgyro   LL_ygro
## 1 0.73172 -0.25236 -0.034005 -2.8146 -9.0737  2.6218 -0.014784 -0.016477
## 2 0.72914 -0.25249 -0.036489 -2.8221 -9.0886  2.6366 -0.012770  0.005717
## 3 0.73080 -0.25176 -0.036151 -2.8071 -9.0811  2.6070 -0.005725  0.009620
## 4 0.73035 -0.25235 -0.033807 -2.8146 -9.0737  2.6218 -0.003929 -0.008371
## 5 0.72932 -0.25278 -0.035693 -2.8072 -9.0886  2.6070 -0.014559  0.016500
## 6 0.73044 -0.25180 -0.034855 -2.8076 -9.0893  2.6085 -0.012902  0.009463
##    LL_zgyro LL_xmag LL_ymag   LL_zmag
## 1  0.002789 0.73937 0.30183 -0.057514
## 2 -0.007918 0.73955 0.30052 -0.057219
## 3  0.006555 0.74029 0.30184 -0.057750
## 4  0.002816 0.73845 0.30090 -0.057527
## 5 -0.006040 0.74009 0.30065 -0.056939
## 6 -0.002449 0.73886 0.30164 -0.057682
str(data)
## 'data.frame':    124 obs. of  45 variables:
##  $ T_xacc  : num  8.13 8.16 8.16 8.16 8.15 ...
##  $ T_yacc  : num  1.02 1.02 1.01 1.03 1.02 ...
##  $ T_zacc  : num  5.38 5.36 5.38 5.35 5.39 ...
##  $ T_xgyro : num  -0.00937 0.01505 0.00689 0.00881 0.007 ...
##  $ T_ygro  : num  0.0235 0.0143 0.018 0.0304 0.0295 ...
##  $ T_zgyro : num  0.001953 0.000204 0.005649 -0.005346 -0.00808 ...
##  $ T_xmag  : num  -0.787 -0.787 -0.785 -0.787 -0.787 ...
##  $ T_ymag  : num  -0.0683 -0.0683 -0.0698 -0.0688 -0.0706 ...
##  $ T_zmag  : num  0.159 0.159 0.159 0.159 0.159 ...
##  $ RA_xacc : num  0.718 0.698 0.728 0.716 0.713 ...
##  $ RA_yacc : num  5.6 5.66 5.64 5.64 5.66 ...
##  $ RA_zacc : num  7.97 7.96 7.98 7.97 7.97 ...
##  $ RA_xgyro: num  0.012862 0.021639 0.01644 0.000286 0.012768 ...
##  $ RA_ygro : num  -0.00524 -0.01042 0.00187 -0.00341 -0.01583 ...
##  $ RA_zgyro: num  -0.00303 0.01199 0.00232 0.00243 -0.00219 ...
##  $ RA_xmag : num  -0.573 -0.574 -0.605 -0.569 -0.569 ...
##  $ RA_ymag : num  -0.557 -0.558 -0.568 -0.56 -0.56 ...
##  $ RA_zmag : num  -0.213 -0.213 -0.245 -0.214 -0.213 ...
##  $ LA_xacc : num  3.43 3.43 3.44 3.45 3.44 ...
##  $ LA_yacc : num  -8.37 -8.38 -8.33 -8.37 -8.36 ...
##  $ LA_zacc : num  3.85 3.88 3.83 3.85 3.86 ...
##  $ LA_xgyro: num  -0.014302 0.000126 0.000092 -0.012524 -0.007098 ...
##  $ LA_ygro : num  -0.001877 -0.008308 0.003542 0.003591 -0.000094 ...
##  $ LA_zgyro: num  -0.005 -0.01206 -0.01115 -0.00498 -0.00851 ...
##  $ LA_xmag : num  -0.648 -0.648 -0.648 -0.649 -0.648 ...
##  $ LA_ymag : num  0.342 0.341 0.341 0.342 0.342 ...
##  $ LA_zmag : num  0.0749 0.0732 0.0741 0.0756 0.0751 ...
##  $ RL_xacc : num  -3.54 -3.54 -3.54 -3.53 -3.56 ...
##  $ RL_yacc : num  9.08 9.07 9.06 9.08 9.06 ...
##  $ RL_zacc : num  -0.933 -0.926 -0.941 -0.933 -0.919 ...
##  $ RL_xgyro: num  0.014458 -0.009606 0.000949 -0.009898 0.000952 ...
##  $ RL_ygro : num  0.00267 0.01062 0.00797 -0.00258 0.01326 ...
##  $ RL_zgyro: num  -0.01187 -0.00829 -0.00301 0.00398 -0.00298 ...
##  $ RL_xmag : num  0.732 0.729 0.731 0.73 0.729 ...
##  $ RL_ymag : num  -0.252 -0.252 -0.252 -0.252 -0.253 ...
##  $ RL_zmag : num  -0.034 -0.0365 -0.0362 -0.0338 -0.0357 ...
##  $ LL_xacc : num  -2.81 -2.82 -2.81 -2.81 -2.81 ...
##  $ LL_yacc : num  -9.07 -9.09 -9.08 -9.07 -9.09 ...
##  $ LL_zacc : num  2.62 2.64 2.61 2.62 2.61 ...
##  $ LL_xgyro: num  -0.01478 -0.01277 -0.00572 -0.00393 -0.01456 ...
##  $ LL_ygro : num  -0.01648 0.00572 0.00962 -0.00837 0.0165 ...
##  $ LL_zgyro: num  0.00279 -0.00792 0.00656 0.00282 -0.00604 ...
##  $ LL_xmag : num  0.739 0.74 0.74 0.738 0.74 ...
##  $ LL_ymag : num  0.302 0.301 0.302 0.301 0.301 ...
##  $ LL_zmag : num  -0.0575 -0.0572 -0.0578 -0.0575 -0.0569 ...
summary(data)
##      T_xacc          T_yacc           T_zacc         T_xgyro         
##  Min.   :7.682   Min.   :0.9974   Min.   :5.317   Min.   :-0.039399  
##  1st Qu.:7.876   1st Qu.:1.0352   1st Qu.:5.437   1st Qu.:-0.004507  
##  Median :7.959   Median :1.0838   Median :5.611   Median : 0.005143  
##  Mean   :7.974   Mean   :1.0835   Mean   :5.608   Mean   : 0.005013  
##  3rd Qu.:8.108   3rd Qu.:1.1240   3rd Qu.:5.753   3rd Qu.: 0.014096  
##  Max.   :8.161   Max.   :1.1832   Max.   :6.181   Max.   : 0.045403  
##      T_ygro            T_zgyro              T_xmag            T_ymag        
##  Min.   :-0.08064   Min.   :-0.030754   Min.   :-0.7955   Min.   :-0.07158  
##  1st Qu.: 0.01103   1st Qu.:-0.008880   1st Qu.:-0.7932   1st Qu.:-0.06985  
##  Median : 0.02411   Median :-0.003463   Median :-0.7916   Median :-0.06873  
##  Mean   : 0.02632   Mean   :-0.003729   Mean   :-0.7908   Mean   :-0.06848  
##  3rd Qu.: 0.04304   3rd Qu.: 0.001965   3rd Qu.:-0.7875   3rd Qu.:-0.06719  
##  Max.   : 0.21476   Max.   : 0.015614   Max.   :-0.7853   Max.   :-0.06435  
##      T_zmag          RA_xacc          RA_yacc         RA_zacc     
##  Min.   :0.1096   Min.   :0.5854   Min.   :5.596   Min.   :7.841  
##  1st Qu.:0.1198   1st Qu.:0.6491   1st Qu.:5.663   1st Qu.:7.889  
##  Median :0.1349   Median :0.6886   Median :5.709   Median :7.933  
##  Mean   :0.1357   Mean   :0.6790   Mean   :5.713   Mean   :7.926  
##  3rd Qu.:0.1547   3rd Qu.:0.7096   3rd Qu.:5.757   3rd Qu.:7.962  
##  Max.   :0.1592   Max.   :0.7593   Max.   :5.848   Max.   :8.001  
##     RA_xgyro            RA_ygro             RA_zgyro            RA_xmag       
##  Min.   :-0.014167   Min.   :-0.017639   Min.   :-0.035780   Min.   :-0.6046  
##  1st Qu.: 0.006609   1st Qu.:-0.006953   1st Qu.:-0.008205   1st Qu.:-0.5713  
##  Median : 0.012949   Median : 0.000066   Median :-0.002933   Median :-0.5689  
##  Mean   : 0.012954   Mean   :-0.001029   Mean   :-0.002965   Mean   :-0.5693  
##  3rd Qu.: 0.018526   3rd Qu.: 0.003621   3rd Qu.: 0.002403   3rd Qu.:-0.5666  
##  Max.   : 0.038867   Max.   : 0.015303   Max.   : 0.016870   Max.   :-0.5384  
##     RA_ymag           RA_zmag           LA_xacc         LA_yacc      
##  Min.   :-0.5743   Min.   :-0.2452   Min.   :3.268   Min.   :-8.482  
##  1st Qu.:-0.5637   1st Qu.:-0.2132   1st Qu.:3.380   1st Qu.:-8.394  
##  Median :-0.5611   Median :-0.2114   Median :3.411   Median :-8.371  
##  Mean   :-0.5616   Mean   :-0.2111   Mean   :3.403   Mean   :-8.376  
##  3rd Qu.:-0.5596   3rd Qu.:-0.2088   3rd Qu.:3.434   3rd Qu.:-8.356  
##  Max.   :-0.5484   Max.   :-0.1840   Max.   :3.487   Max.   :-8.293  
##     LA_zacc         LA_xgyro            LA_ygro              LA_zgyro        
##  Min.   :3.794   Min.   :-0.045979   Min.   :-0.0333500   Min.   :-0.036797  
##  1st Qu.:3.837   1st Qu.:-0.012329   1st Qu.:-0.0054370   1st Qu.:-0.011178  
##  Median :3.849   Median :-0.006575   Median : 0.0000160   Median :-0.006770  
##  Mean   :3.852   Mean   :-0.004886   Mean   : 0.0000467   Mean   :-0.007540  
##  3rd Qu.:3.864   3rd Qu.: 0.003567   3rd Qu.: 0.0057930   3rd Qu.:-0.001463  
##  Max.   :3.922   Max.   : 0.020662   Max.   : 0.0238160   Max.   : 0.016169  
##     LA_xmag           LA_ymag          LA_zmag           RL_xacc      
##  Min.   :-0.6503   Min.   :0.3375   Min.   :0.06794   Min.   :-3.607  
##  1st Qu.:-0.6485   1st Qu.:0.3409   1st Qu.:0.07090   1st Qu.:-3.547  
##  Median :-0.6479   Median :0.3418   Median :0.07238   Median :-3.538  
##  Mean   :-0.6479   Mean   :0.3418   Mean   :0.07226   Mean   :-3.536  
##  3rd Qu.:-0.6471   3rd Qu.:0.3431   3rd Qu.:0.07357   3rd Qu.:-3.524  
##  Max.   :-0.6456   Max.   :0.3456   Max.   :0.07637   Max.   :-3.472  
##     RL_yacc         RL_zacc           RL_xgyro             RL_ygro          
##  Min.   :9.028   Min.   :-1.0010   Min.   :-0.0236640   Min.   :-0.0131380  
##  1st Qu.:9.058   1st Qu.:-0.9548   1st Qu.:-0.0042617   1st Qu.: 0.0008448  
##  Median :9.066   Median :-0.9331   Median : 0.0004570   Median : 0.0078635  
##  Mean   :9.063   Mean   :-0.9345   Mean   : 0.0004088   Mean   : 0.0077402  
##  3rd Qu.:9.066   3rd Qu.:-0.9186   3rd Qu.: 0.0046515   3rd Qu.: 0.0131898  
##  Max.   :9.095   Max.   :-0.8803   Max.   : 0.0187880   Max.   : 0.0307700  
##     RL_zgyro             RL_xmag          RL_ymag           RL_zmag        
##  Min.   :-0.0257220   Min.   :0.7280   Min.   :-0.2538   Min.   :-0.03935  
##  1st Qu.:-0.0099825   1st Qu.:0.7297   1st Qu.:-0.2527   1st Qu.:-0.03661  
##  Median :-0.0038675   Median :0.7303   Median :-0.2521   Median :-0.03574  
##  Mean   :-0.0042643   Mean   :0.7302   Mean   :-0.2521   Mean   :-0.03589  
##  3rd Qu.:-0.0002062   3rd Qu.:0.7307   3rd Qu.:-0.2516   3rd Qu.:-0.03524  
##  Max.   : 0.0191430   Max.   :0.7324   Max.   :-0.2500   Max.   :-0.03311  
##     LL_xacc          LL_yacc          LL_zacc         LL_xgyro         
##  Min.   :-2.868   Min.   :-9.112   Min.   :2.571   Min.   :-0.0246850  
##  1st Qu.:-2.823   1st Qu.:-9.089   1st Qu.:2.608   1st Qu.:-0.0096597  
##  Median :-2.815   Median :-9.089   Median :2.622   Median :-0.0056750  
##  Mean   :-2.815   Mean   :-9.085   Mean   :2.618   Mean   :-0.0050744  
##  3rd Qu.:-2.808   3rd Qu.:-9.075   3rd Qu.:2.625   3rd Qu.:-0.0002395  
##  Max.   :-2.748   Max.   :-9.044   Max.   :2.670   Max.   : 0.0158530  
##     LL_ygro             LL_zgyro            LL_xmag          LL_ymag      
##  Min.   :-0.019207   Min.   :-0.021531   Min.   :0.7375   Min.   :0.2997  
##  1st Qu.:-0.002989   1st Qu.:-0.007927   1st Qu.:0.7390   1st Qu.:0.3007  
##  Median : 0.001615   Median :-0.002556   Median :0.7396   Median :0.3013  
##  Mean   : 0.002281   Mean   :-0.003144   Mean   :0.7396   Mean   :0.3013  
##  3rd Qu.: 0.007602   3rd Qu.: 0.001637   3rd Qu.:0.7402   3rd Qu.:0.3018  
##  Max.   : 0.020288   Max.   : 0.013777   Max.   :0.7424   Max.   :0.3034  
##     LL_zmag        
##  Min.   :-0.05924  
##  1st Qu.:-0.05758  
##  Median :-0.05721  
##  Mean   :-0.05711  
##  3rd Qu.:-0.05650  
##  Max.   :-0.05496
sum(is.na(data))
## [1] 0
data <- data %>% select_if(is.numeric)
data_scaled <- scale(data)

set.seed(123)
kmeans_result <- kmeans(data_scaled, centers = 3, nstart = 25)
fviz_cluster(kmeans_result, data = data_scaled)

dist_matrix <- dist(data_scaled)
hc <- hclust(dist_matrix, method = "ward.D2")

plot(hc)

library(dendextend)
## Warning: package 'dendextend' was built under R version 4.3.3
## 
## ---------------------
## Welcome to dendextend version 1.17.1
## Type citation('dendextend') for how to cite the package.
## 
## Type browseVignettes(package = 'dendextend') for the package vignette.
## The github page is: https://github.com/talgalili/dendextend/
## 
## Suggestions and bug-reports can be submitted at: https://github.com/talgalili/dendextend/issues
## You may ask questions at stackoverflow, use the r and dendextend tags: 
##   https://stackoverflow.com/questions/tagged/dendextend
## 
##  To suppress this message use:  suppressPackageStartupMessages(library(dendextend))
## ---------------------
## 
## Attaching package: 'dendextend'
## The following object is masked from 'package:stats':
## 
##     cutree
res.dist2<-dist(data, method = "euclidean")

res.hcr<-hclust(d = res.dist2, method = "ward.D2")
fviz_dend(res.hcr, cex = 0.5)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

res.hcr1<- hclust(res.dist2, method= "average")
cor(res.dist2, cophenetic(res.hcr1))
## [1] 0.7566942
fviz_dend(res.hcr, cex = 0.5, k_colors=c('brown','yellow','green','blue'), rect = TRUE)

library(dendextend)
res.dsitr<-dist(data, method = "euclidean")
hcr<-hclust(res.dsitr, method = "average")
hcr1<-hclust(res.dsitr, method = "ward.D2")


denr<- as.dendrogram(hcr)
denr2<- as.dendrogram(hcr1)

dend_listr<- dendlist(denr, denr2)

tanglegram(denr, denr2)

dim(data)
## [1] 124  45
sa1<- sample(1:124, 20)
sports<- data[sa1, ]
sports.scaled<- scale(sports)
dim(sports)
## [1] 20 45
res.dsits<- dist(sports, method = "euclidean")
hcs<- hclust(res.dsits, method = "average")
hcss<- hclust(res.dsits, method = "ward.D2")

den34<- as.dendrogram(hcs)
den35<- as.dendrogram(hcss)

dend_lists<- dendlist(den34, den35)
dend_lists
## [[1]]
## 'dendrogram' with 2 branches and 20 members total, at height 0.6251281 
## 
## [[2]]
## 'dendrogram' with 2 branches and 20 members total, at height 1.303784 
## 
## attr(,"class")
## [1] "dendlist"
tanglegram(den34, den35)