Skip to content

Instantly share code, notes, and snippets.

@thistleknot
Last active May 8, 2021
Embed
What would you like to do?
Sorted linked list using factoextra's dist
library("factoextra",lib.loc = "/mnt/distvol/R-4.0.5/library")
library("FactoMineR",lib.loc = "/mnt/distvol/R-4.0.5/library")
library("reshape2",lib.loc = "/mnt/distvol/R-4.0.5/library")
library("data.table",lib.loc = "/mnt/distvol/R-4.0.5/library")
data <- read.csv("/mnt/distvol/pca_dist_scaled.csv", row.names=1)
data2 <- read.csv("/mnt/distvol/states.csv", row.names=1)
returnNorm <- function(df)
{
#print(rownames(df))
d <- lapply(data[,1:ncol(df)], function(x)
{
pnorm(x, mean=0, sd=1, lower.tail=TRUE, log.p =FALSE)
})
d_ <- data.frame(do.call(cbind, d))
rownames(d_) <- rownames(df)
#print(rownames(d_))
return(d_)
}
df <- returnNorm(data)
#df2 <- returnNorm(data2[,1:10])
m = (get_dist(df, method = "pearson"))
m = (as.matrix(m))
nm = m
names = rownames(df)
new_names = names
mi = (min(nm[nm>0]))
s = which(( nm == mi), arr.ind=T )
removed = c()
states <- c()
first = new_names[s[1]]
while(length(removed)<49)
{
states <- c(states, first)
removed = c(removed,first)
print(first)
nm = m[which(names %in% new_names),which(names %in% new_names)]
second_column = nm[,which(new_names==first)]
column_min = min(second_column[second_column>0])
print(column_min)
column_min_id = data.frame(which(second_column == column_min))[,1]
column_min_name = new_names[column_min_id]
new_names = names[-which(names %in% removed)]
first = column_min_name
}
states <- c(states,rownames(df)[-which(names %in% states)])
keyDF <- data.frame(key=states,weight=1:length(states))
df['States'] = names
merged <- merge(df,keyDF,by.x='States',by.y='key',all.x=T,all.y=F)
res <- merged[order(merged$weight),2:10]
rownames(res) <- states
#dist.eucl <- dist(res, method = "euclidean")
dist.eucl <- dist(res, method = "euclidean")
get_clust_tendency(res, n = 49, gradient = gradient_col)
#jpeg('/mnt/distvol/rplot.jpg')
f1 <- fviz_dist(dist.eucl,show_labels = TRUE, order = FALSE)
f1
f2 <- fviz_dist(dist.eucl,show_labels = TRUE, order = TRUE)
f2
#dev.off()
@thistleknot

This comment has been minimized.

Copy link
Owner Author

@thistleknot thistleknot commented May 8, 2021

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment