Last updated: 2020-10-21

Checks: 7 0

Knit directory: 2020_LBPcausal/

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.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

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(20200422) 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 e8814e2. 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:    code/network_WIP.R
    Ignored:    code/network_WIP_mgm.R
    Ignored:    output/data_clean.xlsx
    Ignored:    output/image/
    Ignored:    output/odi_network.tiff
    Ignored:    output/odi_nw.RDS
    Ignored:    output/odi_stability.tiff
    Ignored:    output/odi_strength.tiff
    Ignored:    output/orebro_nw.RDS

Untracked files:
    Untracked:  output/temp.RData

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/BN_analysis.Rmd) and HTML (docs/BN_analysis.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 71501c3 bernard-liew 2020-10-21 added analysis files for publication

Load library

# Helper packages
library (tidyverse)
library (tidyselect)
library (arsenal)
library (janitor)
library (magrittr)
library (Rgraphviz)
library (corrr)
library (corrplot)

# Import
library(readxl)
library (xlsx)

# Missing data
library (mice)
library (VIM)

# Modelling
library (bnlearn)
library (caret)

# Parallel
library (doParallel)

Introduction

This is a bayesian network analysis where the early change variables are between week 10 and baseline. The late change variables are between week 52 and week 10.

Import data

rm (list = ls())
df_list <- readRDS("output/df_change.RDS")

Subset data

df1 <- df_list[["wk10_base"]]
names(df1)[grepl("wk", names (df1))] <- paste0(str_remove(names(df1)[grepl("wk", names (df1))], "wk10_"), "_early")

df2 <- df_list[["wk52_wk10"]]
names(df2)[grepl("wk", names (df2))] <- paste0(str_remove(names(df2)[grepl("wk", names (df2))], "wk52_"), "_late")

df3 <- df_list[["wk52"]]
df3 <- df3 %>%
  select (id, grp, subgrp, wk52_lbp, wk52_lp, wk52_osw, wk52_func) 
names(df3)[grepl("wk", names (df3))] <- paste0(str_remove(names(df3)[grepl("wk", names (df3))], "wk52_"), "_outcome")

df <- df1 %>%
  inner_join (df2, by = c("id", "subgrp", "grp")) %>%
  inner_join (df3, by = c("id", "subgrp", "grp")) %>%
  #select (-contains ("func_"))%>%
  rename (disability_early = osw_early,
          disability_late = osw_late,
          disability_outcome = osw_outcome)

Exploratory analysis

Between group comparisons

tableby (subgrp ~., data = df, digits = 2, digits.p = 2) %>%    
  summary () 
dhr (N=54) nrdp (N=7) rdp (N=63) mfp (N=91) mtg (N=74) Total (N=289) p value
disability_early 0.35
   N-Miss 0 0 1 2 1 4
   Mean (SD) -12.63 (14.97) -10.29 (7.16) -7.97 (10.81) -9.58 (12.55) -11.23 (13.56) -10.25 (12.88)
   Range -42.00 - 30.00 -16.00 - 4.00 -34.00 - 14.00 -36.00 - 38.00 -44.00 - 30.00 -44.00 - 38.00
lbp_early 0.32
   N-Miss 0 0 0 0 1 1
   Mean (SD) -1.84 (2.47) -1.86 (1.46) -2.54 (2.26) -2.13 (2.31) -2.62 (2.57) -2.28 (2.39)
   Range -6.00 - 3.00 -3.00 - 1.00 -8.00 - 3.00 -7.00 - 4.00 -8.00 - 4.00 -8.00 - 4.00
lp_early 0.68
   N-Miss 0 1 14 15 10 40
   Mean (SD) -2.53 (2.82) -2.67 (1.86) -2.20 (2.78) -1.84 (2.49) -2.30 (3.04) -2.20 (2.75)
   Range -8.00 - 3.00 -5.00 - 0.00 -8.00 - 3.00 -6.00 - 5.00 -9.00 - 5.00 -9.00 - 5.00
pain_cope_early 0.38
   N-Miss 3 0 0 4 3 10
   Mean (SD) -0.96 (2.84) 0.29 (3.40) -1.57 (3.14) -1.12 (3.33) -1.70 (3.19) -1.30 (3.17)
   Range -7.00 - 6.00 -4.00 - 4.00 -9.00 - 6.00 -9.00 - 9.00 -10.00 - 6.00 -10.00 - 9.00
anx_early 0.16
   N-Miss 3 0 0 3 3 9
   Mean (SD) -1.57 (2.95) -3.86 (2.73) -1.74 (2.64) -1.33 (2.93) -2.00 (2.72) -1.70 (2.83)
   Range -8.00 - 4.00 -9.00 - -1.00 -7.00 - 4.00 -8.00 - 5.00 -9.00 - 4.00 -9.00 - 5.00
depress_early 0.12
   N-Miss 3 0 2 3 4 12
   Mean (SD) -1.59 (2.82) -3.29 (3.50) -1.20 (2.82) -0.68 (2.96) -1.00 (2.88) -1.11 (2.92)
   Range -8.00 - 5.00 -9.00 - 1.00 -8.00 - 6.00 -7.00 - 8.00 -8.00 - 6.00 -9.00 - 8.00
pain_persist_early 0.61
   N-Miss 3 0 1 4 3 11
   Mean (SD) -2.08 (3.52) -3.86 (2.19) -1.98 (3.48) -1.93 (2.68) -2.30 (3.40) -2.11 (3.20)
   Range -9.00 - 7.00 -7.00 - -1.00 -10.00 - 6.00 -8.00 - 5.00 -9.00 - 6.00 -10.00 - 7.00
work_expect_early 0.32
   N-Miss 9 0 0 5 3 17
   Mean (SD) -0.35 (2.95) -0.86 (1.07) -0.28 (2.40) 0.34 (2.14) -0.17 (2.11) -0.08 (2.33)
   Range -8.00 - 5.51 -3.00 - 0.00 -9.11 - 7.00 -5.00 - 10.00 -8.00 - 7.13 -9.11 - 10.00
sleep_early 0.28
   N-Miss 3 0 0 2 3 8
   Mean (SD) -2.00 (2.76) -2.29 (2.69) -1.05 (2.80) -1.78 (2.54) -1.34 (2.81) -1.56 (2.72)
   Range -9.00 - 4.00 -7.00 - 1.00 -6.00 - 10.00 -8.00 - 4.00 -9.00 - 8.00 -9.00 - 10.00
func_early 0.18
   N-Miss 3 0 0 2 3 8
   Mean (SD) -6.28 (6.36) -5.43 (8.60) -2.63 (8.58) -5.01 (8.42) -3.97 (8.88) -4.46 (8.29)
   Range -26.00 - 7.00 -18.00 - 11.00 -23.00 - 27.00 -29.00 - 14.00 -24.00 - 25.00 -29.00 - 27.00
fear_early 0.76
   N-Miss 6 1 1 3 4 15
   Mean (SD) -3.15 (7.57) -5.80 (11.28) -2.28 (6.39) -3.32 (6.73) -2.88 (7.02) -3.00 (6.97)
   Range -19.00 - 11.00 -22.00 - 8.00 -17.00 - 18.00 -24.00 - 12.00 -25.00 - 13.00 -25.00 - 18.00
grp 1.00
   advice 26 (48.1%) 3 (42.9%) 30 (47.6%) 43 (47.3%) 35 (47.3%) 137 (47.4%)
   individualisedphysio 28 (51.9%) 4 (57.1%) 33 (52.4%) 48 (52.7%) 39 (52.7%) 152 (52.6%)
id < 0.01
   Mean (SD) 1526.96 (209.06) 4573.71 (56.44) 5529.14 (211.09) 2514.09 (228.03) 3515.42 (234.58) 3293.19 (1403.85)
   Range 1101.00 - 1806.00 4501.00 - 4652.00 5101.00 - 5903.00 2101.00 - 2951.00 3101.00 - 3905.00 1101.00 - 5903.00
disability_late 0.84
   N-Miss 1 0 7 6 3 17
   Mean (SD) -5.72 (14.45) -4.86 (5.15) -3.54 (9.44) -3.97 (12.63) -3.35 (11.33) -4.08 (11.92)
   Range -52.00 - 28.00 -12.00 - 4.00 -40.00 - 24.00 -45.12 - 34.00 -28.00 - 36.00 -52.00 - 36.00
lbp_late 0.24
   N-Miss 1 0 6 4 2 13
   Mean (SD) -0.13 (2.13) -1.14 (3.39) 0.05 (1.65) -0.55 (2.52) -0.72 (2.32) -0.40 (2.27)
   Range -7.00 - 5.00 -7.00 - 4.00 -4.00 - 4.00 -7.00 - 6.00 -8.00 - 5.50 -8.00 - 6.00
lp_late 0.37
   N-Miss 1 1 19 18 11 50
   Mean (SD) -0.77 (2.65) -0.67 (1.75) 0.00 (1.67) -0.76 (2.93) -0.12 (2.64) -0.45 (2.58)
   Range -8.00 - 4.00 -3.00 - 2.00 -4.00 - 4.00 -10.00 - 7.00 -9.00 - 9.00 -10.00 - 9.00
pain_cope_late 0.74
   N-Miss 4 0 11 18 12 45
   Mean (SD) -0.33 (2.93) -1.55 (1.88) -0.63 (2.41) -0.22 (3.45) -0.08 (3.72) -0.33 (3.18)
   Range -7.00 - 8.00 -4.00 - 1.00 -8.00 - 5.00 -9.12 - 10.00 -8.00 - 9.00 -9.12 - 10.00
anx_late 0.71
   N-Miss 4 0 11 18 12 45
   Mean (SD) -0.56 (2.35) -0.86 (1.86) 0.18 (2.51) -0.23 (3.21) -0.20 (2.95) -0.22 (2.80)
   Range -7.00 - 4.00 -4.00 - 1.00 -8.46 - 6.00 -9.00 - 9.00 -6.00 - 7.00 -9.00 - 9.00
depress_late 0.31
   N-Miss 4 0 11 18 12 45
   Mean (SD) -0.16 (2.06) -1.14 (1.21) 0.24 (2.18) 0.20 (2.55) -0.42 (2.48) -0.06 (2.34)
   Range -6.00 - 5.00 -3.00 - 0.00 -7.00 - 7.00 -7.00 - 8.00 -7.00 - 7.00 -7.00 - 8.00
pain_persist_late 0.38
   N-Miss 4 0 11 18 12 45
   Mean (SD) -0.12 (3.90) -2.43 (2.37) 0.34 (3.68) -0.22 (3.76) 0.33 (3.65) -0.00 (3.72)
   Range -9.00 - 8.00 -5.00 - 1.00 -10.00 - 9.00 -10.00 - 7.00 -7.50 - 9.00 -10.00 - 9.00
work_expect_late 0.97
   N-Miss 4 0 11 18 12 45
   Mean (SD) 0.32 (3.55) 0.00 (0.82) 0.03 (1.35) 0.20 (2.31) 0.13 (1.72) 0.17 (2.29)
   Range -7.00 - 10.00 -1.00 - 1.00 -4.00 - 4.00 -10.00 - 8.00 -5.00 - 6.00 -10.00 - 10.00
sleep_late 0.74
   N-Miss 4 0 11 17 12 44
   Mean (SD) 0.12 (2.09) -0.14 (2.12) -0.21 (1.35) 0.03 (2.65) -0.38 (2.11) -0.11 (2.15)
   Range -5.00 - 8.00 -2.00 - 4.00 -3.00 - 4.00 -8.00 - 9.00 -9.00 - 5.00 -9.00 - 9.00
func_late 0.10
   N-Miss 4 0 11 17 12 44
   Mean (SD) -2.37 (6.69) -4.29 (6.07) -1.58 (4.38) 0.63 (7.89) -1.15 (7.66) -1.04 (6.98)
   Range -22.00 - 12.00 -16.00 - 2.00 -15.00 - 7.00 -17.00 - 30.00 -21.00 - 28.00 -22.00 - 30.00
fear_late 0.50
   N-Miss 4 0 11 17 12 44
   Mean (SD) -1.23 (5.71) 3.26 (3.75) -0.73 (6.18) -0.77 (6.94) -1.27 (6.61) -0.87 (6.39)
   Range -14.00 - 10.00 -2.00 - 10.00 -13.00 - 14.00 -17.00 - 15.46 -15.00 - 14.00 -17.00 - 15.46
lbp_outcome 0.06
   N-Miss 1 0 6 4 1 12
   Mean (SD) 2.98 (2.27) 3.57 (2.70) 2.47 (2.16) 3.01 (2.53) 2.10 (1.88) 2.67 (2.27)
   Range 0.00 - 8.00 1.00 - 7.00 0.00 - 8.00 0.00 - 9.00 0.00 - 8.00 0.00 - 9.00
lp_outcome 0.35
   N-Miss 1 1 19 18 10 49
   Mean (SD) 2.49 (2.59) 2.00 (2.10) 1.98 (2.29) 2.08 (2.52) 1.54 (2.36) 2.01 (2.45)
   Range 0.00 - 8.00 0.00 - 5.00 0.00 - 8.50 0.00 - 9.00 0.00 - 9.00 0.00 - 9.00
disability_outcome 0.11
   N-Miss 1 0 6 4 2 13
   Mean (SD) 18.30 (18.67) 13.14 (8.71) 11.58 (12.21) 16.74 (14.36) 13.98 (13.11) 15.16 (14.58)
   Range 0.00 - 70.00 2.00 - 26.00 0.00 - 70.00 0.00 - 54.00 0.00 - 60.00 0.00 - 70.00
func_outcome 0.50
   N-Miss 2 0 11 17 12 42
   Mean (SD) 7.00 (8.14) 5.57 (4.47) 5.71 (7.63) 7.99 (9.45) 5.88 (7.48) 6.70 (8.22)
   Range 0.00 - 28.00 0.00 - 14.00 0.00 - 38.00 0.00 - 36.00 0.00 - 34.00 0.00 - 38.00

Correlation plots

col<- colorRampPalette(c("red", "white", "blue"))(20)
df %>%  
  select_if (is.numeric) %>%    
  select (-id) %>%
  cor (use = "complete.obs") %>%
  corrplot(type="upper", tl.col="black", tl.srt=45, col=col)

BN analysis

Create blacklist

df.bn = as.data.frame (df)
df.bn[, grepl ( "id|subgrp|outcome|func",names (df.bn))] <- NULL

tiers_bl = list (colnames (df.bn)[colnames(df.bn) %in% grep ("early",colnames(df.bn), value = TRUE)], 
                 colnames (df.bn)[colnames(df.bn) %in% grep ("late",colnames(df.bn), value = TRUE)])
bl1 = tiers2blacklist(tiers_bl)

tiers_bl = list (colnames (df.bn)[colnames(df.bn) %in% grep ("grp",colnames(df.bn), value = TRUE)], 
                 colnames (df.bn)[colnames(df.bn) %in% grep ("late",colnames(df.bn), value = TRUE)])

bl2 = tiers2blacklist(tiers_bl)

tiers_bl = list (colnames (df.bn)[colnames(df.bn) %in% grep ("grp",colnames(df.bn), value = TRUE)], 
                 colnames (df.bn)[colnames(df.bn) %in% grep ("early",colnames(df.bn), value = TRUE)])

bl3 = tiers2blacklist(tiers_bl)
bl = rbind(bl1, bl2, bl3)

Build BN model

doParallel::registerDoParallel(4)
n_boot <- 200

set.seed (20200420)

############

boot_bl_rel <-  foreach (B = 1: n_boot) %dopar%{
    boot.sample = df.bn[sample(nrow(df.bn), 
                                          nrow(df.bn), replace = TRUE), ]
    bnlearn::structural.em(boot.sample, impute = "bayes-lw", max.iter = 3,
                              maximize.args = list(blacklist = bl))
}
#############

Performance evaluation using nested cross validation.

Inner is bootstrap resampling for model averaging. Outer is bootstrap resampling k = 25 for performance evaluation.

set.seed (20200420)

flds <- createFolds(1:nrow(df.bn), 
                            k = 10, returnTrain = TRUE)
n_boot = 200
corr.df.list <- list()

for (k in seq_along(flds)) {
  
  train <-  df.bn [flds[[k]], ] %>% as.data.frame()
  test <- df.bn [-flds[[k]], ] %>% as.data.frame()
  
  doParallel::registerDoParallel(4)
  ############
  
  boot  =  foreach (B = 1: n_boot) %dopar%{
      boot.sample = train[sample(nrow(train), 
                                            nrow(train), replace = TRUE), ]
      bnlearn::structural.em(boot.sample, impute = "bayes-lw", max.iter = 3,
                                maximize.args = list(blacklist = bl))
  }
  #############
  stopImplicitCluster()
  
  bootstr <-  custom.strength(boot, nodes = names(train))
  avg <-  averaged.network(bootstr, threshold = 0.5)
  fit <-  bn.fit (avg, train, method = "mle")
  
  imp.list = impute (fit, data = test, method = "bayes-lw")
  inames = names (imp.list) [!grepl ("grp", names (imp.list))]
  corr.df =  structure(numeric(length (inames)), names = inames)
  
  for (var in inames) {
      corr.df[var] = cor(predict(fit, data = imp.list, var, method = "bayes-lw"), 
                         imp.list[, var])
    }
  
  corr.df.list[[k]] <- corr.df
  

}

corr.df <- bind_cols (corr.df.list) %>%
  apply (1, mean)

names (corr.df) <- inames

corr.df

See results

bootstr = custom.strength(boot_bl_rel, nodes = names(df.bn))
avg = averaged.network(bootstr, threshold = 0.5)
fit = bn.fit (avg, df.bn, method = "mle")

g = strength.plot(avg, 
                  bootstr, 
                  shape = "rectangle",
                  main = "Figure")

graph::nodeRenderInfo(g) = list(fontsize=18)
renderGraph(g)

Save results

save (df,
      df.bn,
      bl,
      boot_bl,
      boot_bl_rel,
      bootstr,
      corr.df,
      avg,
      fit,
      file = "output/results_early2late.RData")

sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] grid      parallel  stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] doParallel_1.0.15   iterators_1.0.12    foreach_1.5.0      
 [4] caret_6.0-86        lattice_0.20-41     bnlearn_4.5        
 [7] VIM_5.1.1           data.table_1.13.0   colorspace_1.4-1   
[10] mice_3.11.0         xlsx_0.6.3          readxl_1.3.1       
[13] corrplot_0.84       corrr_0.4.2         Rgraphviz_2.30.0   
[16] graph_1.64.0        BiocGenerics_0.32.0 magrittr_1.5       
[19] janitor_2.0.1       arsenal_3.5.0       tidyselect_1.1.0   
[22] forcats_0.5.0       stringr_1.4.0       dplyr_1.0.1        
[25] purrr_0.3.4         readr_1.3.1         tidyr_1.1.1        
[28] tibble_3.0.3        ggplot2_3.3.2       tidyverse_1.3.0    
[31] workflowr_1.6.2    

loaded via a namespace (and not attached):
 [1] ellipsis_0.3.1       class_7.3-17         rio_0.5.16          
 [4] rprojroot_1.3-2      snakecase_0.11.0     fs_1.5.0            
 [7] rstudioapi_0.11      prodlim_2019.11.13   fansi_0.4.1         
[10] lubridate_1.7.9      ranger_0.12.1        xml2_1.3.2          
[13] splines_3.6.2        codetools_0.2-16     robustbase_0.93-6   
[16] knitr_1.29           jsonlite_1.7.0       pROC_1.16.2         
[19] rJava_0.9-12         broom_0.7.0          dbplyr_1.4.4        
[22] compiler_3.6.2       httr_1.4.2           backports_1.1.7     
[25] assertthat_0.2.1     Matrix_1.2-18        cli_2.0.2           
[28] later_1.1.0.1        htmltools_0.5.0      tools_3.6.2         
[31] gtable_0.3.0         glue_1.4.1           reshape2_1.4.4      
[34] Rcpp_1.0.5           carData_3.0-4        cellranger_1.1.0    
[37] vctrs_0.3.2          nlme_3.1-142         lmtest_0.9-37       
[40] timeDate_3043.102    gower_0.2.2          xfun_0.16           
[43] laeken_0.5.1         xlsxjars_0.6.1       openxlsx_4.1.5      
[46] rvest_0.3.6          lifecycle_0.2.0      DEoptimR_1.0-8      
[49] MASS_7.3-51.6        zoo_1.8-8            scales_1.1.1        
[52] ipred_0.9-9          hms_0.5.3            promises_1.1.1      
[55] yaml_2.2.1           curl_4.3             rpart_4.1-15        
[58] stringi_1.4.6        highr_0.8            e1071_1.7-3         
[61] boot_1.3-25          zip_2.1.0            lava_1.6.7          
[64] rlang_0.4.7          pkgconfig_2.0.3      evaluate_0.14       
[67] recipes_0.1.13       plyr_1.8.6           R6_2.4.1            
[70] generics_0.0.2       DBI_1.1.0            pillar_1.4.6        
[73] haven_2.3.1          whisker_0.4          foreign_0.8-72      
[76] withr_2.2.0          survival_3.2-3       abind_1.4-5         
[79] sp_1.4-2             nnet_7.3-14          modelr_0.1.8        
[82] crayon_1.3.4         car_3.0-9            rmarkdown_2.3       
[85] blob_1.2.1           git2r_0.27.1         ModelMetrics_1.2.2.2
[88] vcd_1.4-7            reprex_0.3.0         digest_0.6.25       
[91] httpuv_1.5.4         stats4_3.6.2         munsell_0.5.0