library(remotes) #load external (Github) packages
library(tidyverse) #for core packages e.g. tibble, ggplot, dyplr
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.4     ✓ purrr   0.3.4
## ✓ tibble  3.1.2     ✓ dplyr   1.0.6
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(janitor) #for cleaning names
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(dplyr) #for data manipulation
library(plotrix) #for calculating SE
library(ggplot2) #for creating plots
library(gt) #for creating tables
library(readspss) #for reading .sav SPSS files

all_data <- read.sav("Humiston & Wamsley 2019 data.sav")

cleandata <- all_data %>% 
  filter(exclude=="no")
explore_3a_male <- cleandata %>%
  filter(General_1_Sex == "Male") %>%
  select(baseIATcued, baseIATuncued, preIATcued, preIATuncued, postIATcued, postIATuncued, weekIATcued, weekIATuncued) %>%
  summarise_all(list(mean = mean, sd = sd, se = std.error))

explore_3a_female <- cleandata %>%
  filter(General_1_Sex == "Female") %>%
  select(baseIATcued, baseIATuncued, preIATcued, preIATuncued, postIATcued, postIATuncued, weekIATcued, weekIATuncued) %>%
  summarise_all(list(mean = mean, sd = sd, se = std.error))

df_explore_3a <- tibble(
    condition = c(rep("cued", 8), rep("uncued", 8)),
    time = rep(c("Baseline", "Prenap", "Postnap", "1-week"),4),
    gender = rep(c(rep("Male",4), rep("Female", 4)),2),
    bias_av = c(explore_3a_male$baseIATcued_mean,
                explore_3a_male$preIATcued_mean,
                explore_3a_male$postIATcued_mean, 
                explore_3a_male$weekIATcued_mean,
                explore_3a_female$baseIATcued_mean,
                explore_3a_female$preIATcued_mean,
                explore_3a_female$postIATcued_mean,
                explore_3a_female$weekIATcued_mean,
                explore_3a_male$baseIATuncued_mean,
                explore_3a_male$preIATuncued_mean,
                explore_3a_male$postIATuncued_mean,
                explore_3a_male$weekIATuncued_mean,
                explore_3a_female$baseIATuncued_mean,
                explore_3a_female$preIATuncued_mean,
                explore_3a_female$postIATuncued_mean,
                explore_3a_female$weekIATuncued_mean))

df_explore_3a_2 <- df_explore_3a %>%
  mutate(gender_cue = case_when(
    gender == "Female" & condition == "cued" ~ "cued_Female",
    gender == "Female" & condition == "uncued" ~ "uncued_Female",
    gender == "Male" & condition == "cued" ~ "cued_Male",
    gender == "Male" & condition == "uncued" ~ "uncued_Male"))


ggplot(df_explore_3a_2,
       aes(x = factor
             (time, level = c("Baseline", "Prenap", "Postnap", "1-week")),
           y = bias_av,
           colour = gender,
           group = gender_cue,
           lty = condition
           ))+
  geom_line()+
  scale_colour_brewer(palette = "Set2") + 
  labs(x = "", 
       y = "D600 Bias Score", 
       caption = "Fig 6. Male and female average D600 scores at each IAT timepoint") +
  theme_classic()