---
title: "Main Analysis"
author: "Lakens"
date: "20 juli 2018"
output: html_document
---

```{r global_options, echo=FALSE, warning=FALSE, message=FALSE, include=FALSE}
#This code will check if ggplot2 and reshape2 are installed. 
#If not, this code will install these packages.
if(!require(ggplot2)){install.packages('ggplot2')}
library(ggplot2)
if(!require(reshape2)){install.packages('reshape2')}
library(reshape2)
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE, include=TRUE)
```

# Introduction

Here, we analyze a simple dataset of a Stroop experiment. Students in an introduction to psychology course completed an online Stroop task (http://faculty.washington.edu/chudler/java/ready.html) and named the colors in congruent trials (e.g., the word 'red' written in a red font) and in incongruent trials (e.g., the word 'red' written in a green font). The time they took to name all words was self-reported in seconds (e.g., 21.3 seconds) for both the congruent and incongruent blocks. In this analysis, we are interested in examining whether there is a Stroop effect. 

```{r}
# #run only once to download the data
# stroop_data <- read.table("https://raw.githubusercontent.com/Lakens/Stroop/master/stroop.txt", sep = "\t", header = TRUE)
# 
# write.table(stroop_data, file = "stroop.csv", quote=F, row.names=F)

stroop_data <- read.csv("stroop.csv", sep = " ", header = TRUE)
```

# Plotting the data

When we plot the reaction times, we can visually see that there are no extreme outliers. It is also clear that response times are generally faster for the congruent trials, compared to the incongruent trials. 

```{r, fig.width=4, fig.heigth=4, dpi=150}
#Draw scatterplot

# Main scatterplot
ggplot(stroop_data, aes(x = Congruent, y = Incongruent)) + 
  geom_point(alpha = 0.35) +
  theme_bw(base_size = 14) +
  geom_smooth(method = 'lm', formula = y~x, color = 'black') + 
  coord_fixed() + 
  ylab("Incongruent") + xlab("Congruent") +
  theme(plot.margin = unit(c(0, 0, 0.5, 0.5), "lines"))
```

```{r}
#Perform the dependent t-test. Store results a ttest_result.

ttest_result <- t.test(stroop_data$Incongruent, 
                       stroop_data$Congruent, 
                       alternative = "two.sided", 
                       paired = TRUE, 
                       var.equal = TRUE, 
                       conf.level = 0.95)

#The code below reproduces the ESCI spreadsheet for paired observations by Cumming (2012).
#store lotal sample size (based on the length of the vector storing congruent RT's)
N <- length(stroop_data$Congruent)
#Calculate the differences between congruent and incongruent trials
diff <- stroop_data$Incongruent - stroop_data$Congruent
#calculate the pooled standard deviation for the d_av effect size recommended by Cumming. 
s_av <- sqrt((sd(stroop_data$Congruent)^2 + sd(stroop_data$Incongruent)^2) / 2) 
#Calculate Cohen's d following Cumming, 2012
d_av <- mean(diff) / s_av
#Calculate Hedges'g, or d_unbiased. Note this is approximation of the correction for Hedges'g.
d_unb <- (1 - (3 / (4 * (N - 1) - 1))) * d_av 

```

# Results

The mean reaction time (in seconds) of participants in the Congruent condition (*M* = `r round(mean(stroop_data$Congruent), digits = 2)`, *SD* = `r round(sd(stroop_data$Congruent), digits = 2)`)  was lower than the mean of participants in the Incongruent condition (*M* = `r round(mean(stroop_data$Incongruent), digits = 2)`, *SD* = `r round(sd(stroop_data$Incongruent), digits = 2)`, *r* = `r round(cor(stroop_data$Congruent, stroop_data$Incongruent), digits = 2)`). A dependent *t*-test indicated that based on our preregistered alpha level of 0.01 we could reject the null-hypothesis, *t*(`r round(ttest_result$parameter, digits=2)`) = `r round(ttest_result$statistic, digits=2)`, *p* `r ifelse(ttest_result$p.value > 0.001," = ", " < ")` `r ifelse(ttest_result$p.value > 0.001, formatC(round(ttest_result$p.value, digits=3), digits=3, format="f"), "0.001")`. As we can expect from the Stroop effect, the standardized effect size is very large, Hedges' *g~av~* = `r round(d_unb, digits=2)`. The congruency effect is very clear when we plot the data from the two groups.

```{r, echo=FALSE, fig.width=4, fig.heigth=4, dpi=150}
#Data to long format
stroop_data_long <- melt(stroop_data, id.vars = "PPNR", measure.vars = c("Congruent", "Incongruent"), variable.name = "congruency", value.name = "reactiontimes")

ggplot(stroop_data_long, aes(x = congruency, y = reactiontimes)) +
  geom_boxplot() + 
  geom_jitter(alpha = 0.25, width = 0.2) +
  ylab("Reaction Times")  + xlab("Condition") + theme_bw(base_size = 14) + 
  theme(panel.grid.major.x = element_blank())
```

