-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProbSwitchFactorized.Rmd
2724 lines (2231 loc) · 123 KB
/
ProbSwitchFactorized.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "ProbSwitchFactorized"
output: html_notebook
---
# Define functions
## Read in and clean data
```{r Read in ages}
get_ages = function(ages_file_dir) {
ages = read.csv(ages_file_dir)
colnames(ages)[colnames(ages) == "ID"] = "sID"
# subset(ages, !Gender %in% c(1, 2)) # IDs 307 & 336 are "3"?! asked Sarah 3/19/2019
# subset(ages, is.na(Columbia_cat)) # subjects 11-16, 34 and 55 are missing PDS
# subset(ages, Gender==1 & PDS >3.5) # we only have 1 boy with PDS > 3.5, but 22 girls
ages$Gender = factor(ages$Gender, levels=c(2, 1, 3), labels=c("Female", "Male", NA))
ages$Columbia_cat[ages$sID >= 300] = 10 # community adults (25-30)
ages$Columbia_cat[ages$sID >= 400] = 9 # Berkeley undergrads (ca. 17-25)
ages$Columbia_cat = factor(ages$Columbia_cat, labels=c("Pre", "Pub", "Post", "18-24", "25-30", NA))
# Add testost. group
ages$log_T = log(ages$meanT)
ggplot(ages, aes(log_T, fill=Gender)) + geom_histogram(bins=15, position = position_dodge())
make_groups = function(ages, colname, probs=c(1, 3/4, 2/4, 1/4)) {
quantiles_f = quantile(ages[ages$sID < 300 & ages$Gender=="Female", colname], probs=probs, na.rm=T)
quantiles_m = quantile(ages[ages$sID < 300 & ages$Gender=="Male", colname], probs=probs, na.rm=T)
groups = rep(NA, nrow(ages))
for (i in 1:length(probs)) {
groups[ages$Gender=="Female" & ages[,colname] <= quantiles_f[i]] = paste0(round(100*probs[i], digits=0), "%")
groups[ages$Gender=="Male" & ages[,colname] <= quantiles_m[i]] = paste0(round(100*probs[i], digits=0), "%")
}
# Split adults by recruitment
groups[ages$sID >= 300] = "25-30"
groups[ages$sID >= 400] = "18-24"
# Split adults by age
groups[ages$PreciseYrs > 17] = "18-24"
groups[ages$PreciseYrs > 25] = "25-30"
return(groups)
}
ages$T_group = make_groups(ages, "meanT")
ages$T_group = factor(ages$T_group, levels=c("25%", "50%", "75%", "100%", "18-24", "25-30"))
ages$age_group = make_groups(ages, "PreciseYrs")
ages$age_group = factor(ages$age_group, levels=c("25%", "50%", "75%", "100%", "18-24", "25-30"))
ages$PDS[ages$age_group == "18-24"] = 9
ages$PDS[ages$age_group == "25-30"] = 10
ages$PDS = as.numeric(ages$PDS)
ages$PDS_group[ages$PDS == 1] = "pre"
PDS_sub = (ages$PDS > 1) & !(is.na(ages$PDS))
ages$PDS_group[PDS_sub] = make_groups(ages[PDS_sub,], "PDS", probs=c(1, 2/3, 1/3))
ages$PDS_group = factor(ages$PDS_group, levels=c("pre", "33%", "67%", "100%", "18-24", "25-30"))
ddply(ages, .(T_group, Gender), T=min(meanT, na.rm=T), summarize) # check that assignment was right (min matches up with quantiles)
ggplot(ages, aes(Columbia_cat)) +
geom_histogram(stat="count") +
facet_grid(~ Gender)
ggplot(ages, aes(PDS)) +
geom_histogram() +
facet_grid(~ Gender)
return(ages)
}
```
```{r Read in data}
get_data = function(data_dir, data_name, patt) {
# data_dir = human_data_dir
# data_name = "human"
# patt = ".*csv"
all_files = data.frame()
missed_trials = data.frame()
filenames = list.files(data_dir, pattern = patt)
print(paste(length(filenames), "filenames on the list."))
# filename = filenames[1] # DEBUG
for(filename in filenames) {
if (data_name == "human_mat") {
mat_file = readMat(file.path(data_dir, filename))$exp[,,1]$PROBSWITCHdata[,,1]
subj_file = data.frame(RT = t(mat_file$RT))
subj_file$selected_box = (t(mat_file$key) - 10) / 2 # recode to make key left==0 and key right==1 (initially key right==12; key left==10)
subj_file$reward = t(mat_file$reward) # reward==1, no reward==0
subj_file$correct_box = 1 - t(mat_file$better.box.left) # recode to make left==0 and right==1 (initially the opposite)
sID = as.numeric(strsplit(strsplit(filename, split = "PROBSWITCH_")[[1]][2], ".mat")[[1]])
subj_file$sID = sID
missed_data = subset(subj_file, is.na(reward))
missed_trials = as.data.frame(rbind(missed_trials, missed_data))
subj_file = subset(subj_file, !is.nan(reward) & selected_box %in% c(0, 1)) # remove no-response trials
subj_file = subj_file[47:nrow(subj_file),] # remove instruction trials
} else {
subj_file = read.csv(file.path(data_dir, filename))
}
# Make sure all files have the same columns
## Remove unwanted columns
for (col_name in colnames(subj_file)) {
if (!col_name %in% desired_columns) {
subj_file[,col_name] = NULL
}
}
## Add NA columns for wanted columns
for (col_name in desired_columns) {
if (!col_name %in% colnames(subj_file)) {
subj_file[,col_name] = NA
}
}
if (data_name == "PCsim") {
# Fill in arbitrary data
for (col_name in c("T_group", "PDS_group", "Columbia_cat", "Category", "BMI", "Period_age", "Last_period", "Months", "T1", "T2", "T3")) {
if (!col_name %in% colnames(subj_file)) {
subj_file[,col_name] = 1
}
}
for (col_name in c("PDS", "meanT", "PreciseYrs", "log_T")) {
if (!col_name %in% colnames(subj_file)) {
subj_file[,col_name] = runif(1, 8, 30)
}
}
subj_file$age_group = "50%"
subj_file$Gender = "Male"
}
# Add columns
subj_file$TrialID = 1:nrow(subj_file)
subj_file$rewardversion = subj_file$sID %% 4
subj_file$ACC = with(subj_file, selected_box == correct_box)
# # Sort by TrialID
# subj_file = subj_file[order(subj_file$sID, subj_file$TrialID),]
# Get switch_trial, block, and trialssinceswitch
this_trial = subj_file$correct_box[2:nrow(subj_file)]
prev_trial = subj_file$correct_box[1:(nrow(subj_file) - 1)]
subj_file$switch_trial = c(F, this_trial != prev_trial)
subj_file$block = cumsum(subj_file$switch_trial)
subj_file$trialsinceswitch = NA
for (blocki in subj_file$block) {
n_rows = nrow(subset(subj_file, block == blocki))
if (n_rows >= 4) {
subj_file$trialsinceswitch[subj_file$block == blocki] = 0:(n_rows - 1)
subj_file$trialsinceswitch[subj_file$block == blocki][(n_rows - 3):n_rows] = -4:-1
}
}
subj_file$outcome_1_back = c(NA, subj_file$reward[1:(nrow(subj_file) - 1)])
subj_file$outcome_2_back = c(NA, NA, subj_file$reward[1:(nrow(subj_file) - 2)])
subj_file$choice_left = subj_file$selected_box == 0 # left: 0; right: 1
subj_file$choice_1_back = c(NA, subj_file$choice_left[1:(nrow(subj_file) - 1)])
subj_file$choice_2_back = c(NA, NA, subj_file$choice_left[1:(nrow(subj_file) - 2)])
if (data_name == "human_mat") {
write.csv(subj_file, paste(data_dir, "/PS_", sID, ".csv", sep = ""), row.names = F)
}
all_files = as.data.frame(rbind(all_files, subj_file))
}
reward_versions = ddply(all_files, .(sID), summarize, rewardversion = rewardversion[1])
write.csv(reward_versions, paste0(data_dir, "_rewardversions.csv"))
print(paste("Read in data from", length(unique(all_files$sID)), "unique subjects (might be more simulations)."))
summary(all_files)
summary(subj_file)
colnames(all_files)
if (nrow(missed_trials) > 0) { # Fingers crossed that this works
write.csv(missed_trials, paste0(data_dir, "missed_trials.csv"))
}
return(all_files)
}
```
```{r Remaining fixes for all_filess}
finish_all_files = function(all_files) {
# Sort all_files
all_files = all_files[order(all_files$sID, all_files$TrialID),]
# Remove missed trials
all_files = subset(all_files, !is.na(reward))
# Add columns
all_files$outcome_21_back = paste(all_files$outcome_2_back, all_files$outcome_1_back)
all_files$outcome_21_back = factor(all_files$outcome_21_back,
levels = c("1 1", "0 1", "1 0", "0 0"),
labels = c("both reward", "no reward, reward", "reward, no reward", "both no reward"))
all_files$same_choice_01_back = all_files$choice_left == all_files$choice_1_back # same choice in this trial as in the last?
all_files$same_choice_12_back = all_files$choice_1_back == all_files$choice_2_back # same choice in this trial as in the last?
all_files$choice_12_back = ifelse(all_files$choice_1_back, "left", "right")
all_files$choice_12_back[!all_files$same_choice_12_back | is.na(all_files$same_choice_12_back)] = NA
all_files$reward_port = factor(all_files$correct_box, levels = c(0, 1), labels = c("Left", "Right"))
xyz = ddply(all_files, .(sID, Gender, age_group), x = NA, summarize)
ddply(xyz, .(age_group, Gender), summarize, n=length(age_group))
# stay
all_files$stay = with(all_files, c(choice_left == choice_1_back))
# subset(all_files, select=c(sID, TrialID, choice_left, choice_1_back, ACC, selected_box, stay))
# Part / section / block
# ddply(all_files, .(sID), summarize, n_trials=max(TrialID)) # Most participants have 131 trials; a few early ones have ~156
all_files$part = NA
all_files$part[all_files$TrialID > 11] = 1
all_files$part[all_files$TrialID > 11+1*40] = 2
all_files$part[all_files$TrialID > 11+2*40] = 3
all_files$part[all_files$TrialID > 11+3*40] = NA
# Get win-stay and loose-shift trials
all_files$win_stay = with(all_files, (outcome_1_back == 1) & (stay == 1))
all_files$lose_shift = with(all_files, (outcome_1_back == 0) & (stay == 0))
# subset(all_files, select=c(sID, TrialID, selected_box, reward, outcome_1_back, stay, win_stay, lose_shift))
return(all_files)
}
```
```{r get_ACC_reward_subj}
add_age_group_yrs = function(data) {
data$age_group_ = as.character(data$age_group)
data$age_group_[data$age_group_ == "25%"] = 9
data$age_group_[data$age_group_ == "50%"] = 11.5
data$age_group_[data$age_group_ == "75%"] = 14
data$age_group_[data$age_group_ == "100%"] = 16.5
data$age_group_[data$age_group_ == "18-24"] = 21.5
data$age_group_[data$age_group_ == "25-30"] = 27.5
data$age_group_ = as.numeric(data$age_group_)
return(data)
}
get_ACC_reward_subj = function(all_files) {
# ACC_subj
ACC_subj = ddply(all_files,
.(sID, Gender, T_group, meanT, age_group, PDS_group, PDS, PreciseYrs, Columbia_cat, trialsinceswitch, model_name), summarize, # 2020/06/02: replaced log_T with meanT after re-reading Sarah's methods
ACC = mean(ACC, na.rm=T)) # to make sure error bars are right
ACC_subj = add_age_group_yrs(ACC_subj)
ACC_subj_block = ddply(all_files,
.(sID, Gender, T_group, meanT, age_group, PDS_group, PDS, PreciseYrs, Columbia_cat, trialsinceswitch, model_name, part), summarize,
ACC = mean(ACC, na.rm=T)) # to make sure error bars are right
# reward_subj
reward_subj = ddply(subset(all_files, !is.na(outcome_21_back) & !is.na(choice_12_back)),
.(sID, Gender, PDS, T_group, PDS_group, meanT, Columbia_cat, outcome_21_back, age_group, model_name, PreciseYrs), summarize,
stay = mean(same_choice_01_back, na.rm = T))
reward_subj = add_age_group_yrs(reward_subj)
reward_subj_block = ddply(subset(all_files, !is.na(outcome_21_back) & !is.na(choice_12_back)),
.(sID, part, Gender, PDS, T_group, PDS_group, meanT, Columbia_cat, outcome_21_back, age_group, model_name, PreciseYrs), summarize,
stay = mean(same_choice_01_back, na.rm = T))
# ACCs
ACCs = ddply(all_files, .(sID, PreciseYrs, Gender, PDS, meanT, age_group, PDS_group, T_group), summarize,
mean_ACC = mean(ACC, na.rm=T),
mean_reward = mean(reward, na.rm=T),
mean_stay = mean(stay, na.rm=T),
n_switches=120 * mean(switch_trial, na.rm=T),
p_switch=mean(switch_trial, na.rm=T),
n_fast_RTs=sum(RT<100, na.rm=T),
# median_RT=median(RT, na.rm=T),
n_trials=length(RT))
ACCs$median_cor_RT = ddply(subset(all_files, ACC==T), .(sID, PreciseYrs, Gender, PDS, age_group, PDS_group, T_group), summarize,
median_cor_RT = median(RT, na.rm=T))$median_cor_RT
ACCs = add_age_group_yrs(ACCs)
return(list(ACC_subj, ACC_subj_block, reward_subj, reward_subj_block, ACCs))
}
```
## Analyzing behavior
```{r Exclusion}
plot_indivduals_for_exclusion = function(all_files, exclude_UCB) {
# Overall behavior for each participant
ACCs = ddply(all_files, .(sID, PreciseYrs, Gender, PDS, meanT, age_group), summarize,
mean_ACC = mean(ACC, na.rm=T),
n_switches=120 * mean(switch_trial, na.rm=T),
n_fast_RTs=sum(RT<100, na.rm=T),
mean_RT=mean(RT, na.rm=T),
n_trials=length(RT))
mean(ACCs$n_switches)
sd(ACCs$n_switches)
# Sort by ACC
ACCs = ACCs[order(ACCs$mean_ACC),]
ACCs$ACC_ix = 1:nrow(ACCs)
# Sort by number of switches
ACCs = ACCs[order(ACCs$n_switches),]
ACCs$switch_ix = 1:nrow(ACCs)
# Sort by number of <100msec trial
ACCs = ACCs[order(ACCs$n_fast_RTs),]
ACCs$fast_ix = 1:nrow(ACCs)
# Plot each one
gg_0excl_ACC = ggplot(ACCs, aes(ACC_ix, mean_ACC, color=PreciseYrs)) +
geom_point()
gg_0excl_switch = ggplot(ACCs, aes(switch_ix, n_switches, color=PreciseYrs)) +
geom_point()
gg_0excl_fast = ggplot(ACCs, aes(fast_ix, n_fast_RTs, color=PreciseYrs)) +
geom_point()
# Tell us who'll be excluded
special_excl = 1004
gender_excl = sort(subset(ACCs, is.na(Gender))$sID)
# PDS_excl = sort(subset(ACCs, is.na(PDS) & sID < 400)$sID)
# T_excl = sort(subset(ACCs, is.na(meanT) & sID < 400)$sID)
age_excl = sort(subset(ACCs, is.na(PreciseYrs))$sID)
n_trials_excl = sort(subset(ACCs, n_trials < 120)$sID)
switch_excl = sort(subset(ACCs, n_switches < 5)$sID)
ACC_excl = sort(subset(ACCs, mean_ACC < 0.58)$sID)
above30_excl = sort(subset(ACCs, PreciseYrs > 30)$sID)
if (exclude_UCB) {
above30_excl = c(above30_excl, subset(ACCs, sID > 400)$sID)
}
print("Excluding subject 1004.")
print(paste("Participants missing gender:", paste(gender_excl, collapse=', '), "unsorted Ages:", paste(subset(ACCs, sID %in% gender_excl)$PreciseYrs, collapse=', ')))
# print(paste("Participants missing PDS:", paste(PDS_excl, collapse=', '), "unsorted Ages:", paste(subset(ACCs, sID %in% PDS_excl)$PreciseYrs, collapse=', ')))
# print(paste("Participants missing T:", paste(T_excl, collapse=', '), "unsorted Ages:", paste(subset(ACCs, sID %in% T_excl)$PreciseYrs, collapse=', ')))
print(paste("Participants with < 120 trials:", paste(n_trials_excl, collapse=', '), "unsorted Ages:", paste(subset(ACCs, sID %in% n_trials_excl)$PreciseYrs, collapse=', ')))
print(paste("Participants with n_switches < 5:", paste(switch_excl, collapse=', '), "unsorted Ages:", paste(subset(ACCs, sID %in% switch_excl)$PreciseYrs, collapse=', ')))
print(paste("Participants with mean_ACC < 0.58:", paste(ACC_excl, collapse=', '), "unsorted Ages:", paste(subset(ACCs, sID %in% ACC_excl)$PreciseYrs, collapse=', ')))
print(paste("Participants > 30:", paste(above30_excl, collapse=', '), "unsorted Ages:", paste(subset(ACCs, sID %in% above30_excl)$PreciseYrs, collapse=', ')))
all_excluded = unique(c(gender_excl, n_trials_excl, switch_excl, ACC_excl, above30_excl, special_excl)) # , PDS_excl, T_excl
# Save plots
ggsave(file.path(plot_dir, "/gg_0excl_ACC.eps"), gg_0excl_ACC)
ggsave(file.path(plot_dir, "/gg_0excl_switch.eps"), gg_0excl_switch)
ggsave(file.path(plot_dir, "/gg_0excl_fast.eps"), gg_0excl_fast)
ggsave(file.path(plot_dir, "/gg_0excl_ACC.eps"), gg_0excl_ACC)
ggsave(file.path(plot_dir, "/gg_0excl_switch.eps"), gg_0excl_switch)
ggsave(file.path(plot_dir, "/gg_0excl_fast.eps"), gg_0excl_fast)
return(all_excluded)
}
```
```{r Create regression data}
get_regression_dat = function(all_files) {
all_files_regr = subset(all_files, select = c("sID", "TrialID", "Gender", "PreciseYrs", "PDS", "log_T", "age_group", "PDS_group", "T_group", "block", "reward", "selected_box", "model_name"))
all_files_regr = all_files_regr[order(all_files_regr$model_name, all_files_regr$sID, all_files_regr$TrialID),]
## Age regressors
all_files_regr$PreciseYrs2 = all_files_regr$PreciseYrs ^ 2
all_files_regr$age_z = (all_files_regr$PreciseYrs - mean(all_files_regr$PreciseYrs)) / sd(all_files_regr$PreciseYrs)
all_files_regr$age2_z = (all_files_regr$PreciseYrs2 - mean(all_files_regr$PreciseYrs2)) / sd(all_files_regr$PreciseYrs2)
## PDS regressors
fem = subset(all_files_regr, (Gender == "Female") & (PreciseYrs < 18))
mal = subset(all_files_regr, (Gender == "Male") & (PreciseYrs < 18))
# all_files_regr$PDS_z = NA
all_files_regr[(all_files_regr$Gender == "Female") & (all_files_regr$PreciseYrs < 18), "PDS_z"] = (fem$PDS - mean(fem$PDS, na.rm=T)) / sd(fem$PDS, na.rm=T)
all_files_regr[(all_files_regr$Gender == "Male") & (all_files_regr$PreciseYrs < 18), "PDS_z"] = (mal$PDS - mean(mal$PDS, na.rm=T)) / sd(mal$PDS, na.rm=T)
## T regressors
all_files_regr$T_z = NA
all_files_regr[(all_files_regr$Gender == "Female") & (all_files_regr$PreciseYrs < 18), "T_z"] = (fem$log_T - mean(fem$log_T, na.rm=T)) / sd(fem$log_T, na.rm=T)
all_files_regr[(all_files_regr$Gender == "Male") & (all_files_regr$PreciseYrs < 18), "T_z"] = (mal$log_T - mean(mal$log_T, na.rm=T)) / sd(mal$log_T, na.rm=T)
all_files_regr$selected = as.numeric(as.character(factor(all_files_regr$selected_box, levels = c(0, 1), labels = c(-1, 1)))) # 0 / -1: left; +1: right
## Choice regressors
all_files_regr$right_rew = all_files_regr$selected_box
all_files_regr$right_rew[all_files_regr$reward == 0] = -all_files_regr$right_rew[all_files_regr$reward == 0]
all_files_regr$left_rew = 1 - all_files_regr$selected_box
all_files_regr$left_rew[all_files_regr$reward == 0] = -all_files_regr$left_rew[all_files_regr$reward == 0]
## Reward regressors
all_files_regr$reward_orig = all_files_regr$reward # reward == 1; noReward == 0
all_files_regr$noReward = 1 - all_files_regr$reward # noReward == 1; reward == 0
all_files_regr$reward[all_files_regr$selected_box == 0] = -all_files_regr$reward[all_files_regr$selected_box == 0] # reward[action_right] = 1; reward[action_left] = -1
all_files_regr$noReward[all_files_regr$selected_box == 0] = -all_files_regr$noReward[all_files_regr$selected_box == 0] # noReward[action_right] = 1; noReward[action_left] = -1
all_files_regr$selected_box = factor(all_files_regr$selected_box)
## nback reward regressors
reward_predictors = c("reward", "noReward")
for (n in 1:10) {
for (cond in reward_predictors) {
col_name = paste0("back", n, cond)
all_files_regr[, col_name] = c(rep(NA, n), all_files_regr[1:(nrow(all_files_regr)-n), cond])
}
}
return(all_files_regr)
}
# subj_dat = subset(all_files_regr, sID == 22 & model_name == "Human")
# # subj_dat = subj_dat[order(subj_dat$TrialID),]
# ggplot(subj_dat, aes(TrialID, back1reward, color=factor(selected_box), group=1)) +
# geom_point() #+
# geom_line()
#
# subset(subj_dat, select=c(TrialID, reward, back1reward))
```
```{r old}
run_regressions = function(all_files_regr) {
# Run regression models for each subject, for each n in n-back
nback_regr_dat = data.frame()
reward_predictors = c("reward", "noReward")
sel_rew_predictors = c("selected", "reward")
choice_predictors = c("left_rew", "right_rew")
# subj = all_files_regr$sID[1]
for (subj in unique(all_files_regr$sID)) {
subj_dat = subset(all_files_regr, sID == subj & !is.na(selected_box))
if (nrow(subj_dat) > 0) {
## Add nback columns
# n = 1
for (n in 1:12) {
# cond = choice_predictors[1]
for (cond in c(choice_predictors, reward_predictors, sel_rew_predictors)) {
# for (cond in c(reward_predictors)) {
col_name = paste0("back", n, cond)
subj_dat[,col_name] = c(rep(NA, n), subj_dat[1:(nrow(subj_dat)-n), cond])
}
## Remove conditions with perfect predictors
box_nor = mean(as.numeric(as.character(subset(subj_dat, back1reward == -1)$selected_box)))
box_rew = mean(as.numeric(as.character(subset(subj_dat, back1reward == 1)$selected_box)))
if (T) {#} ((!box_nor %in% c(0, 1)) * (!box_rew %in% c(0, 1))) {
# ## Selected/rewarded model (predictors: selected [left: -1, right: +1], reward [left: -1, right: +1, none: 0])
# sel_rew_formula = paste("selected_box ~", paste(paste0("back", n, sel_rew_predictors), collapse = " + "), "+ (1 | block)")
# sel_rew_mod = glm(as.formula(sel_rew_formula),
# family = "binomial",
# data = subj_dat)
# sel_rew_coefs = as.data.frame(summary(sel_rew_mod)$coef)
# sel_rew_coefss = cbind(sID = subj, model = "selectedRewarded", predictor = rownames(sel_rew_coefs), back = n, data.frame(sel_rew_coefs, row.names = NULL))
# ## Choice model (predictors: left_rew [reward: +1, no reward: -1, none: 0], right_rew [reward: +1, no reward: -1, none: 0])
# choice_formula = paste("selected_box ~ ", paste(paste0("back", n, choice_predictors), collapse = " + "))
# choice_mod = glm(as.formula(choice_formula),
# family = "binomial",
# data = subj_dat)
# choice_coefs = as.data.frame(summary(choice_mod)$coef)
# choice_coefss = cbind(sID = subj, model = "leftRight", predictor = rownames(choice_coefs), back = n, data.frame(choice_coefs, row.names = NULL))
## Reward model (predictors: reward [left: -1, right: +1, none: 0], no reward [left: -1, right: +1, none: 0])
reward_formula = paste("selected_box ~", paste(paste0("back", n, reward_predictors), collapse = " + "))
reward_mod = glm(as.formula(reward_formula),
family = "binomial", maxit=200,
data = subj_dat)
reward_coefs = as.data.frame(summary(reward_mod)$coef)
reward_coefss = cbind(sID = subj, model = "rewardNoReward", predictor = rownames(reward_coefs), back = n, data.frame(reward_coefs, row.names = NULL))
# nback_regr_dat = rbind(nback_regr_dat, choice_coefss, reward_coefss, sel_rew_coefss)
nback_regr_dat = rbind(nback_regr_dat, reward_coefss)
}
}
}
}
# Beautify
nback_regr_dat$predictor = gsub("back.", "", nback_regr_dat$predictor)
nback_regr_dat$predictor = gsub("[0-99]", "", nback_regr_dat$predictor)
nback_regr_dat$sig_Estimate = NA
for (pred in unique(nback_regr_dat$predictor)) {
for (ba in unique(nback_regr_dat$back)) {
sub = subset(nback_regr_dat, predictor==pred & back==ba)
z = (sub$Estimate - mean(sub$Estimate, na.rm=T)) / sd(sub$Estimate, na.rm=T)
nback_regr_dat[nback_regr_dat$predictor==pred & nback_regr_dat$back==ba, "sig_Estimate"] = 2 * (1 / (1 + exp(-sub$Estimate))) - 1 # only tanh transform
nback_regr_dat[nback_regr_dat$predictor==pred & nback_regr_dat$back==ba, "z_sig_Estimate"] = 2 * (1 / (1 + exp(-z))) - 1 # z-score, then tanh transform
}
}
# Summarize over versions (for simulated_humans)
nback_regr_dat = ddply(nback_regr_dat,
colnames(nback_regr_dat)[colnames(nback_regr_dat) != "version"],
summarize,
X = sID[1])
return(nback_regr_dat)
}
# ggplot(nback_regr_dat, aes(Estimate, `Pr...z..`)) +
# geom_point() +
# geom_hline(yintercept=0.99)
#
# ggplot(ddply(subset(nback_regr_dat, `Pr...z..` > 0.99), .(sID, predictor, back, Estimate, predictor), summarize, x=NA), aes(back, Estimate, color=predictor)) +
# geom_point(position="jitter")
#
# weird_regr_people = subset(nback_regr_dat, (Estimate > 18) | (Estimate < -5))
# subset(nback_regr_dat, (sID == 442) & (back == 1))
#
# ggplot(subj_dat, aes(TrialID, back1reward, color=selected_box)) +
# geom_point()
```
# Main script
```{r Load packages, set parameters}
library("ggplot2"); theme_set(theme_classic()); library("plyr"); library("reshape2"); library("R.matlab"); library("zoo"); library(lmerTest); library("ggfortify"); library("RColorBrewer")
# display.brewer.all(n=NULL, type="all", select=NULL, exact.n=TRUE, colorblindFriendly=T)
data_name = "HumAndSim" # can be "PCsim" (only look at PC-based simulations), "HumAndSim" (look at human data and BF-&RL-simulated data), or "Hum" (just humans; leave out simulations)
model_class = "" # can be "BF" or "RL". determines which models will be read in if data_name=="PCsim". otherwise, should be set to ""
exclude_UCB = F
colors_3and1 = c("#00D000", "#00C055", "#00B0AA", "#00A0FF", "gray60", "grey50")
colors_validation = brewer.pal(n=8, name="Set2")
colors_validation[2] = "black"
colors_validation = c(colors_validation, colors_validation)
parameter_names = c('alpha', 'beta', 'nalpha', 'calpha', 'cnalpha', 'p_switch', 'p_reward', 'persev')
desired_columns = c("sID", "selected_box", "p_right", "reward", "RT", "correct", "correct_box", "model_name")
# Directories of all the stuff
ages_file_dir = "C:/Users/maria/MEGAsync/SLCNdata/SLCNinfo2.csv"
human_data_dir = "C:/Users/maria/MEGAsync/SLCNdata/ProbSwitch"
param_file_dir = "C:/Users/maria/MEGAsync/SLCN/PShumanData/fitting/new_ML_models/MCMC/clustermodels/"
dir_to_all_simulations = 'C:/Users/maria/MEGAsync/SLCN/PShumanData/fitting/new_ML_models/MCMC/clustermodels/simulate/simulations'
PC_sim_data_dir = "C:/Users/maria/MEGAsync/SLCNdata/ProbSwitch/PC_sim_dat"
```
```{r get simulated data}
if (data_name == "PCsim") {
plot_dir = file.path(PC_sim_data_dir, "Plots")
if (!file.exists(plot_dir)){
dir.create(plot_dir)
}
all_files = data.frame()
nback_regr_dat = data.frame()
reward_subj = data.frame()
ACC_subj = data.frame()
ACCs = data.frame()
# Get simulated data
patterns = paste0(c("PC_mean_", "PC1_plus_", "PC1_minus_", "PC2_plus_", "PC2_minus_", "PC3_plus_", "PC3_minus_", "PC4_plus_", "PC4_minus_"), model_class)
for (patt in patterns) {
print(patt)
sim_all_files = get_data(PC_sim_data_dir, data_name, patt)
sim_all_files = finish_all_files(sim_all_files)
# excl_subj = plot_indivduals_for_exclusion(sim_all_files, exclude_UCB)
# sim_all_files = subset(sim_all_files, !sID %in% excl_subj)
sim_stuff = get_ACC_reward_subj(sim_all_files)
sim_ACC_subj = sim_stuff[[1]]
sim_reward_subj = sim_stuff[[3]]
sim_ACCs = sim_stuff[[5]]
sim_all_files_regr = get_regression_dat(sim_all_files)
sim_nback_regr_dat = run_regressions(sim_all_files_regr)
sim_nback_regr_dat$age_group = "A"
sim_all_files$model_name = patt
sim_all_files_regr$model_name = patt
sim_nback_regr_dat$model_name = patt
sim_reward_subj$model_name = patt
sim_ACC_subj$model_name = patt
sim_ACCs$model_name = patt
all_files = rbind(all_files, sim_all_files)
nback_regr_dat = rbind(nback_regr_dat, sim_nback_regr_dat)
reward_subj = rbind(reward_subj, sim_reward_subj)
ACC_subj = rbind(ACC_subj, sim_ACC_subj)
ACCs = rbind(ACCs, sim_ACCs)
}
l = length(unique(all_files$model_name)) # to make figures wider
}
```
```{r Read in human and simulated data}
if ((data_name == "HumAndSim") | (data_name == "Hum")) {
# Get human data
plot_dir = file.path(human_data_dir, "Plots")
if (!file.exists(plot_dir)){
dir.create(plot_dir)
}
ages = get_ages(ages_file_dir)
# all_files = get_data(human_data_dir, "human_mat", ".*mat") # Read in mat files, clean, and write to csv; also write missing trials to csv
all_files = get_data(human_data_dir, "human", ".*csv") # Read in cleaned csv files
all_files = merge(all_files, ages, all.x = T)
all_files = finish_all_files(all_files)
excl_subj = plot_indivduals_for_exclusion(all_files, exclude_UCB)
all_files = subset(all_files, !sID %in% excl_subj)
stuff = get_ACC_reward_subj(all_files)
ACC_subj = stuff[[1]]
reward_subj = stuff[[3]]
ACCs = stuff[[5]]
all_files_regr = get_regression_dat(all_files)
nback_regr_dat = run_regressions(all_files_regr)
nback_regr_dat = merge(nback_regr_dat, ages, all.x = T)
nback_regr_dat = add_age_group_yrs(nback_regr_dat)
all_files$model_name = "Human"
all_files_regr$model_name = "Human"
nback_regr_dat$model_name = "Human"
reward_subj$model_name = "Human"
ACC_subj$model_name = "Human"
ACCs$model_name = "Human"
l = 0 # don't make figures wider
}
if (data_name == "HumAndSim") {
# Get simulated data
data_dirs = list.dirs(dir_to_all_simulations, recursive=F)
patt = ".*csv"
for (data_dir in data_dirs) {
# plot_dir = file.path(data_dir, "Plots")
folder_name = strsplit(data_dir, "/")[[1]][length(strsplit(data_dir, "/")[[1]])]
model_name = strsplit(folder_name, "_")[[1]][1]
print(model_name)
# Get data
ages = get_ages(ages_file_dir)
sim_all_files = get_data(data_dir, "simulated_human", ".*csv")
sim_all_files = merge(sim_all_files, ages, all.x = T)
sim_all_files = finish_all_files(sim_all_files)
# Exclude subjects
sim_excl_subj = plot_indivduals_for_exclusion(sim_all_files, exclude_UCB)
sim_all_files = subset(sim_all_files, !sID %in% sim_excl_subj)
# Create summary data
sim_stuff = get_ACC_reward_subj(sim_all_files) # ACC_subj, ACC_subj_block, reward_subj, reward_subj_block, ACCs
sim_ACC_subj = sim_stuff[[1]]
sim_reward_subj = sim_stuff[[3]]
sim_ACCs = sim_stuff[[5]]
sim_all_files_regr = get_regression_dat(sim_all_files)
sim_nback_regr_dat = run_regressions(sim_all_files_regr)
sim_nback_regr_dat = merge(sim_nback_regr_dat, ages, all.x = T)
sim_nback_regr_dat = add_age_group_yrs(sim_nback_regr_dat)
sim_all_files$model_name = model_name
sim_all_files_regr$model_name = model_name
sim_nback_regr_dat$model_name = model_name
sim_reward_subj$model_name = model_name
sim_ACC_subj$model_name = model_name
sim_ACCs$model_name = model_name
all_files = rbind(all_files, sim_all_files)
all_files_regr = rbind(all_files_regr, sim_all_files_regr)
nback_regr_dat = rbind(nback_regr_dat, sim_nback_regr_dat)
reward_subj = rbind(reward_subj, sim_reward_subj)
ACC_subj = rbind(ACC_subj, sim_ACC_subj)
ACCs = rbind(ACCs, sim_ACCs)
}
}
```
```{r Create Meta file}
# Columns needed
# - DONE Overall accuracy
# - DONE Percent stay trials in terms of choice (e.g., same butterfly chosen as the previous this flower came up)
# - DONE Percent stay trials in terms of motor response (i.e., same button press as on previous trial)
# - DONE Percent win-stay trials
# - DONE Percent lose-shift trials
# - DONE Overall RTs (correct trials only; mean; no transformation)
# - DONE Percent missed trials (too slow, no button press)
# - DONE RTs early, middle, late (same)
# - DONE Accuracy early, middle, late (first third, second third, last third of trials)
# - DONE lose-win-stay & win-lose-stay
# - DONE Number of switches
# Missing trials
missed_trials = read.csv(paste0(human_data_dir, "/missed_trials.csv"))
missed_trials$X = NULL
sum_missed = ddply(missed_trials, .(sID), summarize, n_missed=length(sID))
sum_missed$percent_missed = sum_missed$n_missed / max(all_files$TrialID)
sum_missed$n_missed = NULL
dim(sum_missed)
# Overall ACC, stay, win_stay, lose_shift, RTs
sum_ACC = ddply(all_files, .(sID), summarize,
ACC=mean(ACC, na.rm=T), stay=mean(stay, na.rm=T))
sum_RT = ddply(subset(all_files, ACC==1), .(sID), summarize, RTsd=sd(RT, na.rm=T), RT=mean(RT, na.rm=T))
sum_switches = ddply(all_files, .(sID), summarize, n_switches=sum(switch_trial))
sum_dat = merge(merge(sum_ACC, sum_RT), sum_switches)
# Check
dim(sum_dat) # should be 1 row per subject (291 total)
(g = ggplot(sum_dat, aes(sID, ACC, color=sID)) +
geom_point(position="jitter"))
# g + aes(y=RT)
# g + aes(y=win_stay)
# g + aes(y=lose_shift)
# Overall win-lose-stay and lose-win-stay
win_lose_stay =
ddply(subset(all_files, (outcome_21_back=="reward, no reward") & (same_choice_12_back==T)),
.(sID), summarize,
WLS=mean(stay))
lose_win_stay =
ddply(subset(all_files, (outcome_21_back=="no reward, reward") & (same_choice_12_back==T)),
.(sID), summarize,
LWS=mean(stay))
wsls2 = merge(win_lose_stay, lose_win_stay, all=T, by="sID")
win_stay =
ddply(subset(all_files, outcome_1_back==1),
.(sID), summarize,
WS=mean(stay))
lose_stay =
ddply(subset(all_files, outcome_1_back==0),
.(sID), summarize,
LS=mean(stay))
wsls1 = merge(win_stay, lose_stay, all=T, by="sID")
# Check
wsls = merge(wsls2, wsls1, by='sID')
# By parts: ACC, stay, win_stay, lose_shift, RTs
part_sum = ddply(all_files, .(sID, part), summarize,
ACC=mean(ACC, na.rm=T), stay=mean(stay, na.rm=T))
part_RTs = ddply(subset(all_files, ACC==1), .(sID, part), summarize, RTsd=sd(RT, na.rm=T), RT=mean(RT, na.rm=T))
parts = merge(part_sum, part_RTs)
parts = subset(parts, !is.na(part))
# Check
dim(parts) # Should have 3 parts per person, i.e., 3 * 291 = 873 rows
(g = ggplot(parts, aes(part, ACC, color=sID)) +
stat_summary(geom="bar") +
geom_point(position="jitter"))
g + aes(y=RT)
# Combine everything
parts_wide = reshape(parts, timevar="part", idvar=c("sID"), direction="wide")
dim(parts_wide) # should be 1 row per subject (291 total)
ps_params = read.csv("C:/Users/maria/MEGAsync/SLCNdata/Meta/ProbSwitch_base.csv")
dim(ps_params)
sum_dat = merge(merge(ps_params, sum_dat), parts_wide)
sum_dat = merge(sum_dat, sum_missed, all.x = T)
sum_dat$percent_missed[is.na(sum_dat$percent_missed)] = 0 # Replace NA rows (no missed trials) with 0 (0 missed trials)
sum_dat = merge(sum_dat, wsls, all.x=T, by='sID')
# Add criterion trial (need to run chunk 16 before!)
sum_dat = merge(sum_dat, subset(crit_sum, select=c("sID", "criterion_trial")), all.x=T)
# Check
dim(sum_dat)
(g = ggplot(sum_dat, aes(sID, ACC, color=sID)) +
geom_point(position="jitter")) +
geom_smooth()
g + aes(y=ACC.1)
write.csv(sum_dat, "C:/Users/maria/MEGAsync/SLCNdata/Meta/ProbSwitch.csv")
```
```{r Numbers of trials for each participant (they differ! Calculate averages instead of counting!)}
(ggplot(ACCs, aes(n_trials, fill=model_name))
+ geom_histogram()
)
```
```{r Comparing "++" and "-+" trials to see differences between RL and BI}
# Create dataframe for plotting
## Select relevant trials (reward history "rew rew" and "nor rew")
RLBI_dat_all = subset(all_files, outcome_21_back %in% c("both reward", "no reward, reward"))
RLBI_dat = ddply(RLBI_dat_all, .(sID, model_name, PreciseYrs, outcome_21_back), summarize, stay = mean(stay, na.rm=T))
## Calculate difference between both
RLBI_wide = reshape(RLBI_dat, timevar="outcome_21_back", idvar=c("sID", "PreciseYrs", "model_name"), direction="wide")
# for (colname in colnames(RLBI_wide)) {
# if (" " %in% colname) {
# print(colname)
# }
# }
colnames(RLBI_wide) = c("sID", "model_name", "PreciseYrs", "stay_rew_rew", "stay_nor_rew")
RLBI_wide$rew_minus_nor = with(RLBI_wide, stay_rew_rew - stay_nor_rew)
RLBI_wide$model_name = factor(RLBI_wide$model_name, levels = c("Human", "RLabnp2", "Bbspr"))
# Make plots
(s_RLBI_rew_nor = ggplot(RLBI_wide, aes(model_name, rew_minus_nor, fill=model_name)) +
# geom_point(position="jitter") +
stat_summary(geom="bar", position=position_dodge(width=0.9)) +
stat_summary(geom="pointrange", position=position_dodge(width=0.9)) +
labs(x = "", y = "Difference in stay between reward-reward and noReward-reward (%)")
)
(s_RLBI_rew_nor_age = ggplot(subset(RLBI_wide, model_name == "Human"), aes(PreciseYrs, rew_minus_nor)) +
geom_point(position="jitter") +
geom_smooth()
)
# Save plot
ggsave(file.path(plot_dir, paste0(model_class, "s_RLBI_rew_nor.png")), s_RLBI_rew_nor, width=3.5, height=3.5)
ggsave(file.path(plot_dir, paste0(model_class, "s_RLBI_rew_nor.eps")), s_RLBI_rew_nor, width=3.5, height=3.5)
# T-test
for (model_n in levels(as.factor(RLBI_wide$model_name))) {
print(model_n)
print(t.test(subset(RLBI_wide, model_name == model_n)$rew_minus_nor))
}
```
```{r Sample Composition}
# Prepare data
dat = ddply(ACC_subj, .(sID, Gender, age_group, PDS_group, T_group, PreciseYrs, PDS, meanT), summarize, x=NA)
# Plot
(s_agegroup_n = ggplot(dat, aes(age_group, fill=age_group)) +
geom_histogram(stat="count") +
scale_fill_manual(values=colors_3and1) +
labs(x="", y="Participant n") +
facet_grid(~ Gender))
s_PDSgroup_n = s_agegroup_n +
aes(x=PDS_group, fill=PDS_group)
s_PDSgroup_n$data = subset(s_PDSgroup_n$data, !is.na(PDS_group))
s_Tgroup_n = s_agegroup_n +
aes(x=T_group, fill=T_group)
s_Tgroup_n$data = subset(s_Tgroup_n$data, !is.na(T_group))
# Get borders of each group
ddply(dat, .(Gender, PDS_group), summarize, min=min(PDS, na.rm=T), max=max(PDS, na.rm=T))
ddply(dat, .(Gender, T_group), summarize, min=min(meanT, na.rm=T), max=max(meanT, na.rm=T))
# Save plots
ggsave(file.path(plot_dir, paste0(model_class, "s_agegroup_n.eps")), s_agegroup_n, width=5, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "s_PDSgroup_n.eps")), s_PDSgroup_n, width=5, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "s_Tgroup_n.eps")), s_Tgroup_n, width=5, height=2)
```
```{r Basic Performance (adjust to task switches, 2-trial reward history) - No age analyses}
# Adjust to task switches
dat = ddply(ACC_subj, .(sID, Gender, age_group, T_group, PDS_group, model_name, trialsinceswitch), summarize, ACC=mean(ACC))
(a_trialsinceswitch_ACC =
ggplot(dat, aes(trialsinceswitch, 100 * ACC)) +
geom_vline(xintercept = 0, linetype = "dotted") +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0) +
stat_summary(fun.data = mean_se, geom = "line") +
coord_cartesian(x = c(-3, 7), y = c(0, 100)) +
scale_x_continuous(breaks = seq(-3, 7, 2)) +
labs(x = "Trials since switch", y = "% correct") +
facet_grid(~ model_name))
s_trialsinceswitch_ACC_agegroup = a_trialsinceswitch_ACC +
aes(color=age_group) +
scale_color_manual(values=colors_3and1) +
theme(legend.position="none")
s_trialsinceswitch_ACC_Tgroup = a_trialsinceswitch_ACC +
aes(color=T_group) +
scale_color_manual(values=colors_3and1)
s_trialsinceswitch_ACC_PDSgroup = a_trialsinceswitch_ACC +
aes(color=T_group) +
scale_color_manual(values=colors_3and1)
# 2-trial reward history
dat = ddply(reward_subj, .(sID, Gender, age_group, T_group, PDS_group, model_name, outcome_21_back), summarize, stay=mean(stay))
(a_outcome12back_stay =
ggplot(dat, aes(outcome_21_back, 100 * stay)) +
stat_summary(fun.data = mean_se, geom = "bar", position = "dodge") +
stat_summary(fun.data = mean_se, geom = "errorbar", position = position_dodge(0.9), width=0) + # or "pointrante" with size=.02
theme(axis.text.x = element_text(angle = -30, hjust = 0.2)) +
coord_cartesian(ylim = c(0, 100)) +
labs(x = "Previous outcomes", y = "% stay", fill = "Age") +
facet_grid( ~ model_name))
s_outcome12back_stay_agegroup = a_outcome12back_stay +
aes(fill=age_group) +
scale_fill_manual(values=colors_3and1)
s_outcome12back_stay_Tgroup = a_outcome12back_stay +
aes(fill=T_group) +
scale_fill_manual(values=colors_3and1)
s_outcome12back_stay_Tgroup$data = subset(s_outcome12back_stay_Tgroup$data, !is.na(T_group))
s_outcome12back_stay_PDSgroup = a_outcome12back_stay +
aes(fill=PDS_group) +
scale_fill_manual(values=colors_3and1)
s_outcome12back_stay_PDSgroup$data = subset(s_outcome12back_stay_PDSgroup$data, !is.na(PDS_group))
# Save plots
ggsave(file.path(plot_dir, paste0(model_class, "a_trialsinceswitch_ACC.eps")), a_trialsinceswitch_ACC, width=5 + l, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "a_outcome12back_stay.eps")), a_outcome12back_stay, width=3 + l/2, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "a_trialsinceswitch_ACC.png")), a_trialsinceswitch_ACC, width=5 + l, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "a_outcome12back_stay.png")), a_outcome12back_stay, width=3 + l/2, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "s_trialsinceswitch_ACC_agegroup.eps")), s_trialsinceswitch_ACC_agegroup, width=5, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "s_trialsinceswitch_ACC_Tgroup.eps")), s_trialsinceswitch_ACC_agegroup, width=5, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "s_trialsinceswitch_ACC_PDSgroup.eps")), s_trialsinceswitch_ACC_agegroup, width=5, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "s_outcome12back_stay_agegroup.eps")), s_outcome12back_stay_agegroup, width=5, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "s_outcome12back_stay_Tgroup.eps")), s_outcome12back_stay_agegroup, width=5, height=2)
ggsave(file.path(plot_dir, paste0(model_class, "s_outcome12back_stay_PDSgroup.eps")), s_outcome12back_stay_agegroup, width=5, height=2)
```
```{r Basic measures - Age differences and Model validation}
# Correct over age - age bins
(c_age_ACC_validation =
ggplot(ACCs, aes(age_group, 100 * mean_ACC, group=model_name, color=model_name, linetype=model_name!="Human")) + #
stat_summary() +
scale_color_manual(values=colors_validation) +
stat_summary(geom='line'))
s_PDS_ACC_validation = c_age_ACC_validation +
aes(x=PDS_group)
s_PDS_ACC_validation$data = subset(s_PDS_ACC_validation$data, !is.na(PDS_group))
s_T_ACC_validation = c_age_ACC_validation +
aes(x=T_group)
s_T_ACC_validation$data = subset(s_T_ACC_validation$data, !is.na(T_group))
# Correct over age - continuous age
(b_age_ACC =
ggplot(ACCs, aes(x=PreciseYrs, y=100 * mean_ACC, color=Gender, group=1)) +
geom_point(size=0.3) +
geom_smooth(method="lm", formula=y ~ x + I(x^2), color="black", size=0.5, alpha=0.3) +
facet_wrap(~ model_name))
b_PDS_ACC = b_age_ACC +
aes(x=PDS)
b_PDS_ACC$data = subset(b_PDS_ACC$data, !is.na(PDS) & PreciseYrs < 18) # exclude adults
b_T_ACC = b_age_ACC +
aes(x=meanT)
b_T_ACC$data = subset(b_T_ACC$data, !is.na(meanT) & PreciseYrs < 18) # exclude adults
# Points over age - age bins
(c_age_points_validation = c_age_ACC_validation + aes(y=120 * mean_reward))
s_PDS_points_validation = c_age_points_validation +
aes(x=PDS_group)
s_PDS_points_validation$data = subset(s_PDS_points_validation$data, !is.na(PDS_group))
s_T_points_validation = c_age_points_validation +
aes(x=T_group)
s_T_points_validation$data = subset(s_T_points_validation$data, !is.na(T_group))
# Points over age - continuous age
(b_age_points = b_age_ACC + aes(y=120*mean_reward))
b_PDS_points = b_age_points +
aes(x=PDS)
b_PDS_points$data = subset(b_PDS_points$data, !is.na(PDS) & PreciseYrs < 18) # exclude adults
b_T_points = b_age_points +
aes(x=meanT)
b_T_points$data = subset(b_T_points$data, !is.na(meanT) & PreciseYrs < 18) # exclude adults
# N_switches over age - age bins
(c_age_nswitch_validation = c_age_ACC_validation + aes(y=n_switches))
s_PDS_nswitch_validation = c_age_nswitch_validation +
aes(x=PDS_group)
s_PDS_nswitch_validation$data = subset(s_PDS_nswitch_validation$data, !is.na(PDS_group))
s_T_nswitch_validation = c_age_nswitch_validation +
aes(x=T_group)
s_T_nswitch_validation$data = subset(s_T_nswitch_validation$data, !is.na(T_group))
# N_switches over age - continuous age
(b_age_nswitch = b_age_ACC + aes(y=120*mean_reward))
b_PDS_nswitch = b_age_nswitch +