

```{r config, echo = FALSE, warning = FALSE, results = 'hide', message=FALSE}
library(plyr)
library(lmSupport)
library(lme4)
library(reshape2)
library(ggplot2)
library(cowplot)
library(gridExtra)

```

Note that we simply load in the workspace here for creating the figure and for running the extra effect size statistics reported in the JoVE manuscript. For the actual analyses themselves where all models are created, see 'Analysis.Rmd' on https://osf.io/bbf3c/. Here, we start where that analysis ended and load in everything done there to make new figures and run effect size analyses. 

```{r JoVE figure, echo = FALSE, warning = FALSE, results = 'hide'}
# saving tables for the JoVE figure which i made in powerpoint/excel
setwd("~/Box Sync/LCNL Box Folder/Studies_Hopman/Hopman-ALL study (FYP Elise)/OSF data and analysis")
load(file="WorkspaceInclJoVE.Rdata")

# for this figure I want the probability data plotted in the same way as the others, so only by learning condition
pLearningConditionCovariateAvgProbImprob <- data.frame(learningCondition = c(-0.5, 0.5), vocabCovariateC = 0, probItemtype= 0)
pFCACPJoVE <- modelPredictions(mFCACP3, pLearningConditionCovariateAvgProbImprob)
pEMACPJoVE <- modelPredictions(mEMACP, pLearningConditionCovariateAvgProbImprob)
pFCRTPJoVE <- modelPredictions(mFCRTP, pLearningConditionCovariateAvgProbImprob)
pEMRTPJoVE <- modelPredictions(mEMRTP, pLearningConditionCovariateAvgProbImprob)

dataRTJoVE <- rbind.fill(pFCRTV, pFCRTA, pFCRTPJoVE, pEMRTW, pEMRTA, pEMRTPJoVE)
dataACJoVE <- rbind.fill(pFCACV, pFCACA, pFCACPJoVE, pEMACW, pEMACA, pEMACPJoVE)


# these two .csv files are used to create all JoVE results figures in powerpoint. 
write.csv(dataRTJoVE, "JoVERTData.csv")
write.csv(dataACJoVE, "JoVEACData.csv")
save.image("WorkspaceInclJoVE.Rdata")
```

```{r Jove stats, echo = TRUE}

# see also the file 'poweranalysisJoVE.R'!!

effectSize <- function(meandiff, varres, varintsub= 0, varslopesub = 0, varintit = 0, varslopeit = 0){
  d = meandiff/sqrt(varres + varintsub + varslopesub + varintit + varslopeit)
  print(d)
}
# function for effect sizes based on Westfall, Judd & Kenny, 2014 as cited in Brysbaert & Steyvers, 2019


# figure 6

# p values:

# using Wald's z test:
# (but this is anti-conservative, so only done as a first step)
summary(mFCACA)
summary(mEMACA2)

# using likelihood ratio:
# which is better according to: http://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#testing-hypotheses
# these are reported in JoVE manuscript

mFCACA0 = glmer(correctanswer ~ vocabCovariateC + learningCondition:snItemtype + snItemtype  + (1 + snItemtype|subjectnr), data=dataFCACA, family = "binomial")
summary(mFCACA0)
anova(mFCACA, mFCACA0)

mEMACA0 = glmer(correctanswer ~  vocabCovariateC + learningCondition:adjacencyItemtype + learningCondition:snItemtype + learningCondition:adjacencyItemtype:snItemtype + adjacencyItemtype*snItemtype  + (0 + adjacencyItemtype:snItemtype|subjectnr), data=dataEMACA, family = "binomial")
summary(mEMACA0)
anova(mEMACA2, mEMACA0)

# effect sizes (with help from Cassandra L. Jacobs)):

# note that neither Brysbaert & Stevens nor Westfall, Kenny & Judd explicitly talk about glmer's, only lmer's
# thus, we do this only for the RT lmer's, not for accuracy glmer's

# Figure 7

# figure 7A
modelSummary(mFCRTV)
# significant effect of learning condition

# manually read out the relevant variances
summary(mFCRTV)
mFCRTVresvar = 0.7481
mFCRTVintvar = 0.1088
# retrieve the mean difference from the model predictions
mFCRTVmeandiff = pFCRTV$Predicted[2] - pFCRTV$Predicted[1]
# and as a sanity check, yes, that's identical to the learningCondition coefficient

effectSize(mFCRTVmeandiff, mFCRTVresvar, mFCRTVintvar) 
# -0.19 (negative bc prod faster)

# figure 7B

modelSummary(mFCRTA)
# significant effect of learning condition

# manually read out the relevant variances
summary(mFCRTA)
mFCRTAresvar = 0.6290
mFCRTAintvar = 0.5501
# retrieve the mean difference from the model predictions
mFCRTAmeandiff = pFCRTA$Predicted[2] - pFCRTA$Predicted[1]
# and as a sanity check, yes, that's identical to the learningCondition coefficient

effectSize(mFCRTAmeandiff, mFCRTAresvar, mFCRTAintvar) 
# -0.54 (negative bc prod faster)

# Figure 7 C

modelSummary(mFCRTP)
# learningCondition is significant

# manually read out variances
summary(mFCRTP)
mFCRTPresvar = 0.325863
mFCRTPintvar = 0.074293
# retrieve the mean difference from the model predictions
mFCRTPmeandiff = pFCRTPJoVE$Predicted[2] - pFCRTPJoVE$Predicted[1] 
# note that the one titled pFCRTP has other factors, 
# the one called pFCRTPJoVE is appropriate because it only has the learningCondition factor
# and as a sanity check, yes, that's identical to the learningCondition coefficient

effectSize(mFCRTPmeandiff, mFCRTPresvar, mFCRTPintvar)
# -0.22

# figure 7D

modelSummary(mEMRTW)
# learningCondition is significant

# read out variances by hand
summary(mEMRTW)
mEMRTWresvar = 1.4500
mEMRTWintvar = 0.5173
# retrieve the mean difference from the model predictions
mEMRTWmeandiff = pEMRTW$Predicted[2] - pEMRTW$Predicted[1]
# and as a sanity check, yes, that's identical to the learningCondition coefficient

effectSize(mEMRTWmeandiff, mEMRTWresvar, mEMRTWintvar) 
# -0.28

# Figure 7E

modelSummary(mEMRTA)
# learningCondition is significant

# read out variances by hand
summary(mEMRTA)
mEMRTAresvar = 0.62395
mEMRTAintvar = 0.38892
# retrieve the mean difference from the model predictions
mEMRTAmeandiff = pEMRTA$Predicted[2] - pEMRTA$Predicted[1]
# and as a sanity check, yes, that's identical to the learningCondition coefficient

effectSize(mEMRTAmeandiff, mEMRTAresvar, mEMRTAintvar) 
# -0.33

# Figure 7F

modelSummary(mEMRTP)
# learningCondition is significant

# read out variances by hand
summary(mEMRTP)
mEMRTPresvar = 0.54970
mEMRTPintvar = 0.19868
# retrieve the mean difference from the model predictions
mEMRTPmeandiff = pEMRTPJoVE$Predicted[2] - pEMRTPJoVE$Predicted[1]
# note that the one titled pEMRTP has other factors, 
# the one called pEMRTPJoVE is appropriate because it only has the learningCondition factor
# and as a sanity check, yes, that's identical to the learningCondition coefficient

effectSize(mEMRTPmeandiff, mEMRTPresvar, mEMRTPintvar) 
# -0.25

save.image("WorkspaceInclJoVE.Rdata")
```

