setwd("C:/Users/19218/Desktop/bmc/Supplementary")
install.packages(c("bootnet","qgraph","glmnet","networktools"))

library(qgraph) 
library(bootnet) 
library(glmnet) 
library(networktools) 

Data1<-  read.table(file = "T1.csv", sep = ",", header = T) 
Data2<-  read.table(file = "T2.csv", sep = ",", header = T) 


##################################################################################################################################


df=cbind(Data1,Data2)
k <- 23 
adjMat <- matrix(0, k, k) 


for (i in 1:k){
  lassoreg <- cv.glmnet(as.matrix(df[,1:k]), df[,(k+i)], 
                        family = "gaussian", alpha = 1, standardize=TRUE, nfolds = 10)
  lambda <- lassoreg$lambda.min 
  adjMat[1:k,i] <- coef(lassoreg, s = lambda, exact = FALSE)[2:(k+1)]
}

groups <- c(rep("DysExecute", 20), rep("Impulse", 3)) 
labels <- c("D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13","D14","D15","D16","D17","D18","D19","D20","I1","I2","I3") 

Matrix<- qgraph(adjMat,labels = labels, layout="spring",  cut=0.1, groups = groups,theme ="colorblind",colors = c("steelblue","orange")) 
write.csv(adjMat, file="matrix1.csv") 

adjMat2 <- adjMat
diag(adjMat2) <- 0 

Matrix2<- qgraph(adjMat2,labels = labels,layout="spring",  cut=0.1,  groups = groups,theme ="colorblind",colors = c("steelblue","orange")) 
write.csv(adjMat2, file="matrix2.csv" ) 

L<-averageLayout(Matrix, Matrix2) 
qgraph(adjMat,labels = labels, layout=L, cut=0.1, groups = groups,theme ="colorblind",colors = c("steelblue","orange")) 
qgraph(adjMat2,labels = labels,layout=L,  cut=0.1,  groups = groups,theme ="colorblind",colors = c("steelblue","orange")) 

groupsnew <- c("Executive Dysfunction (Working Memory)","Executive Dysfunction (Working Memory)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Working Memory)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Working Memory)","Executive Dysfunction (Working Memory)","Executive Dysfunction (Working Memory)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Working Memory)","Executive Dysfunction (Working Memory)","Executive Dysfunction (Working Memory)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Inhibition)","Executive Dysfunction (Inhibition)",rep("Impulsivity", 3)) 
qgraph(adjMat,labels = labels, layout=L, cut=0.1, groups = groupsnew,theme ="colorblind",colors = c("steelblue","orange","tomato")) 
qgraph(adjMat2,labels = labels,layout=L,  cut=0.1,  groups = groupsnew,theme ="colorblind",colors = c("steelblue","orange","tomato"))

centrality(Matrix) 
centralityPlot(Matrix, scale="raw", include=c("OutExpectedInfluence", "InExpectedInfluence")) 

bridge_centrality<- bridge(Matrix,  communities=groups) 
bridge_centrality
plot(bridge_centrality, include=c("Bridge Expected Influence (1-step)"), zscore=F) 
plot(bridge_centrality, include=c("Bridge Outdegree","Bridge Indegree"), zscore=F) 


#################################################################################################################


df <- as.matrix(cbind(Data1,Data2))
k <- 23
adjMat <- matrix(0, k, k) 

CLPN.fun <- function(df) {
  for (i in 1:k){
    #set.seed(100)
    lassoreg <- cv.glmnet(as.matrix(df[,1:k]), df[,(k+i)], 
                          family = "gaussian", alpha = 1, standardize=TRUE, nfolds = 10)
    lambda <- lassoreg$lambda.min
    adjMat[1:k,i] <- coef(lassoreg, s = lambda, exact = FALSE)[2:(k+1)]
  }
  return(adjMat)
}

groups <- c(rep("DysExecute", 20), rep("Impulse", 3)) 
labels <- c("D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13","D14","D15","D16","D17","D18","D19","D20","I1","I2","I3") 

Network<- estimateNetwork(df, fun = CLPN.fun,labels = labels, directed = TRUE) 

boot1<- bootnet(Network, nCores =1, nBoots = 1000, type = "case", statistics = c("OutExpectedInfluence","InExpectedInfluence","bridgeExpectedInfluence"), communities =groups, useCommunities= "all", directed = T)
plot(boot1, statistics = c("OutExpectedInfluence","InExpectedInfluence","bridgeExpectedInfluence"))
corStability(boot1)
boot2 <- bootnet(Network, directed = T, nCores = 1, nBoots = 1000, type = "nonparametric", statistics = c("edge"))
plot(boot2, statistics = c("edge"), order = "sample") 
plot(boot2, statistics = c("edge"), labels=FALSE,order = "sample")  

boot3 <- bootnet(Network, nBoots = 1000, nCores = 1,statistics = c("bridgeExpectedInfluence", "OutExpectedInfluence","InExpectedInfluence","edge"),communities =groups, useCommunities= "all", directed = T)
plot(boot3, "bridgeExpectedInfluence", plot = "difference", order = "sample")
plot(boot3, "OutExpectedInfluence", plot = "difference", order = "sample") 
plot(boot3, "InExpectedInfluence", plot = "difference", order = "sample") 
plot(boot3, "edge", plot = "difference", onlyNonZero = TRUE, order = "sample") 