Last updated: 2021-04-14

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 225cdea. 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:    .Rproj.user/

Unstaged changes:
    Modified:   analysis/2-network.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 ee49c01 Liew 2021-01-08 redo analysis post update
Rmd b243fbf Liew 2021-01-07 redo analysis after updating bootneet
Rmd 661e55c Liew 2020-12-18 augmented mgm code to furrr and removed missing first
html 8bd4b9a bernard-liew 2020-11-04 Build site.
Rmd 502ad53 bernard-liew 2020-11-04 build contents for web
html 502ad53 bernard-liew 2020-11-04 build contents for web
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::huge.npn (df[, grepl ("Q", names(df))] )

  return (df)

}

Analyse with groups as variable

#### Analyse data with missing
dat <- df$odi_mgm 
set.seed(1)

res_mgm_raw <- dat %>%
  select (time, data) %>% 
  # remove cases with missing columns
  mutate (data = map (data, na.omit)) %>%
  # 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)) 


# Bootstrap by case to get stability of centrality
B <- 2000
plan (multisession)

res_mgm_raw <- res_mgm_raw %>%
  mutate (centr_stb = future_map (nw, 
                                  bootnet, 
                                  nBoots = B,
                                  type = "case",
                                  statistics = stats_type,
                                  .options = furrr_options(seed = TRUE)))

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

# Bootstrap by case to get stability of centrality
plan (multisession)

res_mgm_raw <- res_mgm_raw %>%
  mutate (edgewts = future_map (nw, 
                                  bootnet, 
                                  nBoots = B,
                                .options = furrr_options(seed = TRUE)))


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

sessionInfo()