## libraries
library(tidyverse) 
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.5     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.0.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## prepare workspace
rm(list=ls()) 


## import data
# "full.names=T" ist wie ein passport, der path wird auch angehängt
temp = list.files(pattern="*.csv", path = "./data_ts", full.names = T)
myfiles = lapply(temp, read.csv)
myfiles2 = bind_rows(myfiles)

# bei Participant 3 sind zwei cols mehr drin: "text_end.started" und "text_end.stopped"
# muss raus!

myfiles3 = subset(myfiles2, select=-c(text_end.started, text_end.stopped))

# jetzt noch die ueberschüssige zeile raus (row 121)
myfiles4 <- myfiles3[!apply(is.na(myfiles3) | myfiles3 == "", 1, all),]

str(myfiles4)
## 'data.frame':    1320 obs. of  54 variables:
##  $ ï..word                 : chr  "STONE" "EXERCISE" "TASTE" "BRICK" ...
##  $ picture                 : chr  "Bilder/16_NTraining.bmp" "Bilder/10_MTraining.bmp" "Bilder/2_MTraining.bmp" "Bilder/17_NTraining.bmp" ...
##  $ condition               : chr  "S1" "R1" "S2" "R2" ...
##  $ transition              : chr  "switch" "repeat" "switch" "repeat" ...
##  $ task                    : chr  "picture" "picture" "word" "word" ...
##  $ corr_answ               : chr  "n" "m" "x" "c" ...
##  $ pos_xy                  : chr  "[-0.35, 0.25]" "[0.35, 0.25]" "[0.35, -0.25]" "[-0.35, -0.25]" ...
##  $ trialnr                 : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ phase                   : chr  "practice" "practice" "practice" "practice" ...
##  $ BlockOrder              : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ list                    : chr  "" "" "" "" ...
##  $ practice_loop.thisRepN  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ practice_loop.thisTrialN: int  0 1 2 3 4 5 6 7 8 9 ...
##  $ practice_loop.thisN     : int  0 1 2 3 4 5 6 7 8 9 ...
##  $ practice_loop.thisIndex : int  0 1 2 3 4 5 6 7 8 9 ...
##  $ warmup_loop.thisRepN    : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ warmup_loop.thisTrialN  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ warmup_loop.thisN       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ warmup_loop.thisIndex   : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ exp_loop.thisRepN       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ exp_loop.thisTrialN     : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ exp_loop.thisN          : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ exp_loop.thisIndex      : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ image_display.started   : num  203 205 207 211 213 ...
##  $ image_display.stopped   : chr  "None" "None" "None" "None" ...
##  $ word_display.started    : num  203 205 207 211 213 ...
##  $ word_display.stopped    : chr  "None" "None" "None" "None" ...
##  $ resp_pract.keys         : chr  "n" "m" "c" "c" ...
##  $ resp_pract.corr         : int  1 1 0 1 1 1 1 1 0 1 ...
##  $ resp_pract.rt           : num  1.4 1.34 3.33 1.04 1.99 ...
##  $ text_feedback.started   : num  205 207 211 213 215 ...
##  $ text_feedback.stopped   : chr  "None" "None" "None" "None" ...
##  $ word                    : chr  "STONE" "EXERCISE" "TASTE" "BRICK" ...
##  $ word_corr               : chr  "" "" "" "" ...
##  $ emotionality            : chr  "" "" "" "" ...
##  $ picture.1               : chr  "Bilder/16_NTraining.bmp" "Bilder/10_MTraining.bmp" "Bilder/2_MTraining.bmp" "Bilder/17_NTraining.bmp" ...
##  $ pict_corr               : chr  "" "" "" "" ...
##  $ corr_resp               : chr  "" "" "" "" ...
##  $ img_disp_exp.started    : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ img_disp_exp.stopped    : chr  "" "" "" "" ...
##  $ word_disp_exp.started   : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ word_disp_exp.stopped   : chr  "" "" "" "" ...
##  $ resp.keys               : chr  "" "" "" "" ...
##  $ resp.corr               : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ resp.rt                 : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ resp.started            : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ resp.stopped            : chr  "" "" "" "" ...
##  $ participant             : int  101 101 101 101 101 101 101 101 101 101 ...
##  $ age                     : int  19 19 19 19 19 19 19 19 19 19 ...
##  $ date                    : chr  "2021_Dec_06_1148" "2021_Dec_06_1148" "2021_Dec_06_1148" "2021_Dec_06_1148" ...
##  $ expName                 : chr  "V1_RSI_150_UR" "V1_RSI_150_UR" "V1_RSI_150_UR" "V1_RSI_150_UR" ...
##  $ psychopyVersion         : chr  "2021.2.3" "2021.2.3" "2021.2.3" "2021.2.3" ...
##  $ frameRate               : num  59.9 59.9 59.9 59.9 59.9 ...
##  $ X                       : logi  NA NA NA NA NA NA ...
## relevant columns
colnames(myfiles4)
##  [1] "ï..word"                  "picture"                 
##  [3] "condition"                "transition"              
##  [5] "task"                     "corr_answ"               
##  [7] "pos_xy"                   "trialnr"                 
##  [9] "phase"                    "BlockOrder"              
## [11] "list"                     "practice_loop.thisRepN"  
## [13] "practice_loop.thisTrialN" "practice_loop.thisN"     
## [15] "practice_loop.thisIndex"  "warmup_loop.thisRepN"    
## [17] "warmup_loop.thisTrialN"   "warmup_loop.thisN"       
## [19] "warmup_loop.thisIndex"    "exp_loop.thisRepN"       
## [21] "exp_loop.thisTrialN"      "exp_loop.thisN"          
## [23] "exp_loop.thisIndex"       "image_display.started"   
## [25] "image_display.stopped"    "word_display.started"    
## [27] "word_display.stopped"     "resp_pract.keys"         
## [29] "resp_pract.corr"          "resp_pract.rt"           
## [31] "text_feedback.started"    "text_feedback.stopped"   
## [33] "word"                     "word_corr"               
## [35] "emotionality"             "picture.1"               
## [37] "pict_corr"                "corr_resp"               
## [39] "img_disp_exp.started"     "img_disp_exp.stopped"    
## [41] "word_disp_exp.started"    "word_disp_exp.stopped"   
## [43] "resp.keys"                "resp.corr"               
## [45] "resp.rt"                  "resp.started"            
## [47] "resp.stopped"             "participant"             
## [49] "age"                      "date"                    
## [51] "expName"                  "psychopyVersion"         
## [53] "frameRate"                "X"
keep <- c("participant", "age", "expName", "phase", "BlockOrder", 
          "condition", "transition", "task", "emotionality","pos_xy", "list",
          "word", "word_corr", "picture", "pict_corr", "corr_resp",
          "resp.corr", "resp.rt")

data_raw <- subset(myfiles4, select = keep)
colnames(data_raw)
##  [1] "participant"  "age"          "expName"      "phase"        "BlockOrder"  
##  [6] "condition"    "transition"   "task"         "emotionality" "pos_xy"      
## [11] "list"         "word"         "word_corr"    "picture"      "pict_corr"   
## [16] "corr_resp"    "resp.corr"    "resp.rt"
#### Rows ####
unique(data_raw$phase)
## [1] "practice" "warmup"   "exp"
# relevant rows
# exclude practice trials and warmup trials
data_raw <- data_raw[data_raw$phase != 'practice' &
                       data_raw$phase != 'warmup', ] 

data_raw$phase <- factor(data_raw$phase) # refactor data frame (in order to drop cleared levels completely)
unique(data_raw$phase) # now there won't be a error message when renaming levels
## [1] exp
## Levels: exp
str(data_raw)
## 'data.frame':    1056 obs. of  18 variables:
##  $ participant : int  101 101 101 101 101 101 101 101 101 101 ...
##  $ age         : int  19 19 19 19 19 19 19 19 19 19 ...
##  $ expName     : chr  "V1_RSI_150_UR" "V1_RSI_150_UR" "V1_RSI_150_UR" "V1_RSI_150_UR" ...
##  $ phase       : Factor w/ 1 level "exp": 1 1 1 1 1 1 1 1 1 1 ...
##  $ BlockOrder  : int  1 2 3 4 1 2 3 4 1 2 ...
##  $ condition   : chr  "S1" "R1" "S2" "R2" ...
##  $ transition  : chr  "switch" "repeat" "switch" "repeat" ...
##  $ task        : chr  "picture" "picture" "word" "word" ...
##  $ emotionality: chr  "neutral" "neutral" "negative" "negative" ...
##  $ pos_xy      : chr  "[-0.35, 0.25]" "[0.35, 0.25]" "[0.35, -0.25]" "[-0.35, -0.25]" ...
##  $ list        : chr  "v1_S1.xlsx" "v1_R1.xlsx" "v1_S2.xlsx" "v1_R2.xlsx" ...
##  $ word        : chr  "JOURNAL" "SLEEP" "NAZI" "SNAKE" ...
##  $ word_corr   : chr  "c" "x" "c" "c" ...
##  $ picture     : chr  "Bilder/205_N.bmp" "Bilder/111_M.bmp" "Bilder/35_M.bmp" "Bilder/280_N.bmp" ...
##  $ pict_corr   : chr  "n" "m" "m" "n" ...
##  $ corr_resp   : chr  "n" "m" "c" "c" ...
##  $ resp.corr   : int  1 1 1 1 1 1 1 0 1 1 ...
##  $ resp.rt     : num  1.212 0.924 2.646 1.754 1.242 ...
data_raw$participant <- factor(data_raw$participant)
data_raw$expName <- factor(data_raw$expName)
data_raw$BlockOrder <- factor(data_raw$BlockOrder)
data_raw$condition <- factor(data_raw$condition)
data_raw$transition <- factor(data_raw$transition)
data_raw$task <- factor(data_raw$task)
data_raw$emotionality <- factor(data_raw$emotionality)
data_raw$pos_xy <- factor(data_raw$pos_xy)
data_raw$list <- factor(data_raw$list)
data_raw$word <- factor(data_raw$word)
data_raw$picture <- factor(data_raw$picture)

str(data_raw)
## 'data.frame':    1056 obs. of  18 variables:
##  $ participant : Factor w/ 11 levels "2","3","4","101",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ age         : int  19 19 19 19 19 19 19 19 19 19 ...
##  $ expName     : Factor w/ 4 levels "V1","V1_RSI_150_UR",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ phase       : Factor w/ 1 level "exp": 1 1 1 1 1 1 1 1 1 1 ...
##  $ BlockOrder  : Factor w/ 4 levels "1","2","3","4": 1 2 3 4 1 2 3 4 1 2 ...
##  $ condition   : Factor w/ 4 levels "R1","R2","S1",..: 3 1 4 2 3 1 4 2 3 1 ...
##  $ transition  : Factor w/ 2 levels "repeat","switch": 2 1 2 1 2 1 2 1 2 1 ...
##  $ task        : Factor w/ 2 levels "picture","word": 1 1 2 2 1 1 2 2 1 1 ...
##  $ emotionality: Factor w/ 2 levels "negative","neutral": 2 2 1 1 1 2 2 1 1 2 ...
##  $ pos_xy      : Factor w/ 4 levels "[-0.35, -0.25]",..: 2 4 3 1 2 4 3 1 2 4 ...
##  $ list        : Factor w/ 12 levels "v1_R1.xlsx","v1_R2.xlsx",..: 3 1 4 2 3 1 4 2 3 1 ...
##  $ word        : Factor w/ 96 levels "ALCOHOLIC","ANGER",..: 48 78 63 79 87 31 37 77 20 12 ...
##  $ word_corr   : chr  "c" "x" "c" "c" ...
##  $ picture     : Factor w/ 96 levels "Bilder/1_M.bmp",..: 57 8 88 84 34 64 87 22 4 65 ...
##  $ pict_corr   : chr  "n" "m" "m" "n" ...
##  $ corr_resp   : chr  "n" "m" "c" "c" ...
##  $ resp.corr   : int  1 1 1 1 1 1 1 0 1 1 ...
##  $ resp.rt     : num  1.212 0.924 2.646 1.754 1.242 ...
# convert s to ms in column resp.rt
data_raw$resp.rt <- data_raw$resp.rt * 1000

study_long <- data_raw %>%
  group_by(participant, age, transition, task, emotionality) %>%
  summarise(acc = mean(resp.corr), rt = median(resp.rt)) %>%
  ungroup()
## `summarise()` has grouped output by 'participant', 'age', 'transition', 'task'. You can override using the `.groups` argument.
# save
save(study_long, file = "study_long.Rda")




#### Descriptives ####
rm(list=ls()) 
load("study_long.Rda")
str(study_long)
## tibble [88 x 7] (S3: tbl_df/tbl/data.frame)
##  $ participant : Factor w/ 11 levels "2","3","4","101",..: 1 1 1 1 1 1 1 1 2 2 ...
##  $ age         : int [1:88] 20 20 20 20 20 20 20 20 19 19 ...
##  $ transition  : Factor w/ 2 levels "repeat","switch": 1 1 1 1 2 2 2 2 1 1 ...
##  $ task        : Factor w/ 2 levels "picture","word": 1 1 2 2 1 1 2 2 1 1 ...
##  $ emotionality: Factor w/ 2 levels "negative","neutral": 1 2 1 2 1 2 1 2 1 2 ...
##  $ acc         : num [1:88] 0.917 1 0.75 0.583 1 ...
##  $ rt          : num [1:88] 1115 973 1235 1173 2266 ...
# overall accuracy rates per participant
study_long%>%
  group_by(participant) %>%
  summarise(Macc_overall = mean(acc))
## # A tibble: 11 x 2
##    participant Macc_overall
##    <fct>              <dbl>
##  1 2                  0.844
##  2 3                  0.958
##  3 4                  0.896
##  4 101                0.844
##  5 102                0.698
##  6 103                0.646
##  7 105                0.927
##  8 106                0.917
##  9 107                0.958
## 10 110                0.969
## 11 113                0.865
# subjects 102 and 103 have too low accuracy rates!

study_long %>%
  group_by(transition, task, emotionality) %>%
  summarise(Macc = mean(acc), Mrt = mean(rt))
## `summarise()` has grouped output by 'transition', 'task'. You can override using the `.groups` argument.
## # A tibble: 8 x 5
## # Groups:   transition, task [4]
##   transition task    emotionality  Macc   Mrt
##   <fct>      <fct>   <fct>        <dbl> <dbl>
## 1 repeat     picture negative     0.947 1052.
## 2 repeat     picture neutral      0.917 1105.
## 3 repeat     word    negative     0.795 1344.
## 4 repeat     word    neutral      0.856 1248.
## 5 switch     picture negative     0.917 1674.
## 6 switch     picture neutral      0.917 1641.
## 7 switch     word    negative     0.712 1914.
## 8 switch     word    neutral      0.864 2003.
se <- function(x) sqrt(var(x)/length(x)) ## function for standard error

ts <- study_long %>%
  group_by(transition, task, emotionality) %>%
  summarise(m_rt = mean(rt), 
            sd_rt = round(sd(rt), digits = 0), 
            se_rt = round(se(rt), digits = 0),
            m_acc = mean(acc), 
            sd_acc = round(sd(acc), digits = 3), 
            se_acc = round(se(acc), digits = 3),
            n = length(acc))
## `summarise()` has grouped output by 'transition', 'task'. You can override using the `.groups` argument.
# plot accuracy
p1 <- ggplot(ts, aes(x=task:emotionality, y= m_acc, fill=transition)) +
  geom_bar(stat = "identity", position=position_dodge(), color = "black") +
  geom_errorbar(aes(ymin=m_acc-se_acc, ymax=m_acc+se_acc), width=.2, position = position_dodge(.9)) +
  scale_y_continuous(expand = c(0,0), 
                     limits = c(0,1.1)) +
  scale_fill_manual(values = c("#56B4E9", "#0072B2")) +
  labs(title = "Study phase", 
       x="Task", 
       y = "Accuracy rate", 
       fill = "Transition") +
  theme_classic(base_size = 16)

p1

ggsave(p1, filename = "study_plot_acc.png", height = 10, width = 15, bg = "transparent")


# plot response times
p2 <- ggplot(ts, aes(x=task:emotionality, y= m_rt, fill=transition)) +
  geom_bar(stat = "identity", position=position_dodge(), color = "black") +
  geom_errorbar(aes(ymin=m_rt-se_rt, ymax=m_rt+se_rt), width=.2, position = position_dodge(.9)) +
  scale_y_continuous(breaks = c(0,100,200,300,400,500,600,700,800,900,1000,
                                1100,1200,1300,1400,1500,1600,1700,1800,1900,
                                2000,2100,2200,2300,2400,2500,2600, 2700), 
                     expand = c(0,0), 
                     limits = c(0,2600)) +
  scale_fill_manual(values = c("#56B4E9", "#0072B2")) +
  labs(title = "Study phase", 
       x="Task", 
       y = "Response times in ms", 
       fill = "Transition") +
  theme_classic(base_size = 16)

p2

ggsave(p2, filename = "study_plot_rt.png", height = 10, width = 15, bg = "transparent")




### ANOVAs #### 
library(ez)
library(schoRsch)
library(apa)

rm(list=ls()) 
load("study_long.Rda")
str(study_long)
## tibble [88 x 7] (S3: tbl_df/tbl/data.frame)
##  $ participant : Factor w/ 11 levels "2","3","4","101",..: 1 1 1 1 1 1 1 1 2 2 ...
##  $ age         : int [1:88] 20 20 20 20 20 20 20 20 19 19 ...
##  $ transition  : Factor w/ 2 levels "repeat","switch": 1 1 1 1 2 2 2 2 1 1 ...
##  $ task        : Factor w/ 2 levels "picture","word": 1 1 2 2 1 1 2 2 1 1 ...
##  $ emotionality: Factor w/ 2 levels "negative","neutral": 1 2 1 2 1 2 1 2 1 2 ...
##  $ acc         : num [1:88] 0.917 1 0.75 0.583 1 ...
##  $ rt          : num [1:88] 1115 973 1235 1173 2266 ...
# ANOVA on accuracy: 
aov_acc <- ezANOVA(
  study_long
  , dv = .(acc)
  , wid = .(participant)
  , within = .(transition, task, emotionality)
  , type = 3
  , detailed = TRUE
)
anova_out(aov_acc) # print results
## $`--- ANOVA RESULTS     ------------------------------------`
##                         Effect         MSE df1 df2      F     p petasq getasq
## 1                  (Intercept) 0.090009470   1  10 732.42 0.000   0.99   0.97
## 2                   transition 0.009043561   1  10   1.71 0.220   0.15   0.01
## 3                         task 0.083727904   1  10   3.62 0.086   0.27   0.13
## 4                 emotionality 0.003267045   1  10  13.91 0.004   0.58   0.02
## 5              transition:task 0.004403409   1  10   0.65 0.441   0.06   0.00
## 6      transition:emotionality 0.004056187   1  10   4.98 0.050   0.33   0.01
## 7            task:emotionality 0.004940025   1  10  16.36 0.002   0.62   0.04
## 8 transition:task:emotionality 0.003835227   1  10   1.32 0.278   0.12   0.00
## 
## $`--- SPHERICITY TESTS  ------------------------------------`
## [1] "N/A"
## 
## $`--- FORMATTED RESULTS ------------------------------------`
##                         Effect                                   Text
## 1                  (Intercept) F(1, 10) = 732.42, p < .001, np2 = .99
## 2                   transition F(1, 10) =   1.71, p = .220, np2 = .15
## 3                         task F(1, 10) =   3.62, p = .086, np2 = .27
## 4                 emotionality F(1, 10) =  13.91, p = .004, np2 = .58
## 5              transition:task F(1, 10) =   0.65, p = .441, np2 = .06
## 6      transition:emotionality F(1, 10) =   4.98, p = .050, np2 = .33
## 7            task:emotionality F(1, 10) =  16.36, p = .002, np2 = .62
## 8 transition:task:emotionality F(1, 10) =   1.32, p = .278, np2 = .12
## 
## $`NOTE:`
## [1] "Reporting unadjusted p-values."
# ANOVA on response times: 
aov_acc <- ezANOVA(
  study_long
  , dv = .(rt)
  , wid = .(participant)
  , within = .(transition, task, emotionality)
  , type = 3
  , detailed = TRUE
)
anova_out(aov_acc) # print results
## $`--- ANOVA RESULTS     ------------------------------------`
##                         Effect        MSE df1 df2      F     p petasq getasq
## 1                  (Intercept) 1172649.01   1  10 168.33 0.000   0.94   0.92
## 2                   transition  270550.72   1  10  31.37 0.000   0.76   0.32
## 3                         task   82121.23   1  10  18.06 0.002   0.64   0.08
## 4                 emotionality   29162.99   1  10   0.01 0.931   0.00   0.00
## 5              transition:task   87181.90   1  10   0.44 0.524   0.04   0.00
## 6      transition:emotionality   48697.81   1  10   0.27 0.614   0.03   0.00
## 7            task:emotionality   70245.38   1  10   0.01 0.908   0.00   0.00
## 8 transition:task:emotionality   48282.21   1  10   2.09 0.178   0.17   0.01
## 
## $`--- SPHERICITY TESTS  ------------------------------------`
## [1] "N/A"
## 
## $`--- FORMATTED RESULTS ------------------------------------`
##                         Effect                                   Text
## 1                  (Intercept) F(1, 10) = 168.33, p < .001, np2 = .94
## 2                   transition F(1, 10) =  31.37, p < .001, np2 = .76
## 3                         task F(1, 10) =  18.06, p = .002, np2 = .64
## 4                 emotionality F(1, 10) =   0.01, p = .931, np2 < .01
## 5              transition:task F(1, 10) =   0.44, p = .524, np2 = .04
## 6      transition:emotionality F(1, 10) =   0.27, p = .614, np2 = .03
## 7            task:emotionality F(1, 10) =   0.01, p = .908, np2 < .01
## 8 transition:task:emotionality F(1, 10) =   2.09, p = .178, np2 = .17
## 
## $`NOTE:`
## [1] "Reporting unadjusted p-values."