#Article Title: Personalized Ecological Momentary Assessment and Networks to Improve Client-Practitioner Communication in Motivational Interviewing for Cannabis Use Problems: A Case Study
#Journal: Cognitive Therapy and Research


# Import dataset
data <- read.csv("/Volumes/NO NAME/EMAresponses9May2022_FINAL/data10MAYFINAL.csv", sep = ";", check.names=FALSE)

# load libraries
library(dplyr)
library(qgraph)
library(graphicalVAR)

# Rename variables used in network analysis
data <- data %>% rename (
    "Anxious" = "[1_VAS] In the past three hours how nervous anxious or on edge did you feel?",
    "Depressed" = "[2_VAS] In the past three hours how down depressed or hopeless did you feel?",
    "Happy" = "[3_VAS] In the past three hours how happy did you feel?",
    "Relaxed" = "[4_VAS] In the past three hours how relaxed did you feel?",
    "Cannabis_Craving" = "[5_VAS] In the past three hours how much urge to consume cannabis did you feel?",
    "Angry" = "[40_VAS] In the past three hours how angry irritable or aggressive did you feel?",
    "Cannabis_Grams" = "[21_FFT] In the past three hours how many grams of cannabis did you consume? Please enter the amount in grams with the help of the picture below.",
    "Amount_Cigarettes" = "[23_FFT] In the past three hours how many cigarettes or drums did you consume? Please enter the number.",
    "Activity_Work_Or_Study"= "[31_SAQ] In the past three hours how much time did you spend working or studying?",
    "Activity_Sleep" = "[32_SAQ] In the past three hours how much time did you spend sleeping?",
    "Procrastinated" = "[42_VAS] In the past three hours to what extent did you procrastinate or delay doing things?",
    "Feeling_Guilty" = "[32_VAS] In the past three hours how guilty did you feel?",
    "Stressed" = "[43_VAS] In the past three hours how stressed did you feel?",
    "Rumination_Regrets_About_Past" = "[33_VAS] In the past three hours how much did you think about a recent situation wishing it had gone better?",
    "Difficulty_Concentrating" = "[35_VAS] In the past three hours how much difficulty concentrating did you experience?",
    "Sluggish" = "[36_VAS] In the past three hours how sluggish did you feel?",
    "Feeling_Good_About_Yourself" = "[44_VAS] In the past three hours how good about yourself did you feel?",
    "Activity_Making_Music" = "[37_SAQ] In the past three hours how much time did you spend making music?")

# Select variables used in network analysis
data <- data %>% select("Anxious", "Depressed", "Relaxed","Happy", "Cannabis_Craving", "Angry", "Cannabis_Grams",
                "Amount_Cigarettes","Activity_Work_Or_Study", "Activity_Sleep", "Procrastinated","Feeling_Guilty", "Stressed","Rumination_Regrets_About_Past",
                "Difficulty_Concentrating","Sluggish", "Feeling_Good_About_Yourself","Activity_Making_Music", "beep", "day")


########## Re-run outside of the feedback application the network analyses that were ran within the feedback application during the study ##########
########## This same code was used for the network analysis in the feedback application. However, due to a bug in the code handling the data ##########
########## within the application before the network analyses were ran, the dataset used to ran the following code was erroneous. ##########

# Detrend variables used in network analysis
  Vars <- c("Anxious", "Depressed", "Relaxed","Happy", "Cannabis_Craving", "Angry", "Cannabis_Grams",
             "Amount_Cigarettes","Activity_Work_Or_Study", "Activity_Sleep", "Procrastinated","Feeling_Guilty", "Stressed","Rumination_Regrets_About_Past",
             "Difficulty_Concentrating","Sluggish", "Feeling_Good_About_Yourself","Activity_Making_Music")
  
  for (v in seq_along(Vars)){
    ff <- as.formula(paste0(Vars[[v]]," ~ day + beep"))
    fit <- lm(ff, data = data)
    if (anova(fit)$P[1] < 0.05 | anova(fit)$P[2] < 0.05 ){
      message(paste("Detrending variable",v))
      data[[Vars[v]]][!is.na(data[[Vars[v]]])] <- residuals(fit) }
  }

# Run the network analyses, plot and save the results for each network used in the motivational interviewing session in the study

  # run the network analysis and plot network Figure S1 Supplementary Materials
  Res <- graphicalVAR(data, gamma = 0, vars = c("Anxious", "Depressed", "Happy", "Cannabis_Craving", "Cannabis_Grams"), dayvar = "day", beepvar = "beep")
  pdf(file = "Network1_2992023.pdf",   # The directory you want to save the file in
      width = 6, # The width of the plot in inches
      height = 4) # The height of the plot in inches
  qgraph(Res$PCC, theme = "colorblind", layout = "spring", legend = TRUE, legend.cex = 0.3,
         labels = TRUE, vsize = 4, GLratio = 1.5, nodeNames = c("Anxious", "Depressed", "Happy", "Cannabis Craving", "Cannabis Grams"))
  dev.off()
  
  # run the network analysis and plot network Figure S2 Supplementary Materials
  Res <- graphicalVAR(data, gamma = 0, vars = c("Feeling_Guilty", "Cannabis_Grams", "Difficulty_Concentrating", "Rumination_Regrets_About_Past", "Cannabis_Craving"), dayvar = "day", beepvar = "beep")
  pdf(file = "Network2_2992023.pdf",   # The directory you want to save the file in
      width = 6, # The width of the plot in inches
      height = 4) # The height of the plot in inches
  qgraph(Res$PCC, theme = "colorblind", layout = "spring", legend = TRUE, legend.cex = 0.3,
         labels = TRUE, vsize = 4, GLratio = 1.5, nodeNames = c("Feeling Guilty", "Cannabis Grams", "Difficulty Concentrating", "Rumination Regrets About the Past", "Cannabis Craving"))
  dev.off()

  # run the network analysis and plot network Figures S3 and S5 Supplementary Materials
  Res <- graphicalVAR(data, gamma = 0, vars = c("Cannabis_Grams", "Activity_Sleep", "Activity_Making_Music", "Activity_Work_Or_Study"), dayvar = "day", beepvar = "beep")
  pdf(file = "Network3_2992023.pdf",   # The directory you want to save the file in
      width = 6, # The width of the plot in inches
      height = 4) # The height of the plot in inches
  qgraph(Res$PCC, theme = "colorblind", layout = "spring", legend = TRUE, legend.cex = 0.3,
         labels = TRUE, vsize = 4, GLratio = 1.5, nodeNames = c("Cannabis Grams", "Activity Sleep", "Activity Making Music", "Activity Work or Study"))
  dev.off()
  pdf(file = "Network4_2992023.pdf",   # The directory you want to save the file in
      width = 6, # The width of the plot in inches
      height = 4) # The height of the plot in inches
  qgraph(Res$PDC, theme = "colorblind", layout = "spring", legend = TRUE, legend.cex = 0.3,
         labels = TRUE, vsize = 4, GLratio = 1.5, nodeNames = c("Cannabis Grams", "Activity Sleep", "Activity Making Music", "Activity Work or Study"))
  dev.off()
  
  # run the network analysis and plot network Figure S6 Supplementary Materials
  Res <- graphicalVAR(data, gamma = 0, vars = c("Cannabis_Grams", "Activity_Sleep", "Sluggish"), dayvar = "day", beepvar = "beep")
  pdf(file = "Network5_2992023.pdf",   # The directory you want to save the file in
      width = 6, # The width of the plot in inches
      height = 4) # The height of the plot in inches
  qgraph(Res$PDC, theme = "colorblind", layout = "spring", legend = TRUE, legend.cex = 0.3,
         labels = TRUE, vsize = 4, GLratio = 1.5, nodeNames = c("Cannabis Grams", "Activity Sleep", "Sluggish"))
  dev.off()
  
  # run the network analysis and plot network Figure S4 Supplementary Materials
  Res <- graphicalVAR(data, gamma = 0, vars = c("Amount_Cigarettes", "Anxious", "Angry", "Happy", "Depressed", "Cannabis_Grams"), dayvar = "day", beepvar = "beep")
  pdf(file = "Network6_2992023.pdf",   # The directory you want to save the file in
      width = 6, # The width of the plot in inches
      height = 4) # The height of the plot in inches
  qgraph(Res$PCC, theme = "colorblind", layout = "spring", legend = TRUE, legend.cex = 0.3,
         labels = TRUE, vsize = 4, GLratio = 1.5, nodeNames = c("Amount Cigarettes", "Anxious", "Angry", "Happy", "Depressed", "Cannabis Grams"))
  dev.off()
  
  
  
  