###Libraries needed to run the functions###
library("FactoMineR")
library("factoextra")
library("ggplot2")
library("devtools")

###Clean
rm(list=ls()) 

#Set the folder path
setwd("C:/Users/......)

#Read the .csv data
dados<-read.csv("SI_database.csv",sep=";",header=TRUE)

nrow(dados) #Determine the number of lines

ncols<-6 #Determine the number of columns

#Create a matrix with data to be analyzed
workingdata<-matrix(nrow=nrow(dados),ncol=ncols)
#Fill workindata[] with database data
for (i in 1:6){workingdata[,i]<-dados[,(i+3)]}

###########################################
#Input of symptoms and risky factors#######
###########################################

sintoma1<- "Dysphagia"
sintoma2<- "Jaundice"
Frisk1<- "Obesity"
Frisk2<- "Diet"

###Check if the inputs are in the workingdata matrix###############
###if not workindata[] == 0, if yes, count the number of times each one appear##
if(sintoma1!=0){
for (i in 1:nrow(dados)){
  aux<-0
  for(j in 1:ncols){
  if((workingdata[i,j]==sintoma1)|(workingdata[i,j]==Frisk1)
     |(workingdata[i,j]==sintoma2)|(workingdata[i,j]==Frisk2))
  {
  aux<-aux+1
  }
 }
  if(aux==0){workingdata[i,]<-0}
}
}

#######################################################
##Routine determining the number of rows of the final##
#matrix workingdata2 ##################################
aux<-0
for (i in 1:nrow(dados)){
  if(workingdata[i,1]!=0){
    aux<-aux+1
  }
}
nlin2<-aux
workingdata2<-matrix(nrow=nlin2,ncol=ncols)

#######################################################
#Naming rows and columns###############################
row.names(workingdata)<-dados$disease
colnames(workingdata)<-c("symptom1","symptom2","symptom3","Risk1","Risk2","Risk3")
########################

#Creating matrix with symptom and frequency columns####
datahisto<-workingdata
v<-data.frame(1,1)
colnames(v)<-c("symptoms","counts")
                         
numcol<-ncol(datahisto)
numrow<-nrow(datahisto)
aux<-0
aux2<-1

for (h in 1:numcol){
 for(g in 1:numrow){
    for (j in 1:numcol){
     for(i in 1:numrow){
     if(workingdata[g,h]!=0 & workingdata[g,h]==datahisto[i,j]){
      aux<-aux+1
      datahisto[i,j]<-"o"
     }
     }
    }
    if(aux > 0){
       v[aux2,1]<-workingdata[g,h]
       v[aux2,2]<-aux
       aux2<-aux2+1
       aux<-0}
    }}

###Clean database "None"=0 and "Non-specific"=0 tags####
for(i in 1:nrow(v)){
  if(v[i,1]=="None" | v[i,1]=="Non-Specific"){
    v[i,2]<-0
  }
}

###Reduce the initial matrix to########
###symptoms and risk factors matrix####
aux<-1
for (q in 1:(nrow(workingdata))){
  if (workingdata[q,2]!=0){
    for (k in 1:(ncol(workingdata))){
    workingdata2[aux,k]<-workingdata[q,k]
    }
    aux<-aux+1
  }
}

###Create matrix with symptoms and risk factors##
###as variables and their frequencies as values####
rowname_matrix3<-data.frame(1,1)

workingdata3<-matrix(data=0,nrow=nlin2,ncol=nrow(v))

colnames(workingdata3)<-v$symptoms #give symptoms as column names

##Give row names corresponding to diseases#########
aux<-1
for (q in 1:(nrow(workingdata))){
  if (workingdata[q,2]!=0){
    rowname_matrix3[aux,1]<-dados[q,2]
    aux<-aux+1
  }
  }
row.names(workingdata3)<-rowname_matrix3$X1

##Attribute frequency values of each symptom/risk factor#
##as values of workindata3############################### 

  for (k in 1:nrow(workingdata2)){ 
   for (q in 1:ncol(workingdata2)){  
     for (i in 1:nrow(v)){  
      if(workingdata2[k,q]==v[i,1]){
        workingdata3[k,i]<-v[i,2]} 
    }
   }
  }

####Routine to determine PCA through prcomp()#####################
my_data.pca <- prcomp(workingdata3, scale = FALSE)
##Show the Percentage of variances explained of each principal component###
fviz_eig(my_data.pca)

####Show the Biplot of the most frequent symptoms or RF (max 10)###
####in dimensions 1 and 2#########################################
fviz_pca_biplot(my_data.pca, axes = c(1,2), title = "PCA", legend = "bottom", repel = TRUE,
             select.var = list(cos2=0.5), labelsize = 5, pointsize = 2,
             col.ind = "cos2", # Color by the quality of representation
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"))

#####Routines to perform HCA analysis############
#Calculating distances#####################
escala<-scale(workingdata3, center = FALSE, scale = FALSE)
caldist<-dist(escala,method="euclidean")
distancias<-as.matrix(caldist)
##Calculate clusters using the method ward.D2#####
hc<-hclust(caldist, method="ward.D2")

#####Define number of clusters k##################
#hc$height
a<-hc$height
nclusters<-data.frame(a)

#V[1,2] is the highest frequency of symptom or risk factor###
cutlimit<-v[1,2]
aux<-1
for(i in 1:nrow(nclusters)){
if(nclusters[i,1]>cutlimit){aux<-aux+1}}
kk<-aux

#############################################
### Show dendrogram #########################
fviz_dend(hc, cex = 1,k = kk, horiz = TRUE, labels_track_height = 20)
