# code
# for abstain absent against did not vote
# "p" "a" "z" "n" "r"
> wdir <- "C:/Users/vlado/DL/data/USsenate"
> setwd(wdir)
> source("https://raw.githubusercontent.com/bavla/Rnet/master/R/Pajek.R")
> recode <- function(T,p,n,a,r,z){
+ A <- matrix(NA,nrow=nrow(T),ncol=ncol(T))
+ dimnames(A) <- list(rownames(T),colnames(T))
+ A[T=="p"] <- p; A[T=="n"] <- n; A[T=="a"] <- a; A[T=="r"] <- r; A[T=="z"] <- z
+ return(A)
+ }
# US senate
> S <- read.csv("senate106_1.csv",row.names=362,sep=",",head=TRUE)
> dim(S)
> tail(colnames(S))
> party <- S$party
> S <- S[,2:361]
> head(rownames(S))
> T <- matrix(NA,nrow=nrow(S),ncol=ncol(S))
> dimnames(T) <- list(rownames(S),colnames(S))
> T[S==1] <- "p"; T[S==0] <- "n"; T[S==2] <- "z"
> As <- recode(T,1,-1,0,0,0)
> Ats <- recode(t(T),1,-1,0,0,0)
> D <- crossprod(Ats)
> d <- sqrt(diag(D))
> Cos <- diag(1/d) %*% D %*% diag(1/d)
> colnames(Cos) <- rownames(Cos) <- colnames(Ats)
> matrix2net(Cos,Net="cos106-1.net")
> Dcos <- (1-Cos)/2
> diag(Dcos) <- 0
> disDc <- as.dist(Dcos,diag=FALSE,upper=FALSE)
> t <-hclust(disDc,method="complete")
> plot(t,hang=-1,cex=0.7,main="US Senate - Salton / Complete")
> write.table(T,file="Tsenate106_1.csv",sep=";",row.names=TRUE)
> saveRDS(T,file="Tsenate106_1.RDS")
> party <- S$party
> P <- as.integer(factor(party))
> par <- file("party.clu","w")
> cat("% party: 1 - Rep, 2 - Dem\n",file=par)
> n <- length(P)
> cat("*vertices ",n,"\n",file=par)
> for(v in 1:n) cat(P[v],"\n",sep="",file=par)
> close(par)
http://www.sthda.com/english/wiki/beautiful-dendrogram-visualizations-in-r-5-must-known-methods-unsupervised-machine-learning
> library(dendextend)
> dend <- as.dendrogram(t)
> col <- c("blue","red")
> C <- col[as.integer(factor(party[t$order]))]
> dend %>% set("leaves_pch", 16) %>% # node point type
+ set("labels_cex", 0.7) %>% # Change label font size
+ set("leaves_cex", 1) %>% # node point size
+ set("leaves_col", C) %>% #node point color
+ plot(main = "US Senate 106.1 - Salton / Complete")