---
title             : "Reanalysis of Borota et al. (2014)"
author            : "Frederik Aust"
date              : "`r format(Sys.time(), '%d %B, %Y')`"

output: html_notebook
---

```{r init, include = FALSE}
library("readxl")
library("dplyr")
library("ggplot2")

library("BayesFactor")
```

```{r load-data}
borota_data <- read_excel("Caffeine_raw_data.xlsx", n_max = 20) %>%
  rbind(read_excel("Caffeine_raw_data.xlsx", skip = 23, n_max = 25))

nrow(borota_data)
```

```{r calculate-memory-indices}
# Fix for cells containing 0 and 1 (Murdock &  Ogilvie,  1968)
n_trials <- 64

borota_data$c_hit <- ifelse(
  borota_data$TargetOld == 1
  , 1 - 0.5 / n_trials
  , borota_data$TargetOld
)

borota_data$c_hit <- ifelse(
  borota_data$TargetOld == 0
  , 0.5 / n_trials
  , borota_data$c_hit
)

borota_data$c_fa <- ifelse(
  borota_data$FoilOld == 1
  , 1 - 0.5 / n_trials
  , borota_data$FoilOld
)

borota_data$c_fa <- ifelse(
  borota_data$FoilOld == 0
  , 0.5 / n_trials
  , borota_data$c_fa
)

borota_data$dprime <- with(borota_data, qnorm(c_hit) - qnorm(c_fa))
borota_data$ldi <- with(borota_data, LureSimilar - FoilSimilar)
```


```{r t-tests}
t.test(ldi ~ Group, data = borota_data, var.equal = TRUE)
t.test(dprime ~ Group, data = borota_data, var.equal = TRUE)

t.test(ldi ~ Group, data = borota_data)
t.test(dprime ~ Group, data = borota_data)

ttestBF(subset(borota_data, Group == 1)$ldi, subset(borota_data, Group == 2)$ldi, rscale = 0.5)
ttestBF(subset(borota_data, Group == 1)$dprime, subset(borota_data, Group == 2)$dprime, rscale = 0.5)
```


```{r}
borota_data %>% 
  mutate(Group = factor(Group, labels = c("Caffeine", "Placebo"))) %>% 
  ggplot(aes(x = Group, y = ldi)) +
  stat_summary(fun.data = mean_se, geom = "bar", fill = "white", color = "black") +
  geom_point(alpha = 0.5, shape = 21, fill = "white") +
  stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2) +
  ylim(c(-0.2, 0.8))
```