###########  Correlate sleep within MBO group, Sober (T1) to After Blackout (T3)  ################

########Contents #########

# 1. line 33 - 65   MBO Participants - Free recall, ACC 
# 2. line 69 - 83   MBO Participants - Serial recall, ACC
# 3. line 104 -131  MBO participants - Serial recall, Sequence length
# 4. line 135 -205  MBO participants - Depth of processing, ACC

library("car")
library(readxl)
library (psych) 
library(arm)
library (car)
library (lsr) 
library(Matrix) 
library (lme4) 
library (MASS)
library(reshape2)
library (lattice) 
library (ggplot2) 
library(sjPlot)  
library(Hmisc)
library(gridExtra) 
library(reshape)
library(tidyverse)
library(pastecs)
library(ggsci)
library(BayesFactor)

## NOTE - All data available on the corresponding experiment workbook (i.e., 'Depth_Data').  Tabs within these
# workbooks indicated by name (i.e., 'Depth_Sleep').

#####################################################################################################################
######################   Free Recall Accuracy by Sleep

# Load data.  All data numbers in % 
FreeCorr_Data <- read.csv("~/XXX/XXX/XXX/XXX.csv", header = TRUE, stringsAsFactors = FALSE)

str(FreeCorr_Data)
FreeCorr_Data$Minutes <- as.numeric(as.integer(FreeCorr_Data$Minutes))
str(FreeCorr_Data)

# Model my data
sleepmodel <- lm(formula = Diff.T1.T3 ~ Minutes, 
            data = FreeCorr_Data)

summary(sleepmodel)

baysianFree = regressionBF(Diff.T1.T3~.,data=FreeCorr_Data) 
baysianFree

####################################################################################################
################   Serial Recall Sleep Correlations  ###############################################

# Load data.  All data numbers in % 
SerialCorr_Data <- read.csv("~/XXX/XXX/XXX/XXX.csv", header = TRUE, stringsAsFactors = FALSE)
str(SerialCorr_Data)
SerialCorr_Data$Minutes <- as.numeric(as.integer(SerialCorr_Data$Minutes))
str(SerialCorr_Data)

Ser_CorrA <- SerialCorr_Data[1:23, c("Minutes", "Diff_T1T3_Ave")]
str(Ser_CorrA)

modelserial <- lm(formula = Diff_T1T3_Ave ~ Minutes, 
            data = Ser_CorrA)
summary(modelserial)
  
baysianSerial = regressionBF(Diff_T1T3_Ave~.,data=Ser_CorrA) 
baysianSerial  

####################################################################################################
#########   Serial - Sleep by Sequence Length    ##################

str(SerialCorr_Data)
Ser_CorrL <- SerialCorr_Data[1:23, c("Minutes", "Diff_T1T3_L")]
str(Ser_CorrL)

modelserialL <- lm(formula = Diff_T1T3_L ~ Minutes, 
                  data = Ser_CorrL)
summary(modelserialL)

baysianSerialL = regressionBF(Diff_T1T3_L~.,data=Ser_CorrL) 
baysianSerialL

####################################################################################################
###############    Depth Sleep Correlations  #######################################################

# Load data.  All data numbers in % 
DepthCorr_Data <- read.csv("~/XXX/XXX/XXX/XXX.csv", header = TRUE, stringsAsFactors = FALSE)
str(DepthCorr_Data)
DepthCorr_Data$Minutes <- as.numeric(as.integer(DepthCorr_Data$Minutes))
str(DepthCorr_Data)

#Subset data to enable correlations.  First by Deep Accuracy Immediate
Depth_Corr1 <- DepthCorr_Data[1:23, c("Minutes", "DeepAccImm")]
str(Depth_Corr1)

#Deep Acc Delayed
Depth_Corr2 <- DepthCorr_Data[1:23, c("Minutes", "DeepAccDel")]
str(Depth_Corr2)

#Shallow Acc Immediate
Depth_Corr2 <- DepthCorr_Data[1:23, c("Minutes", "ShalAccImm")]
str(Depth_Corr3)

#Shallow Acc Delayed
Depth_Corr4 <- DepthCorr_Data[1:23, c("Minutes", "ShalAccDel")]
str(Depth_Corr4)

# Combined data set
Depth_Melt <- melt.data.frame(DepthCorr_Data, id.vars = c("Minutes"), 
                          measure.vars = c("DeepAccImm", "DeepAccDel", "ShalAccImm", "ShalAccDel")) 
View(Depth_Melt)

names(Depth_Melt)<- c("Minutes", "Condition", "Accuracy") 

###############    Begin Modelling Correlations    ##########################################

#1. Deep Accuracy Immediate

modeldepth1 <- lm(formula = DeepAccImm ~ Minutes, 
                data = Depth_Corr1)
summary(modeldepth1)

baysianDepthDeepImm = regressionBF(DeepAccImm~.,data=Depth_Corr1) 
baysianDepthDeepImm

#2. Deep Accuracy Delayed

modeldepth2 <- lm(formula = DeepAccDel ~ Minutes, 
   data = Depth_Corr2)
summary(modeldepth2)

baysianDepthDeepDel = regressionBF(DeepAccDel~.,data=Depth_Corr2) 
baysianDepthDeepDel

#3. Shallow Acc Imm
modeldepth3 <- lm(formula = ShalAccImm ~ Minutes, 
                  data = Depth_Corr3)
summary(modeldepth3)

baysianDepthShalImm = regressionBF(ShalAccImm~.,data=Depth_Corr3) 
baysianDepthShalImm

#4. Shallow Acc Delayed
modeldepth4 <- lm(formula = ShalAccDel ~Minutes,
                  data = Depth_Corr4)
summary(modeldepth4)

baysianDepthShalDel = regressionBF(ShalAccDel~.,data=Depth_Corr4) 
baysianDepthShalDel

###################################################################################################
##########  End ##