- Installing Software & Packages
- Why Networks?
- What are Networks?
- Constructing a Network
- Correlation Networks
- Conditional Independence Networks
May 4, 2016
install.packages('qgraph') # Network visualization install.packages('mgm') # Mixed Graphical Models & mixed VAR install.packages('httr') # Download data files from https
library(qgraph) library(mgm) library(httr)
Download the R-file with all exercises here:
(Here you can also find the slides)
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
qgraph(AdjacencyMatrix)
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
qgraph(AdjMatrix)
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)
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"
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
qgraph(CorMatrix, nodeNames=BDI_data$vnames, legend.cex = .3, vsize=4)
qgraph(CorMatrix, nodeNames=BDI_data$vnames, legend.cex = .3, layout='spring', vsize=3)
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
qgraph(fit$wadj, nodeNames=BDI_data$vnames, legend.cex = .3, layout='spring', vsize=3)
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
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
#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')
qgraph(fit2$wadj, nodeNames=Autism_data$colnames, layout='spring', edge.color=fit2$edgecolor, legend.cex=.3, vsize=3)
# 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)
But there is much more …
Contact:
jonashaslbeck@gmail.com / http://jmbh.github.io (slides+code)
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.
Bringmann et al. (2013). A network approach to psychopathology: new insights into clinical longitudinal data. PloS one, 8(4), e60188.
Haslbeck & Waldorp (under review). mgm: Structure Estimation for Time-Varying Mixed Graphical Models in high-dimensional Data. http://arxiv.org/pdf/1510.06871v2.pdf