Last updated: 2020-11-04

Checks: 6 1

Knit directory: 2020_ODI_network/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20201008) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 9e388e3. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/2-network.html
    Ignored:    analysis/site_libs/

Untracked files:
    Untracked:  analysis/2-network.knit.md
    Untracked:  analysis/2-network.utf8.md

Unstaged changes:
    Modified:   analysis/1-Explore.Rmd
    Modified:   analysis/2-network.Rmd
    Modified:   analysis/3-report.Rmd
    Modified:   analysis/4-inference.Rmd
    Modified:   analysis/index.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/2-network.Rmd) and HTML (docs/2-network.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 5f1623a bernard-liew 2020-10-13 initial network analysis

Load package

if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse,
               qgraph,
               stats,
               bootnet,
               igraph,
               mgm,
               NetworkComparisonTest,
               rio,
               furrr,
               cowplot,
               doParallel,
               huge,
               EGAnet)

Import data

rm(list = ls())
df <- readRDS("output/dat_odi_nest.RDS")

Reorganize data

temp1 <- df$raw %>% select (-data_mgm)
temp2 <- df$com %>% select (-data_mgm) %>% rename (data_com = data)
temp3 <- temp1 %>%
  inner_join(temp2, by = "time")

temp4 <- df$raw %>% select (-data) %>% rename (data = data_mgm)
temp5 <- df$com %>% select (-data) %>% rename (data_com = data_mgm)
temp6 <- temp4 %>%
  inner_join(temp5, by = "time")

df <- list (odi = temp3, 
            odi_mgm = temp6)

rm (list = ls(pattern = "temp"))

Network analysis

Set boot number

B <- 2000
stats_type <- c("edge", "strength", "betweenness", "expectedInfluence", "closeness")

my_huge <- function (df) {

  df[, grepl ("Q", names(df))] <- huge.npn (df[, grepl ("Q", names(df))] )

  return (df)

}

Analyse without groups as variable

Analyse data with missing

dat <- df$odi 

plan (multisession)
set.seed(1)

res_raw <- dat %>%
  select (time, data) %>%
  # Transform data
  mutate (data = map(data, huge.npn)) %>%
  # Estimate network
  mutate (nw = map (data,
                    estimateNetwork,
                    default="EBICglasso",
                    corMethod = "cor",
                    tuning = 0.5,
                    lambda.min.ratio = 0.001,
                    corArgs =
                      list(method = "pearson",
                           use = "pairwise.complete.obs")),
          # Get centrality measures
          centr = map (nw, centralityTable),
          # Bootstrap by case to get stability of centrality
          centr_stb = future_map (nw,
                                  bootnet,
                                  default="EBICglasso",
                                  corMethod = "cor",
                                  tuning = 0.5,
                                  lambda.min.ratio = 0.001,
                                  nBoots = B,
                                  type = "case",
                                  statistics = stats_type,
                                  corArgs =
                                    list(method = "pearson",
                                         use = "pairwise.complete.obs")),
          # Stability measure
          cor_stb = map (centr_stb,
                         corStability),
          # Bootstrap to get edgeweights stability
          edgewts = future_map (nw,
                                bootnet,
                                default="EBICglasso",
                                corMethod = "cor",
                                tuning = 0.5,
                                lambda.min.ratio = 0.001,
                                nBoots = B,
                                corArgs =
                                  list(method = "pearson",
                                       use = "pairwise.complete.obs")))

saveRDS(res_raw, "output/raw.RDS")

Analyse data with complete

plan (multisession)
set.seed(1)

res_com <- dat %>%
  select (time, data_com) %>%
  rename (data = data_com) %>%
  # Transform data
  mutate (data = map(data, huge.npn)) %>%
  # Estimate network
  mutate (nw = map (data,
                    estimateNetwork,
                    default="EBICglasso",
                    corMethod = "cor",
                    tuning = 0.5,
                    lambda.min.ratio = 0.001,
                    corArgs =
                      list(method = "pearson",
                           use = "pairwise.complete.obs")),
          # Get centrality measures
          centr = map (nw, centralityTable),
          # Bootstrap by case to get stability of centrality
          centr_stb = future_map (nw,
                                  bootnet,
                                  default="EBICglasso",
                                  corMethod = "cor",
                                  tuning = 0.5,
                                  lambda.min.ratio = 0.001,
                                  nBoots = B,
                                  type = "case",
                                  statistics = stats_type,
                                  corArgs =
                                    list(method = "pearson",
                                         use = "pairwise.complete.obs")),
          # Stability measure
          cor_stb = map (centr_stb,
                         corStability),
          # Bootstrap to get edgeweights stability
          edgewts = future_map (nw,
                                bootnet,
                                default="EBICglasso",
                                corMethod = "cor",
                                tuning = 0.5,
                                lambda.min.ratio = 0.001,
                                nBoots = B,
                                corArgs =
                                  list(method = "pearson",
                                       use = "pairwise.complete.obs")))

saveRDS(res_com, "output/com.RDS")

Analyse with groups as variable

Analyse data with missing

dat <- df$odi_mgm 
set.seed(1)

res_mgm_raw <- dat %>%
  select (time, data) %>%
  # Transform data
  mutate (data = map(data, my_huge)) %>%
  # Estimate network
  mutate (nw = map (data,
                    estimateNetwork,
                    default = "mgm",
                     type= c("c", rep("g", 10)),
                     level= c(2, rep(1, 10)),
                     criterion  = "CV",            # we used cross validation to select optimal tuning parameter
                     nFolds = 10,            # using 10 folds
                     order = 2,                       # we only include second order interactions
                     binarySign = TRUE,
                     scale = TRUE,
                    .pbar = FALSE,
                    .signInfo = FALSE)) %>%
  # Get centrality measures
  mutate (centr = map (nw, centralityTable)) 


# Furrr throws up error with mgm, switched to doParallel
# Bootstrap by case to get stability of centrality
doParallel::registerDoParallel(4)
B <- 2000

centr_stb_list <- list()

centr_stb_list <-  foreach (n = 1: nrow (dat)) %dopar% {
  bootnet::bootnet (res_mgm_raw$data[[n]],
                     default = "mgm",
                     .type= c("c", rep("g", 10)),
                     level= c(2, rep(1, 10)),
                     criterion  = "CV",          
                     nFolds = 10,            
                     order = 2,                       
                     binarySign = TRUE,
                     scale = TRUE,
                    .pbar = FALSE,
                    .signInfo = FALSE,
                    nBoots = B,
                    type = "case",
                    statistics = stats_type)
  
}

res_mgm_raw$centr_stb <- centr_stb_list

# Stability measure
res_mgm_raw <- res_mgm_raw %>%
  mutate (cor_stb = map (centr_stb,
                         corStability))

# Bootstrap by case to get stability of centrality
edgewts_list <- list()

edgewts_list <-  foreach (n = 1: nrow (dat)) %dopar% {
  bootnet::bootnet (res_mgm_raw$nw[[n]],
                    default = "mgm",
                     .type= c("c", rep("g", 10)),
                     level= c(2, rep(1, 10)),
                     criterion  = "CV",          
                     nFolds = 10,            
                     order = 2,                       
                     binarySign = TRUE,
                     scale = TRUE,
                    .pbar = FALSE,
                    .signInfo = FALSE,
                    nBoots = B)
  
}

res_mgm_raw$edgewts <- edgewts_list

saveRDS(res_mgm_raw, "output/mgm_raw.RDS")

Analyse data with complete

set.seed(1)

res_mgm_com <- dat %>%
  select (time, data_com) %>%
  rename (data = data_com) %>%
  # Transform data
  mutate (data = map(data, my_huge)) %>%
  # Estimate network
  mutate (nw = map (data,
                    estimateNetwork,
                    default = "mgm",
                     type= c("c", rep("g", 10)),
                     level= c(2, rep(1, 10)),
                     criterion  = "CV",            # we used cross validation to select optimal tuning parameter
                     nFolds = 10,            # using 10 folds
                     order = 2,                       # we only include second order interactions
                     binarySign = TRUE,
                     scale = TRUE,
                    .pbar = FALSE,
                    .signInfo = FALSE)) %>%
  # Get centrality measures
  mutate (centr = map (nw, centralityTable)) 


# Furrr throws up error with mgm, switched to doParallel
# Bootstrap by case to get stability of centrality
doParallel::registerDoParallel(4)
B <- 2000

centr_stb_list <- list()

centr_stb_list <-  foreach (n = 1: nrow (dat)) %dopar% {
  bootnet::bootnet (res_mgm_com$data[[n]],
                     default = "mgm",
                     .type= c("c", rep("g", 10)),
                     level= c(2, rep(1, 10)),
                     criterion  = "CV",          
                     nFolds = 10,            
                     order = 2,                       
                     binarySign = TRUE,
                     scale = TRUE,
                    .pbar = FALSE,
                    .signInfo = FALSE,
                    nBoots = B,
                    type = "case",
                    statistics = stats_type)
  
}

res_mgm_com$centr_stb <- centr_stb_list

# Stability measure
res_mgm_com <- res_mgm_com %>%
  mutate (cor_stb = map (centr_stb,
                         corStability))

# Bootstrap by case to get stability of centrality
doParallel::registerDoParallel(4)
edgewts_list <- list()

edgewts_list <-  foreach (n = 1: nrow (dat)) %dopar% {
  bootnet::bootnet (res_mgm_com$nw[[n]],
                    default = "mgm",
                     .type= c("c", rep("g", 10)),
                     level= c(2, rep(1, 10)),
                     criterion  = "CV",          
                     nFolds = 10,            
                     order = 2,                       
                     binarySign = TRUE,
                     scale = TRUE,
                    .pbar = FALSE,
                    .signInfo = FALSE,
                    nBoots = B)
  
}

res_mgm_com$edgewts <- edgewts_list

saveRDS(res_mgm_com, "output/mgm_com.RDS")

sessionInfo()