---
title: "R Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*. 


```{r}
# clean slate
rm(list=ls())
cat("\014")
```

```{r}
##### install and load necessary packages

# installs packages if necessary

if(!"emmeans" %in% rownames(installed.packages())) install.packages("emmeans")
if(!"tidyverse" %in% rownames(installed.packages())) install.packages("tidyverse")
if(!"splithalf" %in% rownames(installed.packages())) devtools::install_github("sdparsons/splithalf")
if(!"pwr" %in% rownames(installed.packages())) install.packages("pwr")
if(!"dplyr" %in% rownames(installed.packages())) install.packages("dplyr")
if(!"tidyr" %in% rownames(installed.packages())) install.packages("tidyr")
if(!"psych" %in% rownames(installed.packages())) install.packages("psych")
if(!"rstudioapi" %in% rownames(installed.packages())) install.packages("rstudioapi")


library("splithalf")   # internal consistency
library("psych")       # test-retest
library("tidyverse")
library("dplyr")
library("cvcqv")       # coefficient of variation
library("ggpubr")
library("pwr")
library("tidyr")
library("emmeans")
library("rstudioapi") 
```

```{r}
########## Set working directory and load data 

pathwd<-sub("/AffFlexReliability_DataAnalysis_SingleTrials_R1.Rmd","",system("find . -type f -name AffFlexReliability_DataAnalysis_SingleTrials_R1.Rmd",intern=T)[1]) 
setwd(pathwd)
getwd()
```

```{r}
### 1. Read in Single Trial Data sheet
df_affect_raw = read.csv("AffFlexReliability_SingleTrials.csv", dec = ".", sep = ",", header = T)
```

```{r}
#### assure that relevant variables are treated as factors

df_affect_raw$task <- factor(df_affect_raw$task, levels = c("emotion","gender"))
df_affect_raw$switch_only <- factor(df_affect_raw$switch_only, levels = c("switch","baseline"))
df_affect_raw$time <- factor(df_affect_raw$time, levels = c("test", "retest"))
df_affect_raw$task_switch <- factor(df_affect_raw$task_switch, levels = c("switch_TO_emotion", "baseline_emotion", "switch_TO_gender", "baseline_gender"))
df_affect_raw$is_error <- !as.logical(df_affect_raw$is_hit)
df_affect_raw$Valence_before <- factor(df_affect_raw$Valence_before, levels = c("angry", "happy"))
df_affect_raw$emotion <- factor(df_affect_raw$emotion, levels = c("angry", "happy"))
df_affect_raw$gender <- factor(df_affect_raw$gender, levels = c("male", "female"))
df_affect_raw$is_out <- !as.logical(df_affect_raw$is_in)
```

```{r}
##### create new variable combining information from repeat/switch, emotion/gender, valence_before and current valence
df_affect_raw$cond_all <- paste(df_affect_raw$switch_only, df_affect_raw$task, df_affect_raw$Valence_before, 
                                df_affect_raw$emotion,sep="_")
```

```{r}
session1 <- df_affect_raw[df_affect_raw$time == "test",] 
session2 <- df_affect_raw[df_affect_raw$time == "retest",]
```

```{r}
######## create subset of data for RT analyses
# filtering for only correct trials within the normative RT ranges and excluding errors
df_affect_raw_RT <- df_affect_raw[df_affect_raw$is_hit == "True" & df_affect_raw$is_in == "True",] 
session1_RT <- session1[session1$is_hit == "True" & session1$is_in == "True",] 
session2_RT <- session2[session2$is_hit == "True" & session2$is_in == "True",] 
```

```{r}
### aggregate data to create summary data averaged over trials (for Table 1)
meanRT <- aggregate(response_time~subj_idx+task+switch_only+time+task_switch,df_affect_raw_RT,mean)
meanER <- aggregate(is_error~subj_idx+task+switch_only+time+task_switch,df_affect_raw,mean)
```

######### Provide more detailed information on error trials and RT removed trials

```{r}
meanER_stimulus <- aggregate(is_error~subj_idx+emotion+gender+time,df_affect_raw,mean)
meanOUT_stimulus <- aggregate(is_out~subj_idx+emotion+gender+time,df_affect_raw,mean)
```

```{r}
aggregate(is_error~emotion+gender+time,meanER_stimulus,mean)
```
```{r}
aggregate(is_error~emotion+gender+time,meanER_stimulus,sd)
```
```{r}
#### 2. Calculate ANOVA on error trails and RT removed trials to check, whether stimulus properties 
#### (i.e., gender or emotion information) had specific effects on accuracy

#### calculate repeated measures ANOVA for ERROR trials
ER_stimulus.aov <- aov( is_error~(time*emotion*gender)+Error(subj_idx/(time*emotion*gender)), meanER_stimulus)
summary(ER_stimulus.aov)
```

```{r}
#### post-hoc testing (pairwise comparisons)
ER_stimulus.emm <- emmeans(ER_stimulus.aov, ~ time * emotion * gender)
pairs(ER_stimulus.emm, simple = "each", combine = TRUE, adjust = "mvt")
```

```{r}
###### characterize main effects

##### EMMs test vs. retest (main effect time)
emmeans(ER_stimulus.aov, pairwise ~ time)
```

```{r}
aggregate(is_out~emotion+gender+time,meanOUT_stimulus,mean)
```

```{r}
aggregate(is_out~emotion+gender+time,meanOUT_stimulus,sd)
```

```{r}
#### calculate repeated measures ANOVA for RT REMOVED trials
OUT_stimulus.aov <- aov( is_out~(time*emotion*gender)+Error(subj_idx/(time*emotion*gender)), meanOUT_stimulus)
summary(OUT_stimulus.aov)
```


#############################################################
################# 3. Valence-based analysis ####################


```{r}
df_affect_val <- df_affect_raw[df_affect_raw$Trial != 1,] #exclude first trial of each run, as valence before cannot be assigned

session1_val <- df_affect_val[df_affect_val$time == "test",] 
session2_val <- df_affect_val[df_affect_val$time == "retest",] 
```


```{r}
######## create subset of data for RT analyses
######## i.e., filtering for only correct trials within the normative RT ranges and excluding errors
df_affect_val_RT <- df_affect_val[df_affect_val$is_hit == "True" & df_affect_val$is_in == "True",] 
session1_val_RT <- session1_val[session1_val$is_hit == "True" & session1_val$is_in == "True",] 
session2_val_RT <- session2_val[session2_val$is_hit == "True" & session2_val$is_in == "True",] 
```

```{r}
### aggregate data to create summary data for more differentiated valence information averaged over trials
meanRT_val <- aggregate(response_time~subj_idx+cond_all+time,df_affect_val_RT,mean)
meanER_val <- aggregate(is_error~subj_idx+cond_all+time,df_affect_val,mean)
```


```{r}
##### 3.1. Aggregate data in wide format (averaged over trials but separately for both sessions) 
##### and calculation of valence-based switch costs 

meanRT_val_T1 <- aggregate(response_time~subj_idx+cond_all,session1_val_RT,mean)
meanRT_val_T1_wide <- reshape(meanRT_val_T1, idvar = "subj_idx", timevar = "cond_all", direction = "wide")

meanRT_val_T1_wide <- meanRT_val_T1_wide %>%
  rename(
    RT_Repeat_Emotion_angry_angry_test = response_time.baseline_emotion_angry_angry,
    RT_Repeat_Emotion_angry_happy_test = response_time.baseline_emotion_angry_happy,
    RT_Repeat_Emotion_happy_angry_test = response_time.baseline_emotion_happy_angry,
    RT_Repeat_Emotion_happy_happy_test = response_time.baseline_emotion_happy_happy,
    RT_Switch_Emotion_angry_angry_test = response_time.switch_emotion_angry_angry,
    RT_Switch_Emotion_angry_happy_test = response_time.switch_emotion_angry_happy,
    RT_Switch_Emotion_happy_angry_test = response_time.switch_emotion_happy_angry,
    RT_Switch_Emotion_happy_happy_test = response_time.switch_emotion_happy_happy,
    RT_Repeat_Gender_angry_angry_test = response_time.baseline_gender_angry_angry,
    RT_Repeat_Gender_angry_happy_test = response_time.baseline_gender_angry_happy,
    RT_Repeat_Gender_happy_angry_test = response_time.baseline_gender_happy_angry,
    RT_Repeat_Gender_happy_happy_test = response_time.baseline_gender_happy_happy,
    RT_Switch_Gender_angry_angry_test = response_time.switch_gender_angry_angry,
    RT_Switch_Gender_angry_happy_test = response_time.switch_gender_angry_happy,
    RT_Switch_Gender_happy_angry_test = response_time.switch_gender_happy_angry,
    RT_Switch_Gender_happy_happy_test = response_time.switch_gender_happy_happy,
    )

meanRT_val_T2 <- aggregate(response_time~subj_idx+cond_all,session2_val_RT,mean)
meanRT_val_T2_wide <- reshape(meanRT_val_T2, idvar = "subj_idx", timevar = "cond_all", direction = "wide")

meanRT_val_T2_wide <- meanRT_val_T2_wide %>%
  rename(
    RT_Repeat_Emotion_angry_angry_retest = response_time.baseline_emotion_angry_angry,
    RT_Repeat_Emotion_angry_happy_retest = response_time.baseline_emotion_angry_happy,
    RT_Repeat_Emotion_happy_angry_retest = response_time.baseline_emotion_happy_angry,
    RT_Repeat_Emotion_happy_happy_retest = response_time.baseline_emotion_happy_happy,
    RT_Switch_Emotion_angry_angry_retest = response_time.switch_emotion_angry_angry,
    RT_Switch_Emotion_angry_happy_retest = response_time.switch_emotion_angry_happy,
    RT_Switch_Emotion_happy_angry_retest = response_time.switch_emotion_happy_angry,
    RT_Switch_Emotion_happy_happy_retest = response_time.switch_emotion_happy_happy,
    RT_Repeat_Gender_angry_angry_retest = response_time.baseline_gender_angry_angry,
    RT_Repeat_Gender_angry_happy_retest = response_time.baseline_gender_angry_happy,
    RT_Repeat_Gender_happy_angry_retest = response_time.baseline_gender_happy_angry,
    RT_Repeat_Gender_happy_happy_retest = response_time.baseline_gender_happy_happy,
    RT_Switch_Gender_angry_angry_retest = response_time.switch_gender_angry_angry,
    RT_Switch_Gender_angry_happy_retest = response_time.switch_gender_angry_happy,
    RT_Switch_Gender_happy_angry_retest = response_time.switch_gender_happy_angry,
    RT_Switch_Gender_happy_happy_retest = response_time.switch_gender_happy_happy,
    )

meanRT_val_wide <- merge(meanRT_val_T1_wide,meanRT_val_T2_wide,by="subj_idx")

```

```{r}
####### calculate RT-based, valence-specific switch costs
meanRT_val_wide$RTcost_Emotion_angry_angry_test <- meanRT_val_wide$RT_Switch_Emotion_angry_angry_test -   
                                                        meanRT_val_wide$RT_Repeat_Emotion_angry_angry_test
meanRT_val_wide$RTcost_Emotion_angry_happy_test <- meanRT_val_wide$RT_Switch_Emotion_angry_happy_test -   
                                                        meanRT_val_wide$RT_Repeat_Emotion_angry_happy_test
meanRT_val_wide$RTcost_Emotion_happy_angry_test <- meanRT_val_wide$RT_Switch_Emotion_happy_angry_test -   
                                                        meanRT_val_wide$RT_Repeat_Emotion_happy_angry_test
meanRT_val_wide$RTcost_Emotion_happy_happy_test <- meanRT_val_wide$RT_Switch_Emotion_happy_happy_test -   
                                                        meanRT_val_wide$RT_Repeat_Emotion_happy_happy_test

meanRT_val_wide$RTcost_Gender_angry_angry_test <- meanRT_val_wide$RT_Switch_Gender_angry_angry_test -   
                                                        meanRT_val_wide$RT_Repeat_Gender_angry_angry_test
meanRT_val_wide$RTcost_Gender_angry_happy_test <- meanRT_val_wide$RT_Switch_Gender_angry_happy_test -   
                                                        meanRT_val_wide$RT_Repeat_Gender_angry_happy_test
meanRT_val_wide$RTcost_Gender_happy_angry_test <- meanRT_val_wide$RT_Switch_Gender_happy_angry_test -   
                                                        meanRT_val_wide$RT_Repeat_Gender_happy_angry_test
meanRT_val_wide$RTcost_Gender_happy_happy_test <- meanRT_val_wide$RT_Switch_Gender_happy_happy_test -   
                                                        meanRT_val_wide$RT_Repeat_Gender_happy_happy_test

meanRT_val_wide$RTcost_Emotion_angry_angry_retest <- meanRT_val_wide$RT_Switch_Emotion_angry_angry_retest -   
                                                        meanRT_val_wide$RT_Repeat_Emotion_angry_angry_retest
meanRT_val_wide$RTcost_Emotion_angry_happy_retest <- meanRT_val_wide$RT_Switch_Emotion_angry_happy_retest -   
                                                        meanRT_val_wide$RT_Repeat_Emotion_angry_happy_retest
meanRT_val_wide$RTcost_Emotion_happy_angry_retest <- meanRT_val_wide$RT_Switch_Emotion_happy_angry_retest -   
                                                        meanRT_val_wide$RT_Repeat_Emotion_happy_angry_retest
meanRT_val_wide$RTcost_Emotion_happy_happy_retest <- meanRT_val_wide$RT_Switch_Emotion_happy_happy_retest -   
                                                        meanRT_val_wide$RT_Repeat_Emotion_happy_happy_retest

meanRT_val_wide$RTcost_Gender_angry_angry_retest <- meanRT_val_wide$RT_Switch_Gender_angry_angry_retest -   
                                                        meanRT_val_wide$RT_Repeat_Gender_angry_angry_retest
meanRT_val_wide$RTcost_Gender_angry_happy_retest <- meanRT_val_wide$RT_Switch_Gender_angry_happy_retest -   
                                                        meanRT_val_wide$RT_Repeat_Gender_angry_happy_retest
meanRT_val_wide$RTcost_Gender_happy_angry_retest <- meanRT_val_wide$RT_Switch_Gender_happy_angry_retest -   
                                                        meanRT_val_wide$RT_Repeat_Gender_happy_angry_retest
meanRT_val_wide$RTcost_Gender_happy_happy_retest <- meanRT_val_wide$RT_Switch_Gender_happy_happy_retest -   
                                                        meanRT_val_wide$RT_Repeat_Gender_happy_happy_retest
```

```{r}
meanER_val_T1 <- aggregate(is_error~subj_idx+cond_all,session1_val,mean)
meanER_val_T1_wide <- reshape(meanER_val_T1, idvar = "subj_idx", timevar = "cond_all", direction = "wide")

meanER_val_T1_wide <- meanER_val_T1_wide %>%
  rename(
    ER_Repeat_Emotion_angry_angry_test = is_error.baseline_emotion_angry_angry,
    ER_Repeat_Emotion_angry_happy_test = is_error.baseline_emotion_angry_happy,
    ER_Repeat_Emotion_happy_angry_test = is_error.baseline_emotion_happy_angry,
    ER_Repeat_Emotion_happy_happy_test = is_error.baseline_emotion_happy_happy,
    ER_Switch_Emotion_angry_angry_test = is_error.switch_emotion_angry_angry,
    ER_Switch_Emotion_angry_happy_test = is_error.switch_emotion_angry_happy,
    ER_Switch_Emotion_happy_angry_test = is_error.switch_emotion_happy_angry,
    ER_Switch_Emotion_happy_happy_test = is_error.switch_emotion_happy_happy,
    ER_Repeat_Gender_angry_angry_test = is_error.baseline_gender_angry_angry,
    ER_Repeat_Gender_angry_happy_test = is_error.baseline_gender_angry_happy,
    ER_Repeat_Gender_happy_angry_test = is_error.baseline_gender_happy_angry,
    ER_Repeat_Gender_happy_happy_test = is_error.baseline_gender_happy_happy,
    ER_Switch_Gender_angry_angry_test = is_error.switch_gender_angry_angry,
    ER_Switch_Gender_angry_happy_test = is_error.switch_gender_angry_happy,
    ER_Switch_Gender_happy_angry_test = is_error.switch_gender_happy_angry,
    ER_Switch_Gender_happy_happy_test = is_error.switch_gender_happy_happy,
    )

meanER_val_T2 <- aggregate(is_error~subj_idx+cond_all,session2_val,mean)
meanER_val_T2_wide <- reshape(meanER_val_T2, idvar = "subj_idx", timevar = "cond_all", direction = "wide")

meanER_val_T2_wide <- meanER_val_T2_wide %>%
  rename(
    ER_Repeat_Emotion_angry_angry_retest = is_error.baseline_emotion_angry_angry,
    ER_Repeat_Emotion_angry_happy_retest = is_error.baseline_emotion_angry_happy,
    ER_Repeat_Emotion_happy_angry_retest = is_error.baseline_emotion_happy_angry,
    ER_Repeat_Emotion_happy_happy_retest = is_error.baseline_emotion_happy_happy,
    ER_Switch_Emotion_angry_angry_retest = is_error.switch_emotion_angry_angry,
    ER_Switch_Emotion_angry_happy_retest = is_error.switch_emotion_angry_happy,
    ER_Switch_Emotion_happy_angry_retest = is_error.switch_emotion_happy_angry,
    ER_Switch_Emotion_happy_happy_retest = is_error.switch_emotion_happy_happy,
    ER_Repeat_Gender_angry_angry_retest = is_error.baseline_gender_angry_angry,
    ER_Repeat_Gender_angry_happy_retest = is_error.baseline_gender_angry_happy,
    ER_Repeat_Gender_happy_angry_retest = is_error.baseline_gender_happy_angry,
    ER_Repeat_Gender_happy_happy_retest = is_error.baseline_gender_happy_happy,
    ER_Switch_Gender_angry_angry_retest = is_error.switch_gender_angry_angry,
    ER_Switch_Gender_angry_happy_retest = is_error.switch_gender_angry_happy,
    ER_Switch_Gender_happy_angry_retest = is_error.switch_gender_happy_angry,
    ER_Switch_Gender_happy_happy_retest = is_error.switch_gender_happy_happy,
    )

meanER_val_wide <- merge(meanER_val_T1_wide,meanER_val_T2_wide,by="subj_idx")

```

```{r}
###### 3.2. Calculate descriptive statistics of valence-based switch costs for table 4
####### calculate ER-based, valence-specific switch costs
meanER_val_wide$ERcost_Emotion_angry_angry_test <- meanER_val_wide$ER_Switch_Emotion_angry_angry_test -   
                                                        meanER_val_wide$ER_Repeat_Emotion_angry_angry_test
meanER_val_wide$ERcost_Emotion_angry_happy_test <- meanER_val_wide$ER_Switch_Emotion_angry_happy_test -   
                                                        meanER_val_wide$ER_Repeat_Emotion_angry_happy_test
meanER_val_wide$ERcost_Emotion_happy_angry_test <- meanER_val_wide$ER_Switch_Emotion_happy_angry_test -   
                                                        meanER_val_wide$ER_Repeat_Emotion_happy_angry_test
meanER_val_wide$ERcost_Emotion_happy_happy_test <- meanER_val_wide$ER_Switch_Emotion_happy_happy_test -   
                                                        meanER_val_wide$ER_Repeat_Emotion_happy_happy_test

meanER_val_wide$ERcost_Gender_angry_angry_test <- meanER_val_wide$ER_Switch_Gender_angry_angry_test -   
                                                        meanER_val_wide$ER_Repeat_Gender_angry_angry_test
meanER_val_wide$ERcost_Gender_angry_happy_test <- meanER_val_wide$ER_Switch_Gender_angry_happy_test -   
                                                        meanER_val_wide$ER_Repeat_Gender_angry_happy_test
meanER_val_wide$ERcost_Gender_happy_angry_test <- meanER_val_wide$ER_Switch_Gender_happy_angry_test -   
                                                        meanER_val_wide$ER_Repeat_Gender_happy_angry_test
meanER_val_wide$ERcost_Gender_happy_happy_test <- meanER_val_wide$ER_Switch_Gender_happy_happy_test -   
                                                        meanER_val_wide$ER_Repeat_Gender_happy_happy_test

meanER_val_wide$ERcost_Emotion_angry_angry_retest <- meanER_val_wide$ER_Switch_Emotion_angry_angry_retest -   
                                                        meanER_val_wide$ER_Repeat_Emotion_angry_angry_retest
meanER_val_wide$ERcost_Emotion_angry_happy_retest <- meanER_val_wide$ER_Switch_Emotion_angry_happy_retest -   
                                                        meanER_val_wide$ER_Repeat_Emotion_angry_happy_retest
meanER_val_wide$ERcost_Emotion_happy_angry_retest <- meanER_val_wide$ER_Switch_Emotion_happy_angry_retest -   
                                                        meanER_val_wide$ER_Repeat_Emotion_happy_angry_retest
meanER_val_wide$ERcost_Emotion_happy_happy_retest <- meanER_val_wide$ER_Switch_Emotion_happy_happy_retest -   
                                                        meanER_val_wide$ER_Repeat_Emotion_happy_happy_retest

meanER_val_wide$ERcost_Gender_angry_angry_retest <- meanER_val_wide$ER_Switch_Gender_angry_angry_retest -   
                                                        meanER_val_wide$ER_Repeat_Gender_angry_angry_retest
meanER_val_wide$ERcost_Gender_angry_happy_retest <- meanER_val_wide$ER_Switch_Gender_angry_happy_retest -   
                                                        meanER_val_wide$ER_Repeat_Gender_angry_happy_retest
meanER_val_wide$ERcost_Gender_happy_angry_retest <- meanER_val_wide$ER_Switch_Gender_happy_angry_retest -   
                                                        meanER_val_wide$ER_Repeat_Gender_happy_angry_retest
meanER_val_wide$ERcost_Gender_happy_happy_retest <- meanER_val_wide$ER_Switch_Gender_happy_happy_retest -   
                                                        meanER_val_wide$ER_Repeat_Gender_happy_happy_retest
```


```{r}
##### subset switch cost variables to show means and standard deviations of RT and ER switch costs averaged over participants (for Table 4)
Switchcosts_val_RT <-  select(meanRT_val_wide   ,subj_idx
                                                ,RTcost_Gender_angry_angry_test
                                                ,RTcost_Gender_angry_happy_test
                                                ,RTcost_Gender_happy_angry_test
                                                ,RTcost_Gender_happy_happy_test
                                                ,RTcost_Emotion_angry_angry_test
                                                ,RTcost_Emotion_angry_happy_test
                                                ,RTcost_Emotion_happy_angry_test
                                                ,RTcost_Emotion_happy_happy_test
                                                ,RTcost_Gender_angry_angry_retest
                                                ,RTcost_Gender_angry_happy_retest
                                                ,RTcost_Gender_happy_angry_retest
                                                ,RTcost_Gender_happy_happy_retest
                                                ,RTcost_Emotion_angry_angry_retest
                                                ,RTcost_Emotion_angry_happy_retest
                                                ,RTcost_Emotion_happy_angry_retest
                                                ,RTcost_Emotion_happy_happy_retest)

Switchcosts_val_RT_descript <- Switchcosts_val_RT[,-1]

Switchcosts_val_ER <-  select(meanER_val_wide   ,subj_idx
                                                ,ERcost_Gender_angry_angry_test
                                                ,ERcost_Gender_angry_happy_test
                                                ,ERcost_Gender_happy_angry_test
                                                ,ERcost_Gender_happy_happy_test
                                                ,ERcost_Emotion_angry_angry_test
                                                ,ERcost_Emotion_angry_happy_test
                                                ,ERcost_Emotion_happy_angry_test
                                                ,ERcost_Emotion_happy_happy_test
                                                ,ERcost_Gender_angry_angry_retest
                                                ,ERcost_Gender_angry_happy_retest
                                                ,ERcost_Gender_happy_angry_retest
                                                ,ERcost_Gender_happy_happy_retest
                                                ,ERcost_Emotion_angry_angry_retest
                                                ,ERcost_Emotion_angry_happy_retest
                                                ,ERcost_Emotion_happy_angry_retest
                                                ,ERcost_Emotion_happy_happy_retest)

Switchcosts_val_ER_descript <- Switchcosts_val_ER[,-1]

```

```{r}
sapply(Switchcosts_val_RT_descript, mean, na.rm=TRUE)
```
```{r}
sapply(Switchcosts_val_RT_descript, sd, na.rm=TRUE)
```

```{r}
sapply(Switchcosts_val_ER_descript, mean, na.rm=TRUE)
```
```{r}
sapply(Switchcosts_val_ER_descript, sd, na.rm=TRUE)
```

```{r}
###### 3.3. Restructure valence-based switch costs in long format

Switchcosts_val_RT_long <- Switchcosts_val_RT %>% 
                            gather(cond, RTcost, c("RTcost_Gender_angry_angry_test"
                                                ,"RTcost_Gender_angry_happy_test"
                                                ,"RTcost_Gender_happy_angry_test"
                                                ,"RTcost_Gender_happy_happy_test"
                                                ,"RTcost_Emotion_angry_angry_test"
                                                ,"RTcost_Emotion_angry_happy_test"
                                                ,"RTcost_Emotion_happy_angry_test"
                                                ,"RTcost_Emotion_happy_happy_test"
                                                ,"RTcost_Gender_angry_angry_retest"
                                                ,"RTcost_Gender_angry_happy_retest"
                                                ,"RTcost_Gender_happy_angry_retest"
                                                ,"RTcost_Gender_happy_happy_retest"
                                                ,"RTcost_Emotion_angry_angry_retest"
                                                ,"RTcost_Emotion_angry_happy_retest"
                                                ,"RTcost_Emotion_happy_angry_retest"
                                                ,"RTcost_Emotion_happy_happy_retest"))

df_SwitchcostRT_val_long <- Switchcosts_val_RT_long %>% separate(cond, c("measure", "task", "val_before", "val_curr", "time"), sep = "_")

```

```{r}
Switchcosts_val_ER_long <- Switchcosts_val_ER %>% 
                            gather(cond, ERcost, c("ERcost_Gender_angry_angry_test"
                                                ,"ERcost_Gender_angry_happy_test"
                                                ,"ERcost_Gender_happy_angry_test"
                                                ,"ERcost_Gender_happy_happy_test"
                                                ,"ERcost_Emotion_angry_angry_test"
                                                ,"ERcost_Emotion_angry_happy_test"
                                                ,"ERcost_Emotion_happy_angry_test"
                                                ,"ERcost_Emotion_happy_happy_test"
                                                ,"ERcost_Gender_angry_angry_retest"
                                                ,"ERcost_Gender_angry_happy_retest"
                                                ,"ERcost_Gender_happy_angry_retest"
                                                ,"ERcost_Gender_happy_happy_retest"
                                                ,"ERcost_Emotion_angry_angry_retest"
                                                ,"ERcost_Emotion_angry_happy_retest"
                                                ,"ERcost_Emotion_happy_angry_retest"
                                                ,"ERcost_Emotion_happy_happy_retest"))

df_SwitchcostER_val_long <- Switchcosts_val_ER_long %>% separate(cond, c("measure", "task", "val_before", "val_curr", "time"), sep = "_")

```

```{r}
#### 3.4. Characterize Valence-based Switch Cost Effects (repeated measures ANOVAs on ER and RT based switch costs)

#### calculate repeated measures ANOVA for valence-specific RT switch costs
RTcost_val.aov <- aov( RTcost~(time*task*val_before*val_curr)+Error(subj_idx/(time*task*val_before*val_curr)), df_SwitchcostRT_val_long)
summary(RTcost_val.aov)
```
```{r}
#### post-hoc testing (pairwise comparisons)
RT_val.emm <- emmeans(RTcost_val.aov, ~ time * task * val_before * val_curr)
pairs(RT_val.emm, simple = "each", combine = TRUE, adjust = "mvt")
```

```{r}
###### characterize main effects

##### EMMs emotion vs. gender (main effect task)
emmeans(RTcost_val.aov, pairwise ~ time)
```

```{r}
###### characterize main effects

##### EMMs emotion vs. gender (main effect task)
emmeans(RTcost_val.aov, pairwise ~ task)
```

```{r}
#### Task x Condition interaction
emmeans(RTcost_val.aov, pairwise ~ val_curr | task)
```


```{r}
#### visualize interaction effects
emmip(RTcost_val.aov, val_before ~ val_curr | time )
```


```{r}
#### 3-way interaction
emmeans(RTcost_val.aov, pairwise ~  val_before | time * val_curr )
```

```{r}
#### 3-way interaction
emmeans(RTcost_val.aov, pairwise ~  val_curr | time * val_before )
```

```{r}
#### calculate repeated measures ANOVA for for valence-specific RT switch costs
ERcost_val.aov <- aov( ERcost~(time*task*val_before*val_curr)+Error(subj_idx/(time*task*val_before*val_curr)), df_SwitchcostER_val_long)
summary(ERcost_val.aov)
```
```{r}
###### characterize main effects

##### EMMs emotion vs. gender (main effect task)
emmeans(ERcost_val.aov, pairwise ~ time)
```
```{r}
###### characterize main effects

##### EMMs emotion vs. gender (main effect task)
emmeans(ERcost_val.aov, pairwise ~ task)
```

```{r}
##### EMMs emotion vs. gender (main effect task)
emmeans(ERcost_val.aov, pairwise ~ val_curr)
```


```{r}
session1_RT_angry_angry  <- session1_val_RT[session1_val_RT$emotion == 'angry' & 
                                            session1_val_RT$Valence_before == 'angry',]

session1_RT_angry_happy  <- session1_val_RT[session1_val_RT$emotion == 'happy' & 
                                            session1_val_RT$Valence_before == 'angry',]

session1_RT_happy_angry  <- session1_val_RT[session1_val_RT$emotion == 'angry' & 
                                            session1_val_RT$Valence_before == 'happy',]

session1_RT_happy_happy  <- session1_val_RT[session1_val_RT$emotion == 'happy' & 
                                            session1_val_RT$Valence_before == 'happy',]
```


#####################################################
############ Internal consistencies #################

####### 3.5. Calculate internal consistencies for valence specific data

########### RT #############

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for RT switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session1_RT_angry_angry,
          outcome = "RT",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.RT = "response_time" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for RT switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session1_RT_angry_happy,
          outcome = "RT",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.RT = "response_time" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for RT switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session1_RT_happy_angry,
          outcome = "RT",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.RT = "response_time" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for RT switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session1_RT_happy_happy,
          outcome = "RT",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.RT = "response_time" )

```

```{r}
session2_RT_angry_angry  <- session2_val_RT[session2_val_RT$emotion == 'angry' & 
                                            session2_val_RT$Valence_before == 'angry',]

session2_RT_angry_happy  <- session2_val_RT[session2_val_RT$emotion == 'happy' & 
                                            session2_val_RT$Valence_before == 'angry',]

session2_RT_happy_angry  <- session2_val_RT[session2_val_RT$emotion == 'angry' & 
                                            session2_val_RT$Valence_before == 'happy',]

session2_RT_happy_happy  <- session2_val_RT[session2_val_RT$emotion == 'happy' & 
                                            session2_val_RT$Valence_before == 'happy',]
```


```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for RT switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session2_RT_angry_angry,
          outcome = "RT",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.RT = "response_time" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for RT switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session2_RT_angry_happy,
          outcome = "RT",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.RT = "response_time" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for RT switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session2_RT_happy_angry,
          outcome = "RT",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.RT = "response_time" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for RT switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session2_RT_happy_happy,
          outcome = "RT",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.RT = "response_time" )

```

```{r}
session1_angry_angry  <- session1_val[session1_val$emotion == 'angry' & 
                                            session1_val$Valence_before == 'angry',]

session1_angry_happy  <- session1_val[session1_val$emotion == 'happy' & 
                                            session1_val$Valence_before == 'angry',]

session1_happy_angry  <- session1_val[session1_val$emotion == 'angry' & 
                                            session1_val$Valence_before == 'happy',]

session1_happy_happy  <- session1_val[session1_val$emotion == 'happy' & 
                                            session1_val$Valence_before == 'happy',]
```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for ER switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session1_angry_angry,
          outcome = "accuracy",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.ACC = "is_error" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for ER switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session1_angry_happy,
          outcome = "accuracy",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.ACC = "is_error" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for ER switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session1_happy_angry,
          outcome = "accuracy",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.ACC = "is_error" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for ER switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session1_happy_happy,
          outcome = "accuracy",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.ACC = "is_error" )

```

```{r}
session2_angry_angry  <- session2_val[session2_val$emotion == 'angry' & 
                                            session2_val$Valence_before == 'angry',]

session2_angry_happy  <- session2_val[session2_val$emotion == 'happy' & 
                                            session2_val$Valence_before == 'angry',]

session2_happy_angry  <- session2_val[session2_val$emotion == 'angry' & 
                                            session2_val$Valence_before == 'happy',]

session2_happy_happy  <- session2_val[session2_val$emotion == 'happy' & 
                                            session2_val$Valence_before == 'happy',]
```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for ER switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session2_angry_angry,
          outcome = "accuracy",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.ACC = "is_error" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for ER switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session2_angry_happy,
          outcome = "accuracy",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.ACC = "is_error" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for ER switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session2_happy_angry,
          outcome = "accuracy",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.ACC = "is_error" )

```

```{r permutation internal consistency, results = 'hide', eval=FALSE}
#### calculate internal consistency for ER switchcosts during the test session (for each task)

require(splithalf)
splithalf(data = session2_happy_happy,
          outcome = "accuracy",
          score = "difference",
          permutations = 5000,
          var.trialnum = "X",
          var.condition = "task",
          conditionlist = c("emotion", "gender"),
          var.compare = "switch_only",
          compare1 = "baseline",
          compare2 = "switch",
          var.participant = "subj_idx",
          var.ACC = "is_error" )

```

###########################################
########## Test-Retest Analysis ###########

####### 3.6. Calculate test-retest reliability for valence specific switch costs (RT and ER)

```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
RTcost_Emotion_angry_angry <- cbind(meanRT_val_wide$RTcost_Emotion_angry_angry_test,meanRT_val_wide$RTcost_Emotion_angry_angry_retest)
ICC.RTcost_Emotion_angry_angry <- ICC(RTcost_Emotion_angry_angry)
ICC.RTcost_Emotion_angry_angry
cor.test(meanRT_val_wide$RTcost_Emotion_angry_angry_test,meanRT_val_wide$RTcost_Emotion_angry_angry_retest) # corresponding Pearson correlation
```


```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
RTcost_Emotion_angry_happy <- cbind(meanRT_val_wide$RTcost_Emotion_angry_happy_test,meanRT_val_wide$RTcost_Emotion_angry_happy_retest)
ICC.RTcost_Emotion_angry_happy <- ICC(RTcost_Emotion_angry_happy)
ICC.RTcost_Emotion_angry_happy
cor.test(meanRT_val_wide$RTcost_Emotion_angry_happy_test,meanRT_val_wide$RTcost_Emotion_angry_happy_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
RTcost_Emotion_happy_angry <- cbind(meanRT_val_wide$RTcost_Emotion_happy_angry_test,meanRT_val_wide$RTcost_Emotion_happy_angry_retest)
ICC.RTcost_Emotion_happy_angry <- ICC(RTcost_Emotion_happy_angry)
ICC.RTcost_Emotion_happy_angry
cor.test(meanRT_val_wide$RTcost_Emotion_happy_angry_test,meanRT_val_wide$RTcost_Emotion_happy_angry_retest) # corresponding Pearson correlation
```


```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
RTcost_Emotion_happy_happy <- cbind(meanRT_val_wide$RTcost_Emotion_happy_happy_test,meanRT_val_wide$RTcost_Emotion_happy_happy_retest)
ICC.RTcost_Emotion_happy_happy <- ICC(RTcost_Emotion_happy_happy)
ICC.RTcost_Emotion_happy_happy
cor.test(meanRT_val_wide$RTcost_Emotion_happy_happy_test,meanRT_val_wide$RTcost_Emotion_happy_happy_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
RTcost_Gender_angry_angry <- cbind(meanRT_val_wide$RTcost_Gender_angry_angry_test,meanRT_val_wide$RTcost_Gender_angry_angry_retest)
ICC.RTcost_Gender_angry_angry <- ICC(RTcost_Gender_angry_angry)
ICC.RTcost_Gender_angry_angry
cor.test(meanRT_val_wide$RTcost_Gender_angry_angry_test,meanRT_val_wide$RTcost_Gender_angry_angry_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
RTcost_Gender_angry_happy <- cbind(meanRT_val_wide$RTcost_Gender_angry_happy_test,meanRT_val_wide$RTcost_Gender_angry_happy_retest)
ICC.RTcost_Gender_angry_happy <- ICC(RTcost_Gender_angry_happy)
ICC.RTcost_Gender_angry_happy
cor.test(meanRT_val_wide$RTcost_Gender_angry_happy_test,meanRT_val_wide$RTcost_Gender_angry_happy_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
RTcost_Gender_happy_angry <- cbind(meanRT_val_wide$RTcost_Gender_happy_angry_test,meanRT_val_wide$RTcost_Gender_happy_angry_retest)
ICC.RTcost_Gender_happy_angry <- ICC(RTcost_Gender_happy_angry)
ICC.RTcost_Gender_happy_angry
cor.test(meanRT_val_wide$RTcost_Gender_happy_angry_test,meanRT_val_wide$RTcost_Gender_happy_angry_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
RTcost_Gender_happy_happy <- cbind(meanRT_val_wide$RTcost_Gender_happy_happy_test,meanRT_val_wide$RTcost_Gender_happy_happy_retest)
ICC.RTcost_Gender_happy_happy <- ICC(RTcost_Gender_happy_happy)
ICC.RTcost_Gender_happy_happy
cor.test(meanRT_val_wide$RTcost_Gender_happy_happy_test,meanRT_val_wide$RTcost_Gender_happy_happy_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for RT during repeat trials in the gender task
ERcost_Emotion_angry_angry <- cbind(meanER_val_wide$ERcost_Emotion_angry_angry_test,meanER_val_wide$ERcost_Emotion_angry_angry_retest)
ICC.ERcost_Emotion_angry_angry <- ICC(ERcost_Emotion_angry_angry)
ICC.ERcost_Emotion_angry_angry
cor.test(meanER_val_wide$ERcost_Emotion_angry_angry_test,meanER_val_wide$ERcost_Emotion_angry_angry_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for ER during repeat trials in the gender task
ERcost_Emotion_angry_happy <- cbind(meanER_val_wide$ERcost_Emotion_angry_happy_test,meanER_val_wide$ERcost_Emotion_angry_happy_retest)
ICC.ERcost_Emotion_angry_happy <- ICC(ERcost_Emotion_angry_happy)
ICC.ERcost_Emotion_angry_happy
cor.test(meanER_val_wide$ERcost_Emotion_angry_happy_test,meanER_val_wide$ERcost_Emotion_angry_happy_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for ER during repeat trials in the gender task
ERcost_Emotion_happy_angry <- cbind(meanER_val_wide$ERcost_Emotion_happy_angry_test,meanER_val_wide$ERcost_Emotion_happy_angry_retest)
ICC.ERcost_Emotion_happy_angry <- ICC(ERcost_Emotion_happy_angry)
ICC.ERcost_Emotion_happy_angry
cor.test(meanER_val_wide$ERcost_Emotion_happy_angry_test,meanER_val_wide$ERcost_Emotion_happy_angry_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for ER during repeat trials in the gender task
ERcost_Emotion_happy_happy <- cbind(meanER_val_wide$ERcost_Emotion_happy_happy_test,meanER_val_wide$ERcost_Emotion_happy_happy_retest)
ICC.ERcost_Emotion_happy_happy <- ICC(ERcost_Emotion_happy_happy)
ICC.ERcost_Emotion_happy_happy
cor.test(meanER_val_wide$ERcost_Emotion_happy_happy_test,meanER_val_wide$ERcost_Emotion_happy_happy_retest) # corresponding Pearson correlation
```


```{r}
### calculate test-retest reliability for ER during repeat trials in the gender task
ERcost_Gender_angry_angry <- cbind(meanER_val_wide$ERcost_Gender_angry_angry_test,meanER_val_wide$ERcost_Gender_angry_angry_retest)
ICC.ERcost_Gender_angry_angry <- ICC(ERcost_Gender_angry_angry)
ICC.ERcost_Gender_angry_angry
cor.test(meanER_val_wide$ERcost_Gender_angry_angry_test,meanER_val_wide$ERcost_Gender_angry_angry_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for ER during repeat trials in the gender task
ERcost_Gender_angry_happy <- cbind(meanER_val_wide$ERcost_Gender_angry_happy_test,meanER_val_wide$ERcost_Gender_angry_happy_retest)
ICC.ERcost_Gender_angry_happy <- ICC(ERcost_Gender_angry_happy)
ICC.ERcost_Gender_angry_happy
cor.test(meanER_val_wide$ERcost_Gender_angry_happy_test,meanER_val_wide$ERcost_Gender_angry_happy_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for ER during repeat trials in the gender task
ERcost_Gender_happy_angry <- cbind(meanER_val_wide$ERcost_Gender_happy_angry_test,meanER_val_wide$ERcost_Gender_happy_angry_retest)
ICC.ERcost_Gender_happy_angry <- ICC(ERcost_Gender_happy_angry)
ICC.ERcost_Gender_happy_angry
cor.test(meanER_val_wide$ERcost_Gender_happy_angry_test,meanER_val_wide$ERcost_Gender_happy_angry_retest) # corresponding Pearson correlation
```

```{r}
### calculate test-retest reliability for ER during repeat trials in the gender task
ERcost_Gender_happy_happy <- cbind(meanER_val_wide$ERcost_Gender_happy_happy_test,meanER_val_wide$ERcost_Gender_happy_happy_retest)
ICC.ERcost_Gender_happy_happy <- ICC(ERcost_Gender_happy_happy)
ICC.ERcost_Gender_happy_happy
cor.test(meanER_val_wide$ERcost_Gender_happy_happy_test,meanER_val_wide$ERcost_Gender_happy_happy_retest) # corresponding Pearson correlation
```


Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file). 

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

