```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = '/work/CogSci_Methods01/portfolio_assignment_03-Ida0601/data')
```
```{r load packages, include=FALSE}
library(pacman)
p_load(tidyverse)
p_load(pastecs)
library("car")
library(data.table)
library(vroom)
```
#### Load and anonymize data ####
```{r load_and_anonymize}
if (is_empty(list.files(path = getwd(), pattern = "*SART_Anonymous*", full.names = T))){
files <- list.files(path = getwd(), pattern = "*SART*", full.names = T)
data_out <- list()
num_files <- length(files)
rand_ids <- sample(seq(1,num_files,1))
cnt_f <- 0
for (f in files){
cnt_f <- cnt_f + 1
data_out[[f]] <- read_csv(file = f, col_names = TRUE)
data_out[[f]]$ID <- paste(c("Anonymous", rand_ids[cnt_f]), collapse = "")
out_name <- paste(c(getwd(), "/SART_", unique(data_out[[f]]$ID[1]), ".txt"), collapse = "")
write_csv(data_out[[f]], out_name, na = "NA")
file.remove(f)
}
}
```
```{r}
filelist = list.files(pattern = ".*.txt")
df <- vroom(filelist)
```
# Making new dataframes
```{r}
#Making a dataframe for reaction times:
df_rt <- df %>% dplyr::filter(resp_acc == 1, block_num == 1, number != 3)
df_rt <- df_rt %>%
group_by(part_ID, Condition) %>%
summarise(mean = mean(resp_rt))
#Making a dataframe for accuracy
df_acc <- summarise(df, part_ID, block_num = 1, number = 3, resp_acc, Condition)
df_acc <- df_acc %>% group_by(part_ID, Condition) %>% summarise(resp_acc=mean(resp_acc))
```
# Checking for normality
```{r}
#Shapiro-Wilks test of normality
df_rt %>% group_by(Condition) %>%
summarise(
normality = shapiro.test(mean)$p.value
)
df_acc %>% group_by(Condition) %>%
summarise(
normality = shapiro.test(resp_acc)$p.value
)
```
# Anova tests
```{r}
#Making an ANOVA test for accuracy
anova_acc_model <- aov(resp_acc ~ Condition, data = df_acc)
summary(anova_acc_model)
```
```{r}
#Making an ANOVA test for RT
anova_model_meanRT <- aov(mean ~ Condition, data = df_rt)
summary(anova_model_meanRT)
```
# Making boxplots
```{r}
#Visualization - accuracy
df_acc %>% ggplot(aes(x= Condition, y = resp_acc, fill = Condition))+
geom_boxplot()+
theme_light()+
ggtitle("Accuracy according to condition")
# Making boxplot for mean reaction time
df_rt %>% ggplot(aes(x = Condition, y = mean, fill = Condition))+
geom_boxplot()+
theme_light()+
ggtitle("Mean reaction time according to condition")
```
# Doing post-hoc comparisons
```{r}
# Making multiple t-test for mean reaction time + accuracy between the three condtions (A, B and C)
TukeyHSD(anova_model_meanRT)
TukeyHSD(anova_acc_model)
```
# Checking assumptions
```{r}
#homogeneity test of variance
# Reaction time
leveneTest(mean ~ Condition, data = df_rt)
# Accuracy
leveneTest(resp_acc ~ Condition, data = df_acc)
```
```{r}
## Checking residuals
plot(anova_model_meanRT)
plot(anova_acc_model)
```