# Load ncessary packages ----------------------------------------------- install.packages("readbulk") install.packages("foreign") install.packages("tidyr") install.packages("dplyr") install.packages("ez") install.packages("apaTables") install.packages ("tidyverse") install.packages("readxl") install.packages("gsubfn") install.packages('data.table') library(rstudioapi) # used for some file working directory stuff at the begining of every script you use library(readbulk) # used for import and combining many data files into one big data file #library(ggplot2) # used for creating plots and graphs library(foreign) # used for importing non-typical data into R, including SPSS data files library(tidyr) # used for wrangling and tidying data into the formats and shapes you want it library(dplyr) # same as tidyr #library(ez) # used for running ANOVA type analyses easily #library(apaTables) # used for creating APA style tables from your data analayses library(tidyverse) library(readxl) #library(car) ##additional packages for this script library(psych) library(gsubfn) # Set working directory to source script location ------------------------- dir.name <- dirname(rstudioapi::getActiveDocumentContext()$path) setwd(dir.name) getwd() # Import data ------------------------------------------------------------- # Know that you can always read about how to use any package or function in the # R-Studio 'Help' tab. Simply enter the name of the pakage or funciton in the search bar. # readbulk will append multiple data files into a single data frame to be used in R. # When we use loaded package in a script, it is good practice to prepend the use of the function with the name of the package # Below we will load a whole study's worth of participant attentional bias data. data <- readbulk::read_bulk(extension = "MalleabilityUSYD2_raw.dat", fun = utils::read.delim, verbose = TRUE) # Generate summary stats of data ------------------------------------------ # Once we have our large data frame we may wish to get some summary stats to make sure we imported things correctly # First we may simply wish to see a summary of the data we have str(data) # We can also ensure we have the right number of participants imported by simply counting them count(distinct(data,subject)) # Next we may wish to ensure that the participants we imported have all their data, or determine whether any participants are missing trial data # To do this, we will use as series of dplyr functions all in one line of code. We dont have to do this, but it makes things quicker to type and easier to read :) # We can do this using the %>% symbols, which tells R to follow one command on with another. # See if you can tell what each line of code here is doing trial.summary <- data %>% dplyr::group_by(subject, blockcode) %>% dplyr::summarise(num_trials = n()) trial.summary #-------------------------------------------------------------------------------------- #Questionnaire data #-------------------------------------------------------------------------------------- #taking blocks 5-17 from blocknum column from whole data #. = whatever the data is at that point #all in one line!!!!! # WHEN USING %>%, need to specify before the first one where the data will be coming from. If this happens, then take out the data from the brakets following 'filter' #this is done without demographic data, data columns to select will vary otherwise Q.data <- data_raw_new1 %>% filter(blocknum %in% c(5:11) & subject != "45") %>% .[c(4, 6:18)] write.csv(Q.data, file = "C:/Users/justine.macgoris/OneDrive/Bureau/Qdataraw2_final.csv") trial.summary <- Q.data %>% dplyr::group_by(subject, blockcode) %>% dplyr::summarise(num_trials = n()) trial.summary Q.data <- Qdataraw2_final #--------------------------------------------------------- #DASS #--------------------------------------------------------- #############note for prolific, DASS scores were 1-4; recode to 0-3. Changed in SONA script to be 0-3 Q.DASS<- filter(Q.data, blockcode == "DASS") Q.DASS <- rename(Q.DASS, "question" = "trialnum") Q.DASS$response <- as.numeric(as.character(Q.DASS$response)) str(Q.DASS) #check there are 21 items in the DASS, range 0-3 trial.summary <- Q.DASS %>% dplyr::group_by(subject) %>% dplyr::summarise(items = n(), min = min(response), max = max(response)) trial.summary #recode to 0-3 range (if needed) Q.DASS$response<-Q.DASS$response -1 #re-check there are 21 items in the DASS, range 0-3 trial.summary <- Q.DASS %>% dplyr::group_by(subject) %>% dplyr::summarise(items = n(), min = min(response), max = max(response)) trial.summary #### SEPARATE OUT DEPRESSION, ANXIETY AND STRESS### install.packages("ltm") library(ltm) DASS.D <- filter(Q.DASS, question == 3 | question == 5 | question == 10 | question == 13 | question == 16 | question == 17 | question == 21 ) DASS.D.sum <- aggregate(DASS.D$response, by=list(subject=DASS.D$subject), FUN=sum) DASS.D.sum$x <- as.numeric(DASS.D.sum$x) DASS.D.sum$DASS.D <- DASS.D.sum$x*2 DASS.D.sum$x <- NULL DASS.A <- filter(Q.DASS, question == 2 | question == 4 | question == 7 | question == 9 | question == 15 | question == 19 | question == 20 ) DASS.A.sum <- aggregate (DASS.A$response, by=list(subject=DASS.A$subject), FUN=sum) DASS.A.sum$x <- as.numeric(DASS.A.sum$x) DASS.A.sum$DASS.A <- DASS.A.sum$x*2 DASS.A.sum$x <- NULL DASS.S <- filter(Q.DASS, question == 1 | question == 6 | question == 8 | question == 11 | question == 12 | question == 14 | question == 18 ) DASS.S.sum <- aggregate (DASS.S$response, by=list(subject=DASS.S$subject), FUN=sum) DASS.S.sum$x <- as.numeric(DASS.S.sum$x) DASS.S.sum$DASS.S <- DASS.S.sum$x*2 DASS.S.sum$x <- NULL DASS.final <- full_join(DASS.D.sum, DASS.A.sum, by = "subject") DASS.final <- full_join(DASS.final, DASS.S.sum, by = "subject") #-------------------------------------------- #ACS attentional control scale #-------------------------------------------- install.packages("lmSupport") library (lmSupport) Q.ACS<- filter(Q.data, blockcode == "ACS") Q.ACS_r <- Q.ACS[c(2,11, 14)] Q.ACS_r$response <- as.numeric(Q.ACS_r$response) Q.ACS_r<- spread(Q.ACS_r, stimulusnumber1,response) Q.ACS_r$ATTC_FOC = varScore (Q.ACS_r, Forward= c('4', '5', '9'), Reverse= c('1', '2', '3', '6','7', '8'), Range = c(1,4) ) Q.ACS_r$ATTC_SHI = varScore(Q.ACS_r, Forward= c('10', '13', '14', '17', '18', '19'), Reverse= c('11', '12', '15', '16', '20'), Range = c(1,4) ) Q.ACS_r$ATTC_TOT = varScore(Q.ACS_r, Forward= c('4', '5', '9', '10', '13', '14', '17', '18', '19'), Reverse = c('1', '2', '3', '6','7', '8', '11', '12', '15', '16', '20'), Range = c(1,4) ) Q.ACS_f<- Q.ACS_r [c(1,22,23,24)] #-------------------------------------------- #CFI cognitive flexibility inventory #-------------------------------------------- Q.CFI<- filter(Q.data, blockcode == "CFI") Q.CFI_r <- Q.CFI[c(2,11, 14)] Q.CFI_r<- spread(Q.CFI_r, stimulusnumber1,response) Q.CFI_r$CFI_ALT = varScore (Q.CFI_r, Forward= c('1', '3', '5', '6', '8', '10', '12','13', '14', '16', '18', '19', '20'), Range = c(1,7) ) Q.CFI_r$CFI_TOT = varScore (Q.CFI_r, Forward= c('1', '3', '5', '6', '8', '10', '12','13', '14', '15','16', '18', '19', '20'),Reverse = c('2','4','7','9','11','17'), Range = c(1,7) ) Q.CFI_r$CFI_CTRL = varScore (Q.CFI_r, Forward = c('15'), Reverse = c('2', '4', '7', '9', '11', '17'), Range = c(1,7)) Q.CFI_f <- Q.CFI_r [c(1, 22, 23, 24)] #-------------------------------------------- #MAAS mindfulness #-------------------------------------------- Q.MAAS <- filter (Q.data, blockcode == "MAAS") Q.MAAS_r <- Q.MAAS[c(1, 10, 14)] Q.MAAS_r$response <- as.numeric(Q.MAAS_r$response) Q.MAAS_rmean <-Q.MAAS_r %>% group_by(subject)%>% summarise_at(vars(response), list(name=mean)) #-------------------------------------------- #Pain Catastrophising Scale (PCS) #------------------------------------------ Q.PCS<- filter(Q.data, blockcode == "PCS") trial.summary <- Q.PCS %>% dplyr::group_by(subject) %>% dplyr::summarise(items = n(), min = min(response), max = max(response)) trial.summary write.csv(Q.data, file = "C:/Users/justine.macgoris/OneDrive/Bureau/Qdataraw2.csv") data <- readbulk::read_bulk(extension = "Qdataraw2.csv", fun = utils::read.delim, verbose = TRUE) data <- Qdataraw2 Q.PCS_r <- Q.PCS[c(1,10, 14)] Q.PCS_r$response <- as.numeric(Q.PCS_r$response) str(Q.PCS_r) Q.PCS_r <- spread(Q.PCS_r, key = stimulusitem1, value = response) Q.PCS_r$PCSrumination <- NA Q.PCS_r$PCSrumination <- Q.PCS_r$`I anxiously want the pain to go away` + Q.PCS_r$`I can't seem to keep it out of my mind` + Q.PCS_r$`I keep thinking about how much it hurts` + Q.PCS_r$`I keep thinking about how badly I want the pain to stop` Q.PCS_r$PCSmagnification <- Q.PCS_r$`I become afraid that the pain will get worse` + Q.PCS_r$`I keep thinking of other painful events` + Q.PCS_r$`I wonder whether something serious may happen` Q.PCS_r$PCShelpless <- Q.PCS_r$`I worry all the time about whether the pain will end` + Q.PCS_r$`I feel I can't go on` + Q.PCS_r$`It's terrible and I think it's never going to get any better` + Q.PCS_r$`It's awful and I feel that it overwhelms me` + Q.PCS_r$`I feel I can't stand it anymore`+ Q.PCS_r$`There's nothing I can do to reduce the intensity of the pain` Q.PCS_r$PCS <- Q.PCS_r$PCSrumination + Q.PCS_r$PCSmagnification + Q.PCS_r$PCShelpless PCS.sum <- Q.PCS_r[c(1, 2, 16:18)] #-------------------------------------------- #PROMIS-29 #-------------------------------------------- Q.PROMIS<- filter(Q.data, blockcode == "PROMIS") Q.PROMIS$response <- as.character(Q.PROMIS$response) Q.PROMIS$response <- as.numeric(Q.PROMIS$response) trial.summary <- Q.PROMIS %>% dplyr::group_by(subject) %>% dplyr::summarise(items = n(), min = min(response), max = max(response)) trial.summary Q.P <- Q.PROMIS[c(1, 10, 14)] Q.P$response <- as.numeric(Q.P$response) str(Q.P) Q.P<- spread(Q.P, key = stimulusitem1, value =response) #physical function (more = better) Q.P$P.PF <- Q.P$`Are you able to do chores such as vacuuming or yard work?` + Q.P$`Are you able to go up and down stairs at a normal pace?` + Q.P$`Are you able to go for a walk of at least 15 minutes?` + Q.P$`Are you able to run errands and shop?` #anxiety (more = worse) Q.P$P.ANX <- Q.P$`I felt fearful` + Q.P$`I found it hard to focus on anything other than my anxiety` + Q.P$`My worries overwhelmed me`+ Q.P$`I felt uneasy` #depression (more = worse) Q.P$P.DEP <- Q.P$`I felt worthless` + Q.P$`I felt helpless` + Q.P$`I felt depressed`+ Q.P$`I felt hopeless` #fatigue (more = worse) Q.P$P.FAT <- Q.P$`I felt fatigued` + Q.P$`I have trouble STARTING things because I am tired` + Q.P$`How run-down did you feel on average?` + Q.P$`How fatigued were you on average?` #sleep disturbance (more = worse) Q.P$P.SLEEP <- Q.P$`My sleep quality was..` + Q.P$`My sleep quality was..` + Q.P$`I had a problem with my sleep` + Q.P$`I had difficulty falling asleep` #participation in social roles and activities (more = better) Q.P$P.SOC <- Q.P$`I have trouble doing all of my regular leisure activities with others` + Q.P$`I have trouble doing all of the family activities that I want to do` + Q.P$`I have trouble doing all of my usual work (including work at home)` + Q.P$`I have trouble doing all of the activities with friends that I want to do` #pain interference (more = worse) Q.P$P.PAIN.inter <- Q.P$`How much did pain interfere with your day to day activities?` + Q.P$`How much did pain interfere with work around the home?` + Q.P$`How much did pain interfere with your ability to participate in social activities?`+ Q.P$`How much did pain interfere with your household chores?` #pain intensity (more = worse) Q.P$P.PAIN.level <- Q.P$`How would you rate your pain on average?` PROMIS.final <- Q.P[c(1, 31:38)] #-------------------------------------------- #GCPS graded chronic pain scale #-------------------------------------------- Q.GCPS<- filter(Q.data, blockcode == "GCPS") Q.GCPS$response <- as.character(Q.GCPS$response) Q.GCPS$response <- as.numeric(Q.GCPS$response) trial.summary <- Q.GCPS %>% dplyr::group_by(subject, trialcode) %>% dplyr::summarise(items = n(), min = min(response), max = max(response)) trial.summary #calculate pain intensity and disability scores, out of 100 GCPS.intensity <- filter(Q.GCPS, trialcode == "GCPS_Likert1" ) GCPS.intensity.sum <- aggregate(GCPS.intensity$response, by=list(subject=GCPS.intensity$subject), FUN=sum) GCPS.intensity.sum$x <- as.numeric(GCPS.intensity.sum$x) GCPS.intensity.sum$GCPS.intensity <- (GCPS.intensity.sum$x/3)*10 GCPS.intensity.sum$x <- NULL GCPS.disability <- filter(Q.GCPS, trialcode == "GCPS_Likert2" | trialcode == "GCPS_Likert3") GCPS.disability.sum <- aggregate(GCPS.disability$response, by=list(subject=GCPS.disability$subject), FUN=sum) GCPS.disability.sum$x <- as.numeric(GCPS.disability.sum$x) GCPS.disability.sum$GCPS.disability <- (GCPS.disability.sum$x/3)*10 GCPS.disability.sum$x <- NULL GCPS.disability.sum$DSpts <- ifelse((GCPS.disability.sum$GCPS.disability <=29), 0, ifelse((GCPS.disability.sum$GCPS.disability >=30 & GCPS.disability.sum$GCPS.disability <=49), 1, ifelse((GCPS.disability.sum$GCPS.disability >=50 & GCPS.disability.sum$GCPS.disability <=69), 2, ifelse((GCPS.disability.sum$GCPS.disability >70), 3, "error")))) GCPS.disability.days <- Q.GCPS %>% filter(trialcode == "G6") %>% .[c(1,10)] GCPS.disability.days$DDpts <- ifelse((GCPS.disability.days$response <=6), 0, ifelse((GCPS.disability.days$response >=7 & GCPS.disability.days$response <=14), 1, ifelse((GCPS.disability.days$response >=15 & GCPS.disability.days$response <=30), 2, ifelse((GCPS.disability.days$response >30), 3, "error")))) GCPS <- full_join(GCPS.disability.sum, GCPS.disability.days, by="subject") GCPS <- full_join(GCPS, GCPS.intensity.sum, by="subject") GCPS$DSpts <- as.numeric(GCPS$DSpts) GCPS$DDpts <- as.numeric(GCPS$DDpts) GCPS$GCPS.DP <- GCPS$DSpts + GCPS$DDpts GCPS$CPG <- ifelse((GCPS$GCPS.intensity == 0), "Grade0", ifelse((GCPS$GCPS.intensity < 50 & GCPS$GCPS.disability <3), "Grade1", ifelse((GCPS$GCPS.intensity >=50 & GCPS$GCPS.disability <3), "Grade2", ifelse((GCPS$GCPS.disability >=3 & GCPS$GCPS.disability <=4), "Grade3", ifelse((GCPS$GCPS.disability >=5), "Grade4","error"))))) GCPS <- GCPS %>% rename(GCPS.pain = response) GCPS.final <- GCPS[c(1,2,4,6,7)] #-------------------------------------------- #demographics #-------------------------------------------- data.demo <- readbulk::read_bulk(extension = "startquestions_survey.dat", fun = utils::read.delim, verbose = TRUE) write.csv(data.demo, file = "C:/Users/justine.macgoris/OneDrive/Bureau/QsurveyQ.csv") data.demo <- QsurveyQ_23 data.demo <- data.demo[c(4, 6, 8, 10,12, 14, 18)] data.demo <- data.demo %>% rename (sex = gender_response, age = age_response, nationality = Nationality_response, language = language_response, cpain = pain_response, paincurrent = pain2_response) #-------------------------------------------- #Bringing it all together #------------------------------------------ #merge questionnaire data together Q.final <- full_join( data.demo, GCPS.final, by="subject") Q.final <- full_join(Q.final, PROMIS.final, by="subject") Q.final <- full_join(Q.final,DASS.final, by="subject") Q.final <- full_join(Q.final, Q.MAAS_rmean, by = "subject") Q.final <- full_join(Q.final, Q.ACS_f, by = "subject") Q.final <- full_join(Q.final, Q.CFI_f, by ="subject") write.csv(ABdata, file = "C:/Users/justine.macgoris/OneDrive/Bureau/Lastversion_AM23.csv") ABdata <- full_join(ABdata_LASTVERSION23, Qdatafinal_last23, by = c("subject")) ABdata <- full_join(ABdata, RIR_FINAL23, by = c("subject")) ABdata <- full_join(ABdata, trainingorder, by = c("subject"))