library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.7 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(readxl)
library(itrak)
df <- readxl::read_excel(path = "/Users/noahwolkowicz/Desktop/CT/West Haven/Postdoc/Postdoc Research/Ross Attentional Bias Study/Data/OPAB_SS_clean_v2.0.xlsx") %>%
mutate(Session = factor(Session), #change data types from character strings to factors
condition.f = factor(condition, levels = c(0,1), labels = c("delayed methadone", "scheduled methadone")),
cue = factor(cue),
Probe = factor(probe),
Probe.tf = ifelse(Probe == "congruent", TRUE, FALSE))
glimpse(df) #view data frame
## Rows: 19,163
## Columns: 11
## $ Session <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ Subject <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ condition <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ Trial <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,…
## $ accuracy <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ RT <dbl> 949, 599, 617, 665, 576, 489, 551, 534, 643, 576, 514, 544…
## $ cue <fct> opioid, opioid, pain, pain, opioid, pain, pain, pain, pain…
## $ probe <chr> "incongruent", "incongruent", "congruent", "congruent", "i…
## $ condition.f <fct> scheduled methadone, scheduled methadone, scheduled methad…
## $ Probe <fct> incongruent, incongruent, congruent, congruent, incongruen…
## $ Probe.tf <lgl> FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, …
#Verifying the absence of missing data
naniar::vis_miss(df)
## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## Please use `gather()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
df %>%
group_by(Subject, Session, cue) %>%
summarise(Percent_Accurate = mean(accuracy)) %>%
filter(Percent_Accurate < 0.50)
## `summarise()` has grouped output by 'Subject', 'Session'. You can override
## using the `.groups` argument.
#The code here then removes subject 17 for session 4
df$Sub17Ses4 <- ifelse(df$Subject == 17 & df$Session == 4, 1, 0)
df <- df %>% filter(Sub17Ses4 == 0)
df %>%
group_by(Session) %>%
summarise(Overall_Trials = n(), #Overall trial counts
Count_Inc_Trial = sum(ifelse(accuracy == 0,1,0)), #Incorrect trial count
Per_Inc_Trial = 1 - mean(accuracy), #Percent of trials that are incorrect
Count_Inc_RT = sum(ifelse(RT < 200 | RT > 1500, 1, 0)), #Count of trials whose reaction times are < 200 or > 1500
Per_Inc_RT = 1 - mean(ifelse(RT < 200 | RT > 1500, 0, 1)))
df <- df %>%
filter(accuracy == 1) %>% #Exclude incorrect trials
filter(RT > 200 & RT < 1500) #Exclude RT's <200ms or >1500ms
max(df$RT)
## [1] 1499
min(df$RT)
## [1] 207
MAD_SD_df <- df %>%
group_by(Subject) %>%
summarise(med = median(RT), #1) Gets median RT for each subject,
MAD = mad(RT), #2) gets MAD for each subject,
MAD3 = 3*MAD, #3) calculates 3*MAD for each subject,
MAD_Exclude_Score = med + MAD3, #4) adds the 3*MAD to each subject's median (i.e., the RT score at which a trial would be excluded);
Mean = mean(RT), #5) it calculates mean reaction time,
SD = sd(RT), #6) gets standard deviation of reaction times
SD3 = 3*SD, #7) gets 3x the standard deviation of reaction times
SD_Exclude_Score = SD3 + Mean) #8) adds the 3*SD to each subject's mean reaction time (i.e., the RT score at which a trial would be excluded);
df <- left_join(df, MAD_SD_df, by = "Subject") #Binds the variables calculated above to the data
df %>%
group_by(Session) %>%
summarise(Ovr_Trl_Aftr_Exc = n(), #Overall trial counts after excluding for above-noted criteria
Count_MAD3_Over = sum(ifelse(RT > MAD_Exclude_Score, 1,0)), #Count of trials that had RT > 3MAD above individual median
Per_MAD3_Over = mean(ifelse(RT > MAD_Exclude_Score, 1, 0)),
Count_SD3_Over = sum(ifelse(RT > SD_Exclude_Score, 1,0)), #Count of trials that had RT > 3MAD above individual median
Per_SD3_Over = mean(ifelse(RT > SD_Exclude_Score, 1, 0)))
df <- df %>%
filter(RT < SD_Exclude_Score) #Exclude folks whose RT are >3SD above their individual means
AB_df <- df %>%
arrange(Subject, Trial) %>%
group_by(Subject, Session, cue) %>%
itrak::summarize_bias(data = ., RT = RT, congruent = Probe.tf, method = "nearest")
df <- left_join(df, AB_df, by = c("Session", "cue", "Subject"))
glimpse(df)
## Rows: 17,070
## Columns: 30
## $ Session <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ Subject <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ condition <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ Trial <dbl> 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, …
## $ accuracy <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ RT <dbl> 599, 617, 665, 576, 489, 551, 534, 643, 576, 514, 54…
## $ cue <fct> opioid, pain, pain, opioid, pain, pain, pain, pain, …
## $ probe <chr> "incongruent", "congruent", "congruent", "incongruen…
## $ condition.f <fct> scheduled methadone, scheduled methadone, scheduled …
## $ Probe <fct> incongruent, congruent, congruent, incongruent, cong…
## $ Probe.tf <lgl> FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, F…
## $ Sub17Ses4 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ med <dbl> 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 51…
## $ MAD <dbl> 75.6126, 75.6126, 75.6126, 75.6126, 75.6126, 75.6126…
## $ MAD3 <dbl> 226.8378, 226.8378, 226.8378, 226.8378, 226.8378, 22…
## $ MAD_Exclude_Score <dbl> 738.8378, 738.8378, 738.8378, 738.8378, 738.8378, 73…
## $ Mean <dbl> 521.0064, 521.0064, 521.0064, 521.0064, 521.0064, 52…
## $ SD <dbl> 86.11115, 86.11115, 86.11115, 86.11115, 86.11115, 86…
## $ SD3 <dbl> 258.3334, 258.3334, 258.3334, 258.3334, 258.3334, 25…
## $ SD_Exclude_Score <dbl> 779.3398, 779.3398, 779.3398, 779.3398, 779.3398, 77…
## $ mean_bias <dbl> 14.69231, 3.85000, 3.85000, 14.69231, 3.85000, 3.850…
## $ mean_toward <dbl> 79.34146, 94.39024, 94.39024, 79.34146, 94.39024, 94…
## $ mean_away <dbl> 80.11429, 97.69231, 97.69231, 80.11429, 97.69231, 97…
## $ peak_toward <dbl> 244, 302, 302, 244, 302, 302, 302, 302, 302, 302, 30…
## $ peak_away <dbl> 315, 243, 243, 315, 243, 243, 243, 243, 243, 243, 24…
## $ variability <dbl> 76.10667, 78.73418, 78.73418, 76.10667, 78.73418, 78…
## $ trials_toward <int> 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, …
## $ trials_away <int> 35, 39, 39, 35, 39, 39, 39, 39, 39, 39, 39, 39, 39, …
## $ trials_NA <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ trials_total <int> 76, 80, 80, 76, 80, 80, 80, 80, 80, 80, 80, 80, 80, …
Grouped_df <- df %>%
group_by(Subject, condition.f, Session, cue) %>%
summarise(Mean_Bias = mean(mean_bias),
Mean_Toward = mean(mean_toward),
Mean_Away = mean(mean_away),
Peak_Toward = mean(peak_toward),
Peak_Away = mean(peak_away),
Variability = mean(variability)) %>%
arrange(Subject, Session)
## `summarise()` has grouped output by 'Subject', 'condition.f', 'Session'. You
## can override using the `.groups` argument.
Grouped_df
##
## Error: Subject
## Df Sum Sq Mean Sq
## condition.f 1 1130 1130
##
## Error: Subject:condition.f
## Df Sum Sq Mean Sq
## condition.f 1 714.1 714.1
##
## Error: Subject:Session
## Df Sum Sq Mean Sq
## condition.f 1 1833 1833.3
## Session 4 2855 713.7
##
## Error: Subject:cue
## Df Sum Sq Mean Sq
## cue 1 3409 3409
##
## Error: Subject:condition.f:Session
## Df Sum Sq Mean Sq
## condition.f 1 1 1
## Session 4 8582 2146
##
## Error: Subject:condition.f:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 0.1842 0.1842
##
## Error: Subject:Session:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 107 107.0
## Session:cue 4 1172 292.9
##
## Error: Subject:condition.f:Session:cue
## Df Sum Sq Mean Sq
## cue 1 890 890
## condition.f:cue 1 201 201
## Session:cue 3 10104 3368
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## condition.f 1 40 40.4 0.034 0.854
## Session 5 1428 285.5 0.239 0.945
## cue 1 318 317.9 0.266 0.607
## condition.f:Session 5 7899 1579.8 1.322 0.257
## condition.f:cue 1 1500 1500.2 1.255 0.264
## Session:cue 5 1506 301.2 0.252 0.938
## condition.f:Session:cue 5 5219 1043.8 0.873 0.500
## Residuals 190 227104 1195.3
##
## Error: Subject
## Df Sum Sq Mean Sq
## condition.f 1 19443 19443
##
## Error: Subject:condition.f
## Df Sum Sq Mean Sq
## condition.f 1 9233 9233
##
## Error: Subject:Session
## Df Sum Sq Mean Sq
## condition.f 1 6276 6276
## Session 4 43359 10840
##
## Error: Subject:cue
## Df Sum Sq Mean Sq
## cue 1 117.5 117.5
##
## Error: Subject:condition.f:Session
## Df Sum Sq Mean Sq
## condition.f 1 1310 1310
## Session 4 97716 24429
##
## Error: Subject:condition.f:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 2329 2329
##
## Error: Subject:Session:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 161.8 161.8
## Session:cue 4 738.7 184.7
##
## Error: Subject:condition.f:Session:cue
## Df Sum Sq Mean Sq
## cue 1 373.7 373.7
## condition.f:cue 1 1139.0 1139.0
## Session:cue 3 3112.2 1037.4
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## condition.f 1 9966 9966 1.535 0.217
## Session 5 9639 1928 0.297 0.914
## cue 1 715 715 0.110 0.740
## condition.f:Session 5 16824 3365 0.518 0.762
## condition.f:cue 1 116 116 0.018 0.894
## Session:cue 5 1177 235 0.036 0.999
## condition.f:Session:cue 5 2183 437 0.067 0.997
## Residuals 190 1233808 6494
##
## Error: Subject
## Df Sum Sq Mean Sq
## condition.f 1 10497 10497
##
## Error: Subject:condition.f
## Df Sum Sq Mean Sq
## condition.f 1 3954 3954
##
## Error: Subject:Session
## Df Sum Sq Mean Sq
## condition.f 1 62.9 62.9
## Session 4 2242.6 560.6
##
## Error: Subject:cue
## Df Sum Sq Mean Sq
## cue 1 18542 18542
##
## Error: Subject:condition.f:Session
## Df Sum Sq Mean Sq
## condition.f 1 15252 15252
## Session 4 23875 5969
##
## Error: Subject:condition.f:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 1668 1668
##
## Error: Subject:Session:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 3.5 3.53
## Session:cue 4 864.2 216.05
##
## Error: Subject:condition.f:Session:cue
## Df Sum Sq Mean Sq
## cue 1 2819.7 2819.7
## condition.f:cue 1 30.4 30.4
## Session:cue 3 2985.0 995.0
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## condition.f 1 1157 1157 0.205 0.652
## Session 5 14029 2806 0.496 0.779
## cue 1 795 795 0.141 0.708
## condition.f:Session 5 7085 1417 0.251 0.939
## condition.f:cue 1 25 25 0.004 0.947
## Session:cue 5 2957 591 0.105 0.991
## condition.f:Session:cue 5 2070 414 0.073 0.996
## Residuals 190 1074161 5653
##
## Error: Subject
## Df Sum Sq Mean Sq
## condition.f 1 109217 109217
##
## Error: Subject:condition.f
## Df Sum Sq Mean Sq
## condition.f 1 37476 37476
##
## Error: Subject:Session
## Df Sum Sq Mean Sq
## condition.f 1 36524 36524
## Session 4 244019 61005
##
## Error: Subject:cue
## Df Sum Sq Mean Sq
## cue 1 489.2 489.2
##
## Error: Subject:condition.f:Session
## Df Sum Sq Mean Sq
## condition.f 1 35894 35894
## Session 4 222751 55688
##
## Error: Subject:condition.f:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 73496 73496
##
## Error: Subject:Session:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 5588 5588
## Session:cue 4 41349 10337
##
## Error: Subject:condition.f:Session:cue
## Df Sum Sq Mean Sq
## cue 1 3093 3093
## condition.f:cue 1 28792 28792
## Session:cue 3 8789 2930
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## condition.f 1 61250 61250 1.160 0.283
## Session 5 37152 7430 0.141 0.983
## cue 1 455 455 0.009 0.926
## condition.f:Session 5 67282 13456 0.255 0.937
## condition.f:cue 1 0 0 0.000 0.998
## Session:cue 5 24177 4835 0.092 0.993
## condition.f:Session:cue 5 23296 4659 0.088 0.994
## Residuals 190 10033008 52805
##
## Error: Subject
## Df Sum Sq Mean Sq
## condition.f 1 24742 24742
##
## Error: Subject:condition.f
## Df Sum Sq Mean Sq
## condition.f 1 1404 1404
##
## Error: Subject:Session
## Df Sum Sq Mean Sq
## condition.f 1 41 41
## Session 4 14230 3558
##
## Error: Subject:cue
## Df Sum Sq Mean Sq
## cue 1 92398 92398
##
## Error: Subject:condition.f:Session
## Df Sum Sq Mean Sq
## condition.f 1 93325 93325
## Session 4 64486 16122
##
## Error: Subject:condition.f:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 4881 4881
##
## Error: Subject:Session:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 14472 14472
## Session:cue 4 19380 4845
##
## Error: Subject:condition.f:Session:cue
## Df Sum Sq Mean Sq
## cue 1 24416 24416
## condition.f:cue 1 3189 3189
## Session:cue 3 12751 4250
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## condition.f 1 5863 5863 0.124 0.725
## Session 5 15989 3198 0.068 0.997
## cue 1 12465 12465 0.265 0.608
## condition.f:Session 5 16032 3206 0.068 0.997
## condition.f:cue 1 2346 2346 0.050 0.824
## Session:cue 5 29103 5821 0.124 0.987
## condition.f:Session:cue 5 34116 6823 0.145 0.981
## Residuals 190 8950961 47110
##
## Error: Subject
## Df Sum Sq Mean Sq
## condition.f 1 5346 5346
##
## Error: Subject:condition.f
## Df Sum Sq Mean Sq
## condition.f 1 3081 3081
##
## Error: Subject:Session
## Df Sum Sq Mean Sq
## condition.f 1 39 38.8
## Session 4 6743 1685.8
##
## Error: Subject:cue
## Df Sum Sq Mean Sq
## cue 1 839.9 839.9
##
## Error: Subject:condition.f:Session
## Df Sum Sq Mean Sq
## condition.f 1 6585 6585
## Session 4 27051 6763
##
## Error: Subject:condition.f:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 2203 2203
##
## Error: Subject:Session:cue
## Df Sum Sq Mean Sq
## condition.f:cue 1 469.6 469.6
## Session:cue 4 836.3 209.1
##
## Error: Subject:condition.f:Session:cue
## Df Sum Sq Mean Sq
## cue 1 129.5 129.5
## condition.f:cue 1 569.7 569.7
## Session:cue 3 306.2 102.1
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## condition.f 1 3402 3402 0.895 0.345
## Session 5 1982 396 0.104 0.991
## cue 1 103 103 0.027 0.870
## condition.f:Session 5 3715 743 0.195 0.964
## condition.f:cue 1 332 332 0.087 0.768
## Session:cue 5 371 74 0.020 1.000
## condition.f:Session:cue 5 1286 257 0.068 0.997
## Residuals 190 722574 3803
## `summarise()` has grouped output by 'condition.f', 'Session'. You can override
## using the `.groups` argument.