May 4, 2016

Overview

  1. Installing Software & Packages
  2. Why Networks?
  3. What are Networks?
  4. Constructing a Network
  5. Correlation Networks
  6. Conditional Independence Networks

Installing Software & Packages

Download R - file with code of all exercises

What are Networks?

Nodes = Entities; Edges = Relations

Computer Network

Network of Corporate Control

Vitali et al. 2011

Facebook Friendship network

Psychopathological Classification Network

(Borsboom et al., 2011)

Psychopathological Symptom Network

Different types of Networks

Let's 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
##      [,1] [,2] [,3] [,4]
## [1,]    0    1    0    0
## [2,]    1    0    1    1
## [3,]    0    1    0    0
## [4,]    0    1    0    0

Our first network visualization

qgraph(AdjacencyMatrix)

Generate a random network

p <- 20
AdjMatrix <- matrix(0,p,p)
set.seed(22)
AdjMatrix[upper.tri(AdjMatrix)] <- sample(0:1,(p*(p-1))/2, 
                                         prob=c(.9,.1),replace=TRUE)
AdjMatrix <- AdjMatrix + t(AdjMatrix)

AdjMatrix[1:5,1:5]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    0    0    0    0
## [2,]    0    0    1    0    0
## [3,]    0    1    0    0    0
## [4,]    0    0    0    0    0
## [5,]    0    0    0    0    0

Visualize the Random Network

qgraph(AdjMatrix)

Correlation Networks

Correlation Networks - Load data

Load Depression data from web:

url='https://jmbh.github.io/figs/efpsa_workshop/BDIdata.RDS'
GET(url, write_disk("BDIdata.RDS", overwrite=TRUE))
## Response [https://jmbh.github.io/figs/efpsa_workshop/BDIdata.RDS]
##   Date: 2016-05-14 09:25
##   Status: 200
##   Content-Type: application/octet-stream
##   Size: 34.3 kB
## <ON DISK>  C:\Users\jo\Dropbox\MyData\_PhD\_Talks\efpsa_congress_2016\na_workshop\BDIdata.RDS
BDI_data <- readRDS('BDIdata.RDS')

3640 Responses to Beck's Depression Inventory (BDI)

Correlation Networks - look at data

Let's have a first look at the data:

# data
BDI_data$data[1:3,1:5]
##      aids01 aids02 aids03 aids04 aids05
## [1,]      1      3      1      2      1
## [2,]      3      4      2      1      1
## [3,]      1      1      1      2      1
#labels
BDI_data$vnames[1:5]
## [1] "Falling Asleep"         "Sleep During the Night"
## [3] "Waking Up Too Early"    "Sleeping Too Much"     
## [5] "Feeling Sad"

Compute Correlation Matrix

CorMatrix <- cor(BDI_data$data)

round(CorMatrix[1:4, 1:4],2)
##        aids01 aids02 aids03 aids04
## aids01   1.00   0.28   0.22   0.02
## aids02   0.28   1.00   0.34  -0.06
## aids03   0.22   0.34   1.00  -0.10
## aids04   0.02  -0.06  -0.10   1.00

Visualize Correlation Matrix

qgraph(CorMatrix, nodeNames=BDI_data$vnames, legend.cex = .3, vsize=4)

Visualize Correlation Matrix

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

The Problem with spurious correlations

Conditional Independence Networks

Conditional Independence Networks - Estimation

fit <- mgmfit(BDI_data$data, rep('g', 28), rep(1, 28), pbar=FALSE)

round(fit$wadj[1:4, 1:4],2)
##      [,1] [,2] [,3] [,4]
## [1,] 0.00 0.13 0.03 0.00
## [2,] 0.13 0.00 0.22 0.00
## [3,] 0.03 0.22 0.00 0.05
## [4,] 0.00 0.00 0.05 0.00

Conditional Independence Networks - Visualization

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

Conditional Independence Networks from Mixed Data

Mixed Data: Autism Dataset

Load data from the web:

url='http://jmbh.github.io/figs/efpsa_workshop/autism_datalist.RDS'
GET(url, write_disk("autism_datalist.RDS", overwrite=TRUE))
## Response [http://jmbh.github.io/figs/efpsa_workshop/autism_datalist.RDS]
##   Date: 2016-05-14 09:25
##   Status: 200
##   Content-Type: application/octet-stream
##   Size: 99.7 kB
## <ON DISK>  C:\Users\jo\Dropbox\MyData\_PhD\_Talks\efpsa_congress_2016\na_workshop\autism_datalist.RDS
Autism_data <- readRDS('autism_datalist.RDS')

dim(Autism_data$data)
## [1] 3521   28

Mixed Autism Dataset - Look at data

Autism_data$data[1:3, 1:5]
##   Gender IQ Age diagnosis Openness about Diagnosis Success selfrating
## 1      1  6    -0.9605781                        1               2.21
## 2      2  6    -0.5156103                        1               6.11
## 3      1  5    -0.7063108                        2               5.62
Autism_data$type # type of each variable
##  [1] "c" "g" "g" "c" "g" "c" "c" "p" "p" "p" "p" "p" "p" "c" "p" "c" "g"
## [18] "p" "p" "p" "p" "g" "g" "g" "g" "g" "c" "g"
Autism_data$lev # no of categories of each variable
##  [1] 2 1 1 2 1 5 3 1 1 1 1 1 1 2 1 4 1 1 1 1 1 1 1 1 1 1 3 1

Mixed Autism Dataset - Estimation

#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))
## Response [http://jmbh.github.io/figs/efpsa_workshop/fitobj_mixed.RDS]
##   Date: 2016-05-14 09:25
##   Status: 200
##   Content-Type: application/octet-stream
##   Size: 21.2 kB
## <ON DISK>  C:\Users\jo\Dropbox\MyData\_PhD\_Talks\efpsa_congress_2016\na_workshop\fitobj_mixed.RDS
fit2 <- readRDS('fitobj_mixed.RDS')

Mixed Autism Dataset - Visualization

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

Mixed Autism Dataset - Visualization II

# define groups of variables
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))

# define nice colors for groups
group_col <- c("#72CF53", "#53B0CF", "#FFB026", "#ED3939")


qgraph(fit2$wadj, nodeNames=Autism_data$colnames, 
       layout='spring', edge.color=fit2$edgecolor, 
       legend.cex=.3, vsize=3, color=group_col, 
       groups=groups_type)

Mixed Autism Dataset - Visualization II

What have we learned?

  • Set up a (random) Nework in R
  • Compute and Visualize Correlation Network
  • Estimate and Visualize Conditional Independence Networks

But there is much more …

  • Closer Examination of Categorical Variables
  • Directed Networks
  • Networks that change over time

Contact:

jonashaslbeck@gmail.com / http://jmbh.github.io (slides+code)

Suggested Reading

  • Introductory paper on Network Analysis in Psychology using R

Costantini et al. (2015). State of the aRt personality research: A tutorial on network analysis of personality data in R. Journal of Research in Personality, 54, 13-29.

  • Paper introducing (directed) VAR models in psychopathology:

Bringmann et al. (2013). A network approach to psychopathology: new insights into clinical longitudinal data. PloS one, 8(4), e60188.

  • Paper describing all functionalities of the mgm package:

Haslbeck & Waldorp (under review). mgm: Structure Estimation for Time-Varying Mixed Graphical Models in high-dimensional Data. http://arxiv.org/pdf/1510.06871v2.pdf