Studyphase

## 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, expName, age, transition, task, emotionality) %>%
  summarise(acc = mean(resp.corr), rt = median(resp.rt)) %>%
  ungroup()
## `summarise()` has grouped output by 'participant', 'expName', '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 8] (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 ...
##  $ expName     : Factor w/ 4 levels "V1","V1_RSI_150_UR",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ 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 8] (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 ...
##  $ expName     : Factor w/ 4 levels "V1","V1_RSI_150_UR",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ 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."

Testphase

# libraries
library(tidyverse) 

## prepare workspace
cat('\014') # clear console
rm(list=ls()) # clear workspace


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

str(myfiles2)
## 'data.frame':    2664 obs. of  43 variables:
##  $ ï..stimulus              : chr  "DEDUCTION" "PLANT" "AGE" "EXERCISE" ...
##  $ condition                : chr  "new" "S2" "R2" "R1" ...
##  $ transition               : chr  "new" "switch" "repeat" "repeat" ...
##  $ task                     : chr  "new" "word" "word" "picture" ...
##  $ corr_respTS              : chr  "new" "c" "x" "m" ...
##  $ category                 : chr  "x" "c" "x" "x" ...
##  $ attention                : chr  "new" "target" "target" "distractor" ...
##  $ old_new                  : chr  "new" "old" "old" "old" ...
##  $ corr_resp                : chr  "n" "v" "v" "v" ...
##  $ emotionality             : chr  "0" "0" "0" "0" ...
##  $ phase                    : chr  "practice" "practice" "practice" "practice" ...
##  $ practWord_loop.thisRepN  : int  0 0 0 0 NA NA NA NA NA NA ...
##  $ practWord_loop.thisTrialN: int  0 1 2 3 NA NA NA NA NA NA ...
##  $ practWord_loop.thisN     : int  0 1 2 3 NA NA NA NA NA NA ...
##  $ practWord_loop.thisIndex : int  3 1 2 0 NA NA NA NA NA NA ...
##  $ practPict_loop.thisRepN  : int  NA NA NA NA 0 0 0 0 NA NA ...
##  $ practPict_loop.thisTrialN: int  NA NA NA NA 0 1 2 3 NA NA ...
##  $ practPict_loop.thisN     : int  NA NA NA NA 0 1 2 3 NA NA ...
##  $ practPict_loop.thisIndex : int  NA NA NA NA 3 1 0 2 NA NA ...
##  $ Word_loop.thisRepN       : int  NA NA NA NA NA NA NA NA 0 0 ...
##  $ Word_loop.thisTrialN     : int  NA NA NA NA NA NA NA NA 0 1 ...
##  $ Word_loop.thisN          : int  NA NA NA NA NA NA NA NA 0 1 ...
##  $ Word_loop.thisIndex      : int  NA NA NA NA NA NA NA NA 80 98 ...
##  $ Pict_loop.thisRepN       : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ Pict_loop.thisTrialN     : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ Pict_loop.thisN          : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ Pict_loop.thisIndex      : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ old_new_word_resp.keys   : chr  "n" "n" "n" "v" ...
##  $ old_new_word_resp.corr   : int  1 0 0 1 NA NA NA NA 1 1 ...
##  $ old_new_word_resp.rt     : num  2.93 4.2 1.71 5.56 NA ...
##  $ r_k_word_resp.keys       : chr  "None" "None" "None" "2" ...
##  $ r_k_word_resp.rt         : num  NA NA NA 5.02 NA ...
##  $ old_new_pict_resp.keys   : chr  "" "" "" "" ...
##  $ old_new_pict_resp.corr   : int  NA NA NA NA 1 1 1 1 NA NA ...
##  $ old_new_pict_resp.rt     : num  NA NA NA NA 3.33 ...
##  $ r_k_pict_resp.keys       : chr  "" "" "" "" ...
##  $ r_k_pict_resp.rt         : num  NA NA NA NA NA ...
##  $ participant              : int  102 102 102 102 102 102 102 102 102 102 ...
##  $ date                     : chr  "2021_Dec_06_1316" "2021_Dec_06_1316" "2021_Dec_06_1316" "2021_Dec_06_1316" ...
##  $ expName                  : chr  "V2_rec" "V2_rec" "V2_rec" "V2_rec" ...
##  $ 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 ...
#### Rows ####
unique(myfiles2$phase)
## [1] "practice" "exp"
# relevant rows
# exclude practice trials and warmup trials
myfiles3 <- myfiles2[myfiles2$phase != 'practice', ] 

myfiles3$phase <- factor(myfiles3$phase) # refactor data frame (in order to drop cleared levels completely)
unique(myfiles3$phase) # now there won't be a error message when renaming levels
## [1] exp
## Levels: exp
## remove columns with all NAs
not_all_na <- function(x) any(!is.na(x))
myfiles4 <- myfiles3 %>% select(where(not_all_na))

## when you want remove columns with only some na's
# not_any_na <- function(x) all(!is.na(x))
# myfiles5 <- myfiles3 %>%
#   select(where(not_any_na))



## relevant columns
colnames(myfiles4)
##  [1] "ï..stimulus"            "condition"              "transition"            
##  [4] "task"                   "corr_respTS"            "category"              
##  [7] "attention"              "old_new"                "corr_resp"             
## [10] "emotionality"           "phase"                  "Word_loop.thisRepN"    
## [13] "Word_loop.thisTrialN"   "Word_loop.thisN"        "Word_loop.thisIndex"   
## [16] "Pict_loop.thisRepN"     "Pict_loop.thisTrialN"   "Pict_loop.thisN"       
## [19] "Pict_loop.thisIndex"    "old_new_word_resp.keys" "old_new_word_resp.corr"
## [22] "old_new_word_resp.rt"   "r_k_word_resp.keys"     "r_k_word_resp.rt"      
## [25] "old_new_pict_resp.keys" "old_new_pict_resp.corr" "old_new_pict_resp.rt"  
## [28] "r_k_pict_resp.keys"     "r_k_pict_resp.rt"       "participant"           
## [31] "date"                   "expName"                "psychopyVersion"       
## [34] "frameRate"
keep <- c("participant","expName", "phase", "ï..stimulus",
          "condition", "transition", "task", "emotionality","category", "attention",
          "old_new_word_resp.corr", 
          "old_new_pict_resp.corr", 
          "r_k_word_resp.keys",
          "r_k_pict_resp.keys")

data_raw <- subset(myfiles4, select = keep)
colnames(data_raw)
##  [1] "participant"            "expName"                "phase"                 
##  [4] "ï..stimulus"            "condition"              "transition"            
##  [7] "task"                   "emotionality"           "category"              
## [10] "attention"              "old_new_word_resp.corr" "old_new_pict_resp.corr"
## [13] "r_k_word_resp.keys"     "r_k_pict_resp.keys"
# rename column 
colnames(data_raw)[colnames(data_raw)=="ï..stimulus"] <- "stimulus"
colnames(data_raw)
##  [1] "participant"            "expName"                "phase"                 
##  [4] "stimulus"               "condition"              "transition"            
##  [7] "task"                   "emotionality"           "category"              
## [10] "attention"              "old_new_word_resp.corr" "old_new_pict_resp.corr"
## [13] "r_k_word_resp.keys"     "r_k_pict_resp.keys"
str(data_raw)
## 'data.frame':    2592 obs. of  14 variables:
##  $ participant           : int  102 102 102 102 102 102 102 102 102 102 ...
##  $ expName               : chr  "V2_rec" "V2_rec" "V2_rec" "V2_rec" ...
##  $ phase                 : Factor w/ 1 level "exp": 1 1 1 1 1 1 1 1 1 1 ...
##  $ stimulus              : chr  "APPROACH" "MISERY" "MOBILITY" "ALCOHOLIC" ...
##  $ condition             : chr  "S1" "new" "S1" "R1" ...
##  $ transition            : chr  "switch" "new" "switch" "repeat" ...
##  $ task                  : chr  "picture" "new" "picture" "picture" ...
##  $ emotionality          : chr  "neutral" "negative" "neutral" "negative" ...
##  $ category              : chr  "x" "x" "x" "c" ...
##  $ attention             : chr  "distractor" "new" "distractor" "distractor" ...
##  $ old_new_word_resp.corr: int  1 1 0 1 1 1 1 1 0 1 ...
##  $ old_new_pict_resp.corr: int  NA NA NA NA NA NA NA NA NA NA ...
##  $ r_k_word_resp.keys    : chr  "2" "None" "None" "1" ...
##  $ r_k_pict_resp.keys    : chr  "" "" "" "" ...
data_raw$participant <- factor(data_raw$participant)
data_raw$expName <- factor(data_raw$expName)
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$category <- factor(data_raw$category)
data_raw$attention <- factor(data_raw$attention)

str(data_raw)
## 'data.frame':    2592 obs. of  14 variables:
##  $ participant           : Factor w/ 9 levels "2","3","4","102",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ expName               : Factor w/ 3 levels "V1_recog","V2_rec",..: 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 ...
##  $ stimulus              : chr  "APPROACH" "MISERY" "MOBILITY" "ALCOHOLIC" ...
##  $ condition             : Factor w/ 5 levels "new","R1","R2",..: 4 1 4 2 4 2 4 1 5 2 ...
##  $ transition            : Factor w/ 3 levels "new","repeat",..: 3 1 3 2 3 2 3 1 3 2 ...
##  $ task                  : Factor w/ 3 levels "new","picture",..: 2 1 2 2 2 2 2 1 3 2 ...
##  $ emotionality          : Factor w/ 3 levels "0","negative",..: 3 2 3 2 3 2 3 2 3 2 ...
##  $ category              : Factor w/ 4 levels "c","m","n","x": 4 4 4 1 1 4 1 1 4 4 ...
##  $ attention             : Factor w/ 3 levels "distractor","new",..: 1 2 1 1 1 1 1 2 3 1 ...
##  $ old_new_word_resp.corr: int  1 1 0 1 1 1 1 1 0 1 ...
##  $ old_new_pict_resp.corr: int  NA NA NA NA NA NA NA NA NA NA ...
##  $ r_k_word_resp.keys    : chr  "2" "None" "None" "1" ...
##  $ r_k_pict_resp.keys    : chr  "" "" "" "" ...
# replace None with 0
data_raw[data_raw==""]<-NA

# combine accuracy of wordlist and picturelist to ACC column
colnames(data_raw)
##  [1] "participant"            "expName"                "phase"                 
##  [4] "stimulus"               "condition"              "transition"            
##  [7] "task"                   "emotionality"           "category"              
## [10] "attention"              "old_new_word_resp.corr" "old_new_pict_resp.corr"
## [13] "r_k_word_resp.keys"     "r_k_pict_resp.keys"
testphase <- data_raw %>%
  mutate(ACC = coalesce(old_new_word_resp.corr, old_new_pict_resp.corr)) 

testphase2 <- testphase %>%
  mutate(RK = coalesce(r_k_word_resp.keys, r_k_pict_resp.keys)) 

# create new Column "running" indicating which Block was running (word or picture)
testphase2$running <- 99

# when character in column "stimulus" starts with "Bilder" write "picture" to column "running"
for (i in 1:length(testphase2$running)) {
  if(startsWith(testphase2$stimulus[i],"Bilder")) {testphase2$running[i] <- "picture"}
  else{testphase2$running[i] <- "word"}
}

testphase2$running <- factor(testphase2$running)

## relevant columns
colnames(testphase2)
##  [1] "participant"            "expName"                "phase"                 
##  [4] "stimulus"               "condition"              "transition"            
##  [7] "task"                   "emotionality"           "category"              
## [10] "attention"              "old_new_word_resp.corr" "old_new_pict_resp.corr"
## [13] "r_k_word_resp.keys"     "r_k_pict_resp.keys"     "ACC"                   
## [16] "RK"                     "running"
keep <- c("participant","expName", "stimulus", "running",
          "condition", "transition", "task", "emotionality","category", "attention",
          "ACC", "RK")

testphase3 <- subset(testphase2, select = keep)
colnames(testphase3)
##  [1] "participant"  "expName"      "stimulus"     "running"      "condition"   
##  [6] "transition"   "task"         "emotionality" "category"     "attention"   
## [11] "ACC"          "RK"
# aggregate to means of condiitons
testphase_long <- testphase3 %>%
  group_by(participant, expName, running, condition, attention, transition, emotionality) %>%
  summarise(acc = mean(ACC), items = length(ACC)) %>%
  ungroup()
## `summarise()` has grouped output by 'participant', 'expName', 'running', 'condition', 'attention', 'transition'. You can override using the `.groups` argument.
judgements <- testphase3[testphase3$condition != "new",] %>% # only old items
  group_by(participant, attention, transition, emotionality, running, RK) %>%
  summarise(count_judgements = length(RK)) %>%
  complete(RK) %>%
  ungroup()
## `summarise()` has grouped output by 'participant', 'attention', 'transition', 'emotionality', 'running'. You can override using the `.groups` argument.
# "None" = "incorrect" response = miss

judgements <- judgements %>%
  spread(key = RK, value = count_judgements)


# replace NAs with 0
unique(judgements$`1`)
##  [1]  4  1 NA  7  5  9  2 11  3  6  8 10
judgements$`1`[is.na(judgements$`1`)] <- 0
unique(judgements$`1`)
##  [1]  4  1  0  7  5  9  2 11  3  6  8 10
unique(judgements$`2`)
##  [1] NA  3  1  4 10  2  9  8  6  7  5 12 11
judgements$`2`[is.na(judgements$`2`)] <- 0
unique(judgements$`2`)
##  [1]  0  3  1  4 10  2  9  8  6  7  5 12 11
# calculate proportion of remember and know responses for hits
judgements$p_remember <- 1/(12/judgements$`1`) # we have 12 items per subject and per condition
judgements$p_know <- 1/(12/judgements$`2`) # we have 12 items per subject and per condition
# proportion remember responses + proportion know responses = hit rate

# merge two dataframes
str(testphase_long)
## tibble [171 x 9] (S3: tbl_df/tbl/data.frame)
##  $ participant : Factor w/ 9 levels "2","3","4","102",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ expName     : Factor w/ 3 levels "V1_recog","V2_rec",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ running     : Factor w/ 2 levels "picture","word": 1 1 1 1 1 1 1 1 1 2 ...
##  $ condition   : Factor w/ 5 levels "new","R1","R2",..: 1 2 2 3 3 4 4 5 5 1 ...
##  $ attention   : Factor w/ 3 levels "distractor","new",..: 2 3 3 1 1 3 3 1 1 2 ...
##  $ transition  : Factor w/ 3 levels "new","repeat",..: 1 2 2 2 2 3 3 3 3 1 ...
##  $ emotionality: Factor w/ 3 levels "0","negative",..: 1 2 3 2 3 2 3 2 3 2 ...
##  $ acc         : num [1:171] 0.917 0.917 0.583 0.333 0.333 ...
##  $ items       : int [1:171] 48 12 12 12 12 12 12 12 12 24 ...
levels(testphase_long$transition)
## [1] "new"    "repeat" "switch"
str(judgements) 
## tibble [144 x 10] (S3: tbl_df/tbl/data.frame)
##  $ participant : Factor w/ 9 levels "2","3","4","102",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ attention   : Factor w/ 3 levels "distractor","new",..: 1 1 1 1 1 1 1 1 3 3 ...
##  $ transition  : Factor w/ 3 levels "new","repeat",..: 2 2 2 2 3 3 3 3 2 2 ...
##  $ emotionality: Factor w/ 3 levels "0","negative",..: 2 2 3 3 2 2 3 3 2 2 ...
##  $ running     : Factor w/ 2 levels "picture","word": 1 2 1 2 1 2 1 2 1 2 ...
##  $ 1           : num [1:144] 4 1 4 0 4 1 7 0 7 1 ...
##  $ 2           : num [1:144] 0 3 0 0 1 3 0 1 4 10 ...
##  $ None        : int [1:144] 8 8 8 12 7 8 5 11 1 1 ...
##  $ p_remember  : num [1:144] 0.3333 0.0833 0.3333 0 0.3333 ...
##  $ p_know      : num [1:144] 0 0.25 0 0 0.0833 ...
# dfjudgments is shorter than dftestphase_long because "new" items have been excluded
test_long <- merge(testphase_long,judgements, 
                   all = TRUE) # keep all columns and rows to not lose "new" items


# create final file
str(test_long)
## 'data.frame':    171 obs. of  14 variables:
##  $ participant : Factor w/ 9 levels "2","3","4","102",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ running     : Factor w/ 2 levels "picture","word": 1 1 1 1 1 1 1 1 1 2 ...
##  $ attention   : Factor w/ 3 levels "distractor","new",..: 1 1 1 1 2 3 3 3 3 1 ...
##  $ transition  : Factor w/ 3 levels "new","repeat",..: 2 2 3 3 1 2 2 3 3 2 ...
##  $ emotionality: Factor w/ 3 levels "0","negative",..: 2 3 2 3 1 2 3 2 3 2 ...
##  $ expName     : Factor w/ 3 levels "V1_recog","V2_rec",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ condition   : Factor w/ 5 levels "new","R1","R2",..: 3 3 5 5 1 2 2 4 4 2 ...
##  $ acc         : num  0.333 0.333 0.417 0.583 0.917 ...
##  $ items       : int  12 12 12 12 48 12 12 12 12 12 ...
##  $ 1           : num  4 4 4 7 NA 7 7 5 9 1 ...
##  $ 2           : num  0 0 1 0 NA 4 0 2 2 3 ...
##  $ None        : int  8 8 7 5 NA 1 5 5 1 8 ...
##  $ p_remember  : num  0.333 0.333 0.333 0.583 NA ...
##  $ p_know      : num  0 0 0.0833 0 NA ...
colnames(test_long)
##  [1] "participant"  "running"      "attention"    "transition"   "emotionality"
##  [6] "expName"      "condition"    "acc"          "items"        "1"           
## [11] "2"            "None"         "p_remember"   "p_know"
keep <- c("participant", "expName","running", "condition",
          "attention", "transition", "emotionality",
          "acc", "items", "p_remember", "p_know")

test_long <- subset(test_long, select = keep)
colnames(test_long)
##  [1] "participant"  "expName"      "running"      "condition"    "attention"   
##  [6] "transition"   "emotionality" "acc"          "items"        "p_remember"  
## [11] "p_know"
# save
save(test_long, file = "test_long.Rda")




#### Check data ####
library(tidyverse) 

## prepare workspace
rm(list=ls()) # clear workspace
load("test_long.Rda")
str(test_long)
## 'data.frame':    171 obs. of  11 variables:
##  $ participant : Factor w/ 9 levels "2","3","4","102",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ expName     : Factor w/ 3 levels "V1_recog","V2_rec",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ running     : Factor w/ 2 levels "picture","word": 1 1 1 1 1 1 1 1 1 2 ...
##  $ condition   : Factor w/ 5 levels "new","R1","R2",..: 3 3 5 5 1 2 2 4 4 2 ...
##  $ attention   : Factor w/ 3 levels "distractor","new",..: 1 1 1 1 2 3 3 3 3 1 ...
##  $ transition  : Factor w/ 3 levels "new","repeat",..: 2 2 3 3 1 2 2 3 3 2 ...
##  $ emotionality: Factor w/ 3 levels "0","negative",..: 2 3 2 3 1 2 3 2 3 2 ...
##  $ acc         : num  0.333 0.333 0.417 0.583 0.917 ...
##  $ items       : int  12 12 12 12 48 12 12 12 12 12 ...
##  $ p_remember  : num  0.333 0.333 0.333 0.583 NA ...
##  $ p_know      : num  0 0 0.0833 0 NA ...
load("study_long.Rda")
str(study_long)
## tibble [88 x 8] (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 ...
##  $ expName     : Factor w/ 4 levels "V1","V1_RSI_150_UR",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ 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 ...
# convert to wide format
study_wide <- study_long %>%
  pivot_wider(names_from = c(transition, task, emotionality), 
              values_from = c(acc, rt))

test_wide <- test_long %>%
  pivot_wider(names_from = c(running, condition, attention, transition, emotionality), 
              values_from = c(items, acc, p_remember, p_know))


# save as csv files
write.csv(test_wide, file = "test_wide.csv")
write.csv(study_wide, file = "study_wide.csv")


## check if the same version was run in study and test phase per subject
controlvers <- left_join(study_wide[,c("participant", "expName")], 
                         test_wide[,c("participant", "expName")], by = "participant")

controlvers$studyVers <- substr(controlvers$expName.x, start = 1, stop = 2)
controlvers$testVers <- substr(controlvers$expName.y, start = 1, stop = 2)



## identify participants with low accuracy in study phase
# 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!

exclusion <- study_long %>%
  group_by(participant) %>%
  summarise(Macc_overall = mean(acc))

test_long <- merge(test_long, exclusion, by = "participant") # merge computed columns

excl_part <- test_long[test_long$Macc_overall < 0.7, ] # extract participants with accuracy < 70%
unique(excl_part$participant) # see which participants are excluded: 102, 103
## [1] 102 103
## Levels: 2 3 4 102 103 106 107 110 113
test_long <- test_long[test_long$Macc_overall >= 0.7, ] # delete participant with error rate > 30%

test_long$participant <- factor(test_long$participant)
levels(test_long$participant) # see only included participants
## [1] "2"   "3"   "4"   "106" "107" "110" "113"
## exclude participants with low accuracy in study phase
# 102 and 103
test_long <- test_long[test_long$participant != 102 &
                         test_long$participant != 103,]


# save
save(test_long, file = "test_long.Rda")




#### Descriptives ####
library(tidyverse)

test_long[test_long$condition != "new",] %>%
  group_by(running, attention, transition, emotionality) %>%
  summarise(Macc = mean(acc))
## `summarise()` has grouped output by 'running', 'attention', 'transition'. You can override using the `.groups` argument.
## # A tibble: 16 x 5
## # Groups:   running, attention, transition [8]
##    running attention  transition emotionality   Macc
##    <fct>   <fct>      <fct>      <fct>         <dbl>
##  1 picture distractor repeat     negative     0.321 
##  2 picture distractor repeat     neutral      0.345 
##  3 picture distractor switch     negative     0.417 
##  4 picture distractor switch     neutral      0.452 
##  5 picture target     repeat     negative     0.75  
##  6 picture target     repeat     neutral      0.690 
##  7 picture target     switch     negative     0.726 
##  8 picture target     switch     neutral      0.762 
##  9 word    distractor repeat     negative     0.310 
## 10 word    distractor repeat     neutral      0.0238
## 11 word    distractor switch     negative     0.333 
## 12 word    distractor switch     neutral      0.119 
## 13 word    target     repeat     negative     0.881 
## 14 word    target     repeat     neutral      0.833 
## 15 word    target     switch     negative     0.869 
## 16 word    target     switch     neutral      0.726
se <- function(x) sqrt(var(x)/length(x)) ## function for standard error

re <- test_long[test_long$condition != "new",] %>%
  group_by(running, attention, transition, emotionality) %>%
  summarise(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 'running', 'attention', 'transition'. You can override using the `.groups` argument.
# plot recognition performance
p3 <- ggplot(re, aes(x=emotionality:attention, y= m_acc, fill=transition)) +
  facet_grid(running ~.) +
  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 = "Test phase", 
       x="Emotionality:Attention", 
       y = "Hits", 
       fill = "Transition") +
  theme_classic(base_size = 16)

p3

ggsave(p3, filename = "test_plot.png", height = 10, width = 15, bg = "transparent")




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

rm(list=ls()) 
load("test_long.Rda")
str(test_long)
## 'data.frame':    133 obs. of  12 variables:
##  $ participant : Factor w/ 7 levels "2","3","4","106",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ expName     : Factor w/ 3 levels "V1_recog","V2_rec",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ running     : Factor w/ 2 levels "picture","word": 1 1 1 1 1 1 1 1 1 2 ...
##  $ condition   : Factor w/ 5 levels "new","R1","R2",..: 3 3 5 5 1 2 2 4 4 2 ...
##  $ attention   : Factor w/ 3 levels "distractor","new",..: 1 1 1 1 2 3 3 3 3 1 ...
##  $ transition  : Factor w/ 3 levels "new","repeat",..: 2 2 3 3 1 2 2 3 3 2 ...
##  $ emotionality: Factor w/ 3 levels "0","negative",..: 2 3 2 3 1 2 3 2 3 2 ...
##  $ acc         : num  0.667 0.333 0.417 0.25 0.854 ...
##  $ items       : int  12 12 12 12 48 12 12 12 12 12 ...
##  $ p_remember  : num  0.3333 0.1667 0.1667 0.0833 NA ...
##  $ p_know      : num  0.333 0.167 0.25 0.167 NA ...
##  $ Macc_overall: num  0.917 0.917 0.917 0.917 0.917 ...
# ANOVA on accuracy: 
aov_acc <- ezANOVA(
  test_long[test_long$condition != "new",]
  , dv = .(acc)
  , wid = .(participant)
  , within = .(running, attention, transition, emotionality)
  , type = 3
  , detailed = TRUE
)
## Warning: You have removed one or more levels from variable "attention".
## Refactoring for ANOVA.
## Warning: You have removed one or more levels from variable "transition".
## Refactoring for ANOVA.
## Warning: You have removed one or more levels from variable "emotionality".
## Refactoring for ANOVA.
anova_out(aov_acc) # print results
## $`--- ANOVA RESULTS     ------------------------------------`
##                                       Effect         MSE df1 df2      F     p
## 1                                (Intercept) 0.085317460   1   6 375.70 0.000
## 2                                    running 0.068700397   1   6   0.87 0.388
## 3                                  attention 0.009259259   1   6 724.83 0.000
## 4                                 transition 0.013020833   1   6   2.10 0.197
## 5                               emotionality 0.019593254   1   6   9.57 0.021
## 6                          running:attention 0.018353175   1   6  30.49 0.001
## 7                         running:transition 0.018229167   1   6   1.50 0.267
## 8                       attention:transition 0.019634590   1   6   3.44 0.113
## 9                       running:emotionality 0.007771164   1   6  29.69 0.002
## 10                    attention:emotionality 0.010664683   1   6   2.10 0.198
## 11                   transition:emotionality 0.030960648   1   6   0.10 0.765
## 12              running:attention:transition 0.004918981   1   6   0.62 0.462
## 13            running:attention:emotionality 0.008349868   1   6   8.09 0.029
## 14           running:transition:emotionality 0.025586971   1   6   0.29 0.608
## 15         attention:transition:emotionality 0.013599537   1   6   0.22 0.653
## 16 running:attention:transition:emotionality 0.007812500   1   6   3.50 0.111
##    petasq getasq
## 1    0.98   0.94
## 2    0.13   0.03
## 3    0.99   0.76
## 4    0.26   0.01
## 5    0.61   0.08
## 6    0.84   0.20
## 7    0.20   0.01
## 8    0.36   0.03
## 9    0.83   0.10
## 10   0.26   0.01
## 11   0.02   0.00
## 12   0.09   0.00
## 13   0.57   0.03
## 14   0.05   0.00
## 15   0.04   0.00
## 16   0.37   0.01
## 
## $`--- SPHERICITY TESTS  ------------------------------------`
## [1] "N/A"
## 
## $`--- FORMATTED RESULTS ------------------------------------`
##                                       Effect
## 1                                (Intercept)
## 2                                    running
## 3                                  attention
## 4                                 transition
## 5                               emotionality
## 6                          running:attention
## 7                         running:transition
## 8                       attention:transition
## 9                       running:emotionality
## 10                    attention:emotionality
## 11                   transition:emotionality
## 12              running:attention:transition
## 13            running:attention:emotionality
## 14           running:transition:emotionality
## 15         attention:transition:emotionality
## 16 running:attention:transition:emotionality
##                                     Text
## 1  F(1, 6) = 375.70, p < .001, np2 = .98
## 2  F(1, 6) =   0.87, p = .388, np2 = .13
## 3  F(1, 6) = 724.83, p < .001, np2 = .99
## 4  F(1, 6) =   2.10, p = .197, np2 = .26
## 5  F(1, 6) =   9.57, p = .021, np2 = .61
## 6  F(1, 6) =  30.49, p = .001, np2 = .84
## 7  F(1, 6) =   1.50, p = .267, np2 = .20
## 8  F(1, 6) =   3.44, p = .113, np2 = .36
## 9  F(1, 6) =  29.69, p = .002, np2 = .83
## 10 F(1, 6) =   2.10, p = .198, np2 = .26
## 11 F(1, 6) =   0.10, p = .765, np2 = .02
## 12 F(1, 6) =   0.62, p = .462, np2 = .09
## 13 F(1, 6) =   8.09, p = .029, np2 = .57
## 14 F(1, 6) =   0.29, p = .608, np2 = .05
## 15 F(1, 6) =   0.22, p = .653, np2 = .04
## 16 F(1, 6) =   3.50, p = .111, np2 = .37
## 
## $`NOTE:`
## [1] "Reporting unadjusted p-values."
# Running = stimulus type