# This file contains rotatable MDS plots, and clustering diagrams, as described in [reference]
library(knitr)
# library(kableExtra)
library(dplyr)

mat <- as.matrix(read.csv("FC_diff_matrix.csv", row.names = 1, check.names = FALSE))

# mat # to see the matrix 
# the following renames the rows and columns with a letter prefix, so that I can use it in the plotting later one. 
# Generate prefixes A, B, C, ... for rows and columatns
row_prefixes <- LETTERS[seq_len(nrow(mat))]
col_prefixes <- LETTERS[seq_len(ncol(mat))]

# Add prefixes to row and columatn namates
rownames(mat) <- paste0(row_prefixes, ":", rownames(mat))
colnames(mat) <- paste0(col_prefixes, ":", colnames(mat))

# mat
# assume your matrix is called mat
row_dist <- dist(mat)                 # distance between rows
# row_dist # display it 
row_hclust <- hclust(row_dist)        # hierarchical clustering
## supress the first plot
##plot(row_hclust, main = "Row Clustering")

#{r} #t(mat) # see transpose #col_dist <- dist(t(mat)) # distance between columns #col_dist # display it #col_hclust <- hclust(col_dist) #plot(col_hclust, main = "Column Clustering") #

mat2 <- mat
for (i in 1:nrow(mat2)) {
  row_mean <- mean(mat2[i,], na.rm = TRUE)
  mat2[i, is.na(mat2[i, ])] <- row_mean
}
# display mat
# mat
#display mat2 has row means. [It is for column analysis ]
# mat2 
write.csv(mat2, file = "FC_mat2.csv", row.names = TRUE)
mat3 <- mat
for (j in 1:ncol(mat3)) {
  col_mean <- mean(mat3[, j], na.rm = TRUE)
  mat3[is.na(mat3[, j]), j] <- col_mean
}
# display mat
# mat 
#display mat3 [has column means ]
# mat3
write.csv(mat3, file = "FC_mat3.csv", row.names = TRUE)
# repeat for the corrected row and column data. 
row_dist <- dist(mat3)                 # distance between rows using column means
# row_dist # display it 
row_hclust <- hclust(row_dist)        # hierarchical clustering
plot(row_hclust, main = "Row Clustering: by distance from predecessor")

# this file contains rotatable 3D plots of the results of MDS scaling as described in [LINK HERE]
# assume your matrix is called mat
row_dist <- dist(t(mat2))                 # distance between columns, using row means
# add a better name
col_dist<-row_dist
# row_dist # display it 
row_hclust <- hclust(row_dist)        # hierarchical clustering
plot(row_hclust, main = "column clustering - by impact on successors")

# for rows

# Row MDS, no colors
display_rotatable_MDS_v2(
  mat3,
  plot_title = "Row MDS:by distance from predecessor ",
  use_cluster_colors = FALSE
)
##   Dimension     Stress
## 1         1 0.22083495
## 2         2 0.11187079
## 3         3 0.06234949
## 4         4 0.02456790
## 5         5 0.01430793
## 6         6 0.01019443
Label:Cluster:Rule
A:0:buckOrd02
B:0:buckOrd13
C:0:buckets_0213
D:0:ccw
E:0:cm_BRYK
F:0:cm_RBKY
G:0:cw
H:0:ordL1
I:0:ordRevOfL1
J:0:quadMixed1
K:0:quadNearby
L:0:sm_csqt
M:0:sm_qcts
# Column MDS, no colors
display_rotatable_MDS_v2(
  mat2,
  plot_title = "Column MDS: by impact on successors",
  use_cluster_colors = FALSE
)
##   Dimension      Stress
## 1         1 0.156750664
## 2         2 0.080093172
## 3         3 0.040122127
## 4         4 0.018180210
## 5         5 0.011757256
## 6         6 0.007812546
Label:Cluster:Rule
A:0:buckOrd02
B:0:buckOrd13
C:0:buckets_0213
D:0:ccw
E:0:cm_BRYK
F:0:cm_RBKY
G:0:cw
H:0:ordL1
I:0:ordRevOfL1
J:0:quadMixed1
K:0:quadNearby
L:0:sm_csqt
M:0:sm_qcts
# Row MDS, 5 cluster colors
display_rotatable_MDS_v2(
  mat3,
  plot_title = "Row MDS: by distance from predecessor ",
  use_cluster_colors = TRUE,
  k_clusters = 5
)
##   Dimension     Stress
## 1         1 0.22083495
## 2         2 0.11187079
## 3         3 0.06234949
## 4         4 0.02456790
## 5         5 0.01430793
## 6         6 0.01019443
Label:Cluster:Rule
A:1:buckOrd02
B:1:buckOrd13
C:2:buckets_0213
D:3:ccw
E:1:cm_BRYK
F:4:cm_RBKY
G:5:cw
H:1:ordL1
I:1:ordRevOfL1
J:2:quadMixed1
K:2:quadNearby
L:2:sm_csqt
M:1:sm_qcts
# Column MDS, 5 cluster colors
display_rotatable_MDS_v2(
  mat2,
  plot_title = "Column MDS:by impact on successors ",
  use_cluster_colors = TRUE,
  k_clusters = 5
)
##   Dimension      Stress
## 1         1 0.156750664
## 2         2 0.080093172
## 3         3 0.040122127
## 4         4 0.018180210
## 5         5 0.011757256
## 6         6 0.007812546
Label:Cluster:Rule
A:1:buckOrd02
B:1:buckOrd13
C:1:buckets_0213
D:2:ccw
E:1:cm_BRYK
F:3:cm_RBKY
G:4:cw
H:1:ordL1
I:1:ordRevOfL1
J:1:quadMixed1
K:1:quadNearby
L:1:sm_csqt
M:5:sm_qcts