This is the code used in the slides for the Workshop 'Applying Network Analysis to Psychological Data' at the EFPSA Congress 2016. The slides can be downloaded here http://jmbh.github.io.


```{r}
# load necessary pacakges

library(devtools)
#install_github('SachaEpskamp/qgraph')
#install_github('jmbh/mgm')
library(qgraph)
library(mgm)
library(httr) # downloading data from https

```


# Constructing Networks

```{r}

# construct a network
AdjacencyMatrix <- matrix(0,4,4)
AdjacencyMatrix[1,2] <- AdjacencyMatrix[2,1] <- 1
AdjacencyMatrix[2,3] <- AdjacencyMatrix[3,2] <- 1
AdjacencyMatrix[2,4] <- AdjacencyMatrix[4,2] <- 1

AdjacencyMatrix

qgraph(AdjacencyMatrix) # visualize

```


# Constructing Random Networks

```{r, message=FALSE}

## set up random network

p <- 20 # number of nodes
AdjMatrix <- matrix(0,p,p) #create empty matrix
set.seed(22) #set seed for reproducibility
AdjMatrix[upper.tri(AdjMatrix)] <- sample(0:1,(p*(p-1))/2, 
                                         prob=c(.9,.1),replace=TRUE)
AdjMatrix <- AdjMatrix + t(AdjMatrix) # make symmetric


AdjMatrix[1:5,1:5] # look at edges between first 5 nodes


qgraph(AdjMatrix) # visualize

```



# Correlation Networks


```{r}
url='https://jmbh.github.io/figs/efpsa_workshop/BDIdata.RDS'
GET(url, write_disk("BDIdata.RDS", overwrite=TRUE))
BDI_data <- readRDS('BDIdata.RDS')

# look at data

# data
BDI_data$data[1:3,1:5]
#labels
BDI_data$vnames[1:5]


CorMatrix <- cor(BDI_data$data)
round(CorMatrix[1:4, 1:4],2)

# visualization: ring layout
qgraph(CorMatrix, nodeNames=BDI_data$vnames, legend.cex = .3, vsize=4)

# visualization: 'spring' layout (Fruchterman Reingold algorithm)
qgraph(CorMatrix, nodeNames=BDI_data$vnames, 
       legend.cex = .3, layout='spring', vsize=3)

```



# Conditional Independence Networks

```{r, messages=FALSE}

## BDI Data

# fit conditional independence network
fit <- mgmfit(BDI_data$data, rep('g', 28), rep(1, 28), d=2, pbar = FALSE)

round(fit$wadj[1:4, 1:4],2)


qgraph(fit$wadj, nodeNames=BDI_data$vnames, 
       legend.cex = .3, layout='spring', vsize=3)

```



```{r, fig.width=15, fig.height=9}

## Autism data
url='http://jmbh.github.io/figs/efpsa_workshop/autism_datalist.RDS'
GET(url, write_disk("autism_datalist.RDS", overwrite=TRUE))
Autism_data <- readRDS('autism_datalist.RDS')

# fit mixed graphical model

#fit2 <- mgmfit(Autism_data$data, Autism_data$type, Autism_data$lev, 
#                d=2, pbar=FALSE) # commented out as it takes a while

# instead we download the fit object here:
url='http://jmbh.github.io/figs/efpsa_workshop/fitobj_mixed.RDS'
GET(url, write_disk("fitobj_mixed.RDS", overwrite=TRUE))
fit2 <- readRDS('fitobj_mixed.RDS')

# look at edges between first 10 variables
round(fit2$wadj[1:10, 1:10],2)

# visualize
qgraph(fit2$wadj, nodeNames=Autism_data$colnames, 
       layout='spring', edge.color=fit2$edgecolor, 
       legend.cex=.3, vsize=3, legend.cex=1)



## Make a nicer graph

groups_type <- list("Demographics"=c(1,14,15,28), 
                    "Psychological"=c(2,4,5,6,18,20,21),
                    "Social environment" = c(7,16,17,19,26,27),
                    "Medical"=c(3,8,9,10,11,12,13,22,23,24,25))
group_col <- c("#72CF53", "#53B0CF", "#FFB026", "#ED3939")


```


```{r, fig.width=15, fig.height=9}
# we need this graph for the layout: Q0$layout
Q0 <- qgraph(fit2$adj, 
             vsize=3.5, esize=2.5, layout="spring", edge.color = fit2$edgecolor, 
             color=group_col,
             border.width=1.5,
             border.color="black",
             groups=groups_type,
             nodeNames=Autism_data$colnames,
             legend=TRUE, 
             legend.mode="style2",
             legend.cex=.6)
```


```{r, fig.width=15, fig.height=9}
# Produces the graph in the slides

qgraph(fit2$wadj, 
       vsize=3.5, esize=6, 
       layout=Q0$layout, 
       edge.color = fit2$edgecolor, 
       color=group_col,
       border.width=1.5,
       border.color="black",
       groups=groups_type,
       nodeNames=Autism_data$colnames,
       legend=TRUE, 
       legend.mode="style2",
       legend.cex=.6)
```

