# 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
# 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
# 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
# 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