Session 1 preprocessing
details.ellrep = file.info(list.files(path = "C:\\Users\\saptaf1\\Documents local\\ellrep-ti-task\\session1\\data", pattern = "*.csv", full.names = TRUE))
details.filtered.ellrep <- details.ellrep
data_all <- rownames(details.filtered.ellrep) %>%
map_df(~read_csv(.x, col_types = cols(slider.response = col_character(),slider.rt = col_character(),fixation_cross.stopped = col_character(),stopFeedback.started = col_character(),blockPerformance.started = col_character(), block_avg=col_number(),session=col_character(), block_avg_middlepair=col_number())))
## New names:
## • `` -> `...51`
session1.ellrep <- bind_rows(data_all) %>% mutate(stimCategory = case_when(
grepl(pattern = "faces", x = Image1) ~ "faces",
grepl(pattern = "objects", x = Image1) ~ "objects",
grepl(pattern = "scenes", x = Image1) ~ "scenes")) %>% filter(!is.na(Rank1))
## New names:
## • `...51` -> `...130`
session1.attention <- bind_rows(data_all) %>% mutate(stimCategory = case_when(
grepl(pattern = "faces", x = Image1) ~ "faces",
grepl(pattern = "objects", x = Image1) ~ "objects",
grepl(pattern = "scenes", x = Image1) ~ "scenes")) %>% filter(!is.na(num1))
## New names:
## • `...51` -> `...130`
session1.ellrep %>% group_by(participant) %>% summarise(blockcount = ifelse(sum(!is.na(key_resp_learning.keys))/10>10,10,sum(!is.na(key_resp_learning.keys)/10))) %>% filter(blockcount>=30) -> avgBlocks
hist(avgBlocks$blockcount)

session1.ellrep %>% group_by(participant) %>% summarise(blockcount = sum(!is.na(key_resp_learning.keys))/10) %>% filter(blockcount<=10)-> tmp
session1.avgs.ellrep <- session1.ellrep %>% select(participant,group, session, stimCategory, key_resp_im.corr, Rank1, Rank2) %>% mutate(cRank= paste0(pmin(Rank1,Rank2),pmax(Rank1,Rank2))) %>% group_by(participant,group, session,cRank,stimCategory) %>% summarise_each(funs(mean(key_resp_im.corr, na.rm = TRUE))) %>% filter(!is.na(session)) %>% select(-Rank1,-Rank2)
# for output
session1.ellrep %>% select(participant,group, session, stimCategory, key_resp_im.corr,key_resp_im.rt, Rank1, Rank2,date) %>% mutate(cRank= paste0(pmin(Rank1,Rank2),pmax(Rank1,Rank2))) %>% na.omit() %>% rename(corr=key_resp_im.corr, rt=key_resp_im.rt) %>% mutate(part="immediateTest") %>% mutate(date = lubridate::parse_date_time(date,'ymd HMS!')) %>% mutate(learningCriteria = ifelse(date>=as.Date('2022-11-23 10:30'),"75%","66%")) -> s1.im
s1.im %>% saveRDS("ellrep_tamas/session1_ellrep_trials_immediate_design-bws.RDS")
# NOTE!! 4xAFERQVTO not okay // 136 people reached criteria
s1.im %>% group_by(participant,group,date) %>% count() %>% nrow()
## [1] 136
session1.ellrep %>% select(participant,group, session, stimCategory, key_resp_learning.corr,key_resp_learning.rt, Rank1, Rank2,date) %>% mutate(part="learning") %>% mutate(date = lubridate::parse_date_time(date,'ymd HMS!')) %>% mutate(learningCriteria = ifelse(date>=as.Date('2022-11-23 10:30'),"75%","66%")) %>% mutate(cRank= paste0(pmin(Rank1,Rank2),pmax(Rank1,Rank2))) %>% na.omit() %>% filter(date %in% s1.im$date ) ->tmp.learn
tmp.learn %>% saveRDS("ellrep_tamas/session1_ellrep_trials_learning_design-bws.RDS")
# NOTE!! 4xAFERQVTO not okay // 134 people reached criteria -weird discrepancy
tmp.learn %>% group_by(participant,group,date) %>% count() %>% nrow()
## [1] 134
#attention checks
session1.attention %>% select(participant,group, session, date,slider.response,desc,num1,num2, totalsum) %>% mutate(date = lubridate::parse_date_time(date,'ymd HMS!')) %>% na.omit() %>% filter(date %in% s1.im$date ) %>% saveRDS("ellrep_tamas/session1_ellrep_attentioncheck_design-bws.RDS")
#immediate recall performance
print(head(session1.avgs.ellrep))
## # A tibble: 6 × 6
## # Groups: participant, group, session, cRank [6]
## participant group session cRank stimCategory key_resp_im.corr
## <chr> <chr> <chr> <chr> <chr> <dbl>
## 1 AFERQVTO sleep 1 12 faces NaN
## 2 AFERQVTO sleep 1 23 faces NaN
## 3 AFERQVTO sleep 1 34 faces NaN
## 4 AFERQVTO sleep 1 45 faces NaN
## 5 AFERQVTO sleep 1 56 faces NaN
## 6 AFERQVTO wake 1 12 faces 0.52
immediatePerformance.s1 <- session1.avgs.ellrep %>% mutate(pairType= ifelse(cRank %in% c("12","23","34","45","56"),"premise", ifelse(cRank %in% c("24","25","35"), "inference","anchor"))) %>% na.omit() %>% group_by(participant,group, pairType) %>% summarise(meanPerf=mean(key_resp_im.corr))
## `summarise()` has grouped output by 'participant', 'group'. You can override
## using the `.groups` argument.
grouped_ggbetweenstats(
data = immediatePerformance.s1,
x = group,
y = meanPerf,
grouping.var = pairType,
type="p",
bf.messsage = FALSE
)

s1.im %>% group_by(group,date,participant,learningCriteria) %>% summarise(meanPer=mean(corr))
## `summarise()` has grouped output by 'group', 'date', 'participant'. You can
## override using the `.groups` argument.
## # A tibble: 136 × 5
## # Groups: group, date, participant [136]
## group date participant learningCriteria meanPer
## <chr> <dttm> <chr> <chr> <dbl>
## 1 sleep 2022-02-16 21:28:45 EYHPBRAT 66% 0.8
## 2 sleep 2022-02-23 20:59:31 XUWCSLAG 66% 0.25
## 3 sleep 2022-02-23 21:49:07 LWAHPZSK 66% 0.725
## 4 sleep 2022-02-24 21:05:51 ZDQXYIOU 66% 0.375
## 5 sleep 2022-02-24 21:11:05 OIEJGADV 66% 0.55
## 6 sleep 2022-02-25 21:01:06 WLZGFQUD 66% 0.85
## 7 sleep 2022-02-25 21:08:17 NCBXYVKS 66% 0.775
## 8 sleep 2022-02-25 21:09:00 LJXNTYCF 66% 0.5
## 9 sleep 2022-02-26 21:20:59 USTGPEMF 66% 0.75
## 10 sleep 2022-02-28 21:05:46 SJXWYEKQ 66% 0.575
## # … with 126 more rows
grouped_ggbetweenstats(
data = s1.im %>% group_by(group,date,participant,learningCriteria) %>% summarise(meanPerf=mean(corr)),
x = group,
y = meanPerf,
grouping.var = learningCriteria,
type="p",
bf.messsage = FALSE
)
## `summarise()` has grouped output by 'group', 'date', 'participant'. You can
## override using the `.groups` argument.

ggbetweenstats(
data = s1.im %>% group_by(group,date,participant,learningCriteria) %>% summarise(meanPerf=mean(corr)) %>% mutate(condition=paste(group,learningCriteria)),
x = condition,
y = meanPerf,
type="np",
pairwise.display = "all",
bf.messsage = FALSE,
p.adjust.method = "none"
)
## `summarise()` has grouped output by 'group', 'date', 'participant'. You can
## override using the `.groups` argument.
## Adding missing grouping variables: `group`, `date`, `participant`

Session 2 preprocessing
details.ellrep = file.info(list.files(path = "C:\\Users\\saptaf1\\Documents local\\ellrep-ti-task\\session2\\data",
pattern = "*.csv", full.names = TRUE))
details.filtered.ellrep <- details.ellrep
data_all <- rownames(details.filtered.ellrep) %>%
map_df(~read_csv(.x, col_types = cols(slider.response = col_character(),slider.rt = col_character(),fixation_cross.stopped = col_character(),stopFeedback.started = col_character(),blockPerformance.started = col_character(), block_avg=col_number(), block_avg_middlepair=col_number())))
session2.ellrep <- bind_rows(data_all) %>% mutate(stimCategory = case_when(
grepl(pattern = "faces", x = Image1) ~ "faces",
grepl(pattern = "objects", x = Image1) ~ "objects",
grepl(pattern = "scenes", x = Image1) ~ "scenes")) %>% filter(!is.na(Rank1))
# session2.ellrep %>% group_by(participant) %>% summarise(blockcount = ifelse(sum(!is.na(key_resp_learning.keys))/10>10,10,sum(!is.na(key_resp_learning.keys)/10))) %>% filter(blockcount>=30) -> avgBlocks
# hist(avgBlocks$blockcount)
# session2.ellrep %>% group_by(participant) %>% summarise(blockcount = sum(!is.na(key_resp_learning.keys))/10) %>% filter(blockcount<=10)-> tmp
session2.ellrep %>% select(participant,group, session, stimCategory, key_resp_im.corr,key_resp_im.rt, Rank1, Rank2,date) %>% mutate(cRank= paste0(pmin(Rank1,Rank2),pmax(Rank1,Rank2))) %>% na.omit() %>% rename(corr=key_resp_im.corr, rt=key_resp_im.rt) %>% mutate(part="delayedTest") %>% mutate(date = lubridate::parse_date_time(date,'ymd HMS!')) %>% mutate(learningCriteria = ifelse(date>=as.Date('2022-11-24'),"75%","66%"), session=2) %>% saveRDS("ellrep_tamas/session2_ellrep_trials_delayed_design-bws.RDS")
session2.ellrep %>% select(participant,group, session, stimCategory, key_resp_im.corr,key_resp_im.rt, Rank1, Rank2,date) %>% mutate(cRank= paste0(pmin(Rank1,Rank2),pmax(Rank1,Rank2))) %>% na.omit() %>% rename(corr=key_resp_im.corr, rt=key_resp_im.rt) %>% mutate(part="delayedTest") %>% mutate(date = lubridate::parse_date_time(date,'ymd HMS!')) %>% mutate(learningCriteria = ifelse(date>=as.Date('2022-11-24'),"75%","66%"), session=2) %>% mutate(participant_u_id =paste(participant,group,date, sep ="_" ), distance=abs(Rank1-Rank2), pairType=ifelse(distance==1,"premise",ifelse(distance %in% c(2,3), "inference","anchor"))) %>% filter(group %in% c("wake","sleep"))-> session2.ellrep.tidy
session2.avgs.ellrep <- session2.ellrep %>% select(participant,group, session, stimCategory, key_resp_im.corr, Rank1, Rank2,date) %>% mutate(cRank= paste0(pmin(Rank1,Rank2),pmax(Rank1,Rank2))) %>% group_by(participant,group, session,cRank,stimCategory,date) %>% summarise_each(funs(mean(key_resp_im.corr, na.rm = TRUE))) %>% filter(!is.na(session)) %>% select(-Rank1,-Rank2) %>% mutate(date = lubridate::parse_date_time(date,'ymd HMS!')) %>% mutate(learningCriteria = ifelse(date>=as.Date('2022-11-24'),"75%","66%")) %>% filter(group != "ws")
# how many people per group
# time of when the raise to 75% happens: 2022-11-23_10h49.00.768.csv
session2.avgs.ellrep %>% group_by(participant,group,date,learningCriteria) %>% count() %>% ungroup() %>% group_by(group, learningCriteria) %>% count() %>% arrange(learningCriteria)
## # A tibble: 4 × 3
## # Groups: group, learningCriteria [4]
## group learningCriteria n
## <chr> <chr> <int>
## 1 sleep 66% 38
## 2 wake 66% 34
## 3 sleep 75% 29
## 4 wake 75% 34
#for some reason there are 2s and 3s in this column.. investigate
session2.avgs.ellrep$session <-2
#immediate recall performance
print(length(unique(session2.avgs.ellrep$participant)))
## [1] 133
#immediate recall performance
print(head(session2.avgs.ellrep))
## # A tibble: 6 × 8
## # Groups: participant, group, session, cRank, stimCategory [6]
## participant group session cRank stimCate…¹ date key_r…² learn…³
## <chr> <chr> <dbl> <chr> <chr> <dttm> <dbl> <chr>
## 1 AFERQVTO sleep 2 12 faces 2022-11-23 18:43:13 0.375 66%
## 2 AFERQVTO sleep 2 16 faces 2022-11-23 18:43:13 0.875 66%
## 3 AFERQVTO sleep 2 23 faces 2022-11-23 18:43:13 0.625 66%
## 4 AFERQVTO sleep 2 24 faces 2022-11-23 18:43:13 0.5 66%
## 5 AFERQVTO sleep 2 25 faces 2022-11-23 18:43:13 0.375 66%
## 6 AFERQVTO sleep 2 34 faces 2022-11-23 18:43:13 0.5 66%
## # … with abbreviated variable names ¹​stimCategory, ²​key_resp_im.corr,
## # ³​learningCriteria
grouped_ggbetweenstats(
data = session2.avgs.ellrep %>% mutate(pairType= ifelse(cRank %in% c("12","23","34","45","56"),"premise", ifelse(cRank %in% c("24","25","35"), "inference","anchor"))) %>% group_by(participant,group,learningCriteria, pairType) %>% summarise(meanPerf=mean(key_resp_im.corr)) %>% ungroup(),
x = group,
y = meanPerf,
grouping.var = pairType,
type="p",
bf.messsage = FALSE
)
## `summarise()` has grouped output by 'participant', 'group', 'learningCriteria'.
## You can override using the `.groups` argument.

grouped_ggbetweenstats(
data = session2.avgs.ellrep %>% mutate(pairType= ifelse(cRank %in% c("12","23","34","45","56"),"premise", ifelse(cRank %in% c("24","25","35"), "inference","anchor"))) %>% group_by(participant,group,learningCriteria, pairType) %>% summarise(meanPerf=mean(key_resp_im.corr)) %>% ungroup() %>% filter(learningCriteria== "66%"),
x = group,
y = meanPerf,
grouping.var = pairType,
type="np",
bf.messsage = FALSE
)
## `summarise()` has grouped output by 'participant', 'group', 'learningCriteria'.
## You can override using the `.groups` argument.

grouped_ggbetweenstats(
data = session2.avgs.ellrep %>% mutate(pairType= ifelse(cRank %in% c("12","23","34","45","56"),"premise", ifelse(cRank %in% c("24","25","35"), "inference","anchor"))) %>% group_by(participant,group,learningCriteria, pairType) %>% summarise(meanPerf=mean(key_resp_im.corr)) %>% ungroup() %>% filter(learningCriteria== "75%"),
x = group,
y = meanPerf,
grouping.var = pairType,
type="p",
bf.messsage = FALSE
)
## `summarise()` has grouped output by 'participant', 'group', 'learningCriteria'.
## You can override using the `.groups` argument.

# proper filtering
s1.im %>% group_by(participant,group,date)%>% summarise(meanIm=mean(corr)) %>% filter(participant !="AFERQVTO") -> s1.im.aggr
## `summarise()` has grouped output by 'participant', 'group'. You can override
## using the `.groups` argument.
session2.ellrep.tidy %>% group_by(participant,learningCriteria,group,date)%>%filter(pairType=="premise") %>% summarise(meanDelayed=mean(corr)) %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% pivot_longer(c("meanIm","meanDelayed"), names_to = "session", values_to = "meanPerf")
## `summarise()` has grouped output by 'participant', 'learningCriteria', 'group'.
## You can override using the `.groups` argument.
## # A tibble: 270 × 7
## # Groups: participant, learningCriteria, group [134]
## parti…¹ learn…² group date.x date.y session meanP…³
## <chr> <chr> <chr> <dttm> <dttm> <chr> <dbl>
## 1 AFERQV… 66% sleep 2022-11-23 18:43:13 NA meanIm NA
## 2 AFERQV… 66% sleep 2022-11-23 18:43:13 NA meanDe… 0.525
## 3 AFERQV… 66% wake 2022-02-08 17:41:31 NA meanIm NA
## 4 AFERQV… 66% wake 2022-02-08 17:41:31 NA meanDe… 0.5
## 5 AHSRJU… 66% wake 2022-11-15 22:43:18 2022-11-15 10:27:37 meanIm 0.6
## 6 AHSRJU… 66% wake 2022-11-15 22:43:18 2022-11-15 10:27:37 meanDe… 0.375
## 7 AKPJDW… 75% wake 2022-12-06 04:47:34 2022-12-05 09:07:45 meanIm 0.95
## 8 AKPJDW… 75% wake 2022-12-06 04:47:34 2022-12-05 09:07:45 meanDe… 0.775
## 9 AKPJDW… 75% wake 2022-12-09 22:45:00 2022-12-09 09:08:27 meanIm 0.35
## 10 AKPJDW… 75% wake 2022-12-09 22:45:00 2022-12-09 09:08:27 meanDe… 0.475
## # … with 260 more rows, and abbreviated variable names ¹​participant,
## # ²​learningCriteria, ³​meanPerf
grouped_ggwithinstats(
data = session2.ellrep.tidy %>% group_by(participant,learningCriteria,group,date)%>%filter(pairType=="premise") %>% summarise(meanDelayed=mean(corr)) %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% pivot_longer(c("meanIm","meanDelayed"), names_to = "session", values_to = "meanPerf"),
x = session,
y = meanPerf,
grouping.var = learningCriteria,
bf.message=FALSE,
type="p"
)
## `summarise()` has grouped output by 'participant', 'learningCriteria', 'group'.
## You can override using the `.groups` argument.

session2.ellrep.tidy %>% group_by(participant,learningCriteria,group,date)%>%filter(pairType=="premise") %>% summarise(meanDelayed=mean(corr)) %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% pivot_longer(c("meanIm","meanDelayed"), names_to = "session", values_to = "meanPerf") %>% ungroup() %>% filter(participant !="AFERQVTO")-> anova_dat
## `summarise()` has grouped output by 'participant', 'learningCriteria', 'group'.
## You can override using the `.groups` argument.
# library(rstatix)
# res.aov <- anova_test(
# data = session2.ellrep.tidy %>% group_by(participant,learningCriteria,group,date)%>%filter(pairType=="premise") %>% summarise(meanDelayed=mean(corr)) %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% pivot_longer(c("meanIm","meanDelayed"), names_to = "session", values_to = "meanPerf") %>% ungroup() %>% filter(participant %in% subs.normalrange) %>% mutate(participant=paste0(participant,date.x)), dv = meanPerf, wid = participant,
# between = c(group, learningCriteria), within = session)
# get_anova_table(res.aov)
library(afex)
## Loading required package: lme4
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
## ************
## Welcome to afex. For support visit: http://afex.singmann.science/
## - Functions for ANOVAs: aov_car(), aov_ez(), and aov_4()
## - Methods for calculating p-values with mixed(): 'S', 'KR', 'LRT', and 'PB'
## - 'afex_aov' and 'mixed' objects can be passed to emmeans() for follow-up tests
## - NEWS: emmeans() for ANOVA models now uses model = 'multivariate' as default.
## - Get and set global package options with: afex_options()
## - Set orthogonal sum-to-zero contrasts globally: set_sum_contrasts()
## - For example analyses see: browseVignettes("afex")
## ************
##
## Attaching package: 'afex'
##
## The following object is masked from 'package:lme4':
##
## lmer
(aw <- aov_ez("participant", "meanPerf", anova_dat, within = c("session"), between=c("group", "learningCriteria")))
## Converting to factor: group, learningCriteria
## Contrasts set to contr.sum for the following variables: group, learningCriteria
## Anova Table (Type 3 tests)
##
## Response: meanPerf
## Effect df MSE F ges p.value
## 1 group 1, 125 0.06 0.17 .001 .685
## 2 learningCriteria 1, 125 0.06 1.43 .010 .234
## 3 group:learningCriteria 1, 125 0.06 2.69 .019 .103
## 4 session 1, 125 0.01 9.47 ** .006 .003
## 5 group:session 1, 125 0.01 3.58 + .002 .061
## 6 learningCriteria:session 1, 125 0.01 0.02 <.001 .893
## 7 group:learningCriteria:session 1, 125 0.01 2.41 .002 .123
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1
afex_plot(aw, x = "session", trace = c("learningCriteria", "group"))

session2.ellrep.tidy.ext <- session2.ellrep.tidy %>% filter(pairType == "inference") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant!="AFERQVTO") %>% mutate(timeelapsed=date.x-date.y)
session2.ellrep.tidy.ext %>% group_by(participant,group,date.x) %>% summarise(timeelapsed=unique(timeelapsed)) %>% ggplot(., aes(timeelapsed)) +
geom_histogram()
## `summarise()` has grouped output by 'participant', 'group'. You can override
## using the `.groups` argument.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

minTime<-480
maxTime<-840
session2.ellrep.tidy.ext %>% group_by(participant,group,date.x) %>% summarise(timeelapsed=unique(timeelapsed)) %>% filter(timeelapsed>minTime,timeelapsed<maxTime) %>% ggplot(., aes(timeelapsed)) +
geom_histogram()
## `summarise()` has grouped output by 'participant', 'group'. You can override
## using the `.groups` argument.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

session2.ellrep.tidy.ext %>% group_by(participant,group,date.x) %>% summarise(timeelapsed=unique(timeelapsed)) %>% filter(timeelapsed>minTime,timeelapsed<maxTime) -> subs.normalrange
## `summarise()` has grouped output by 'participant', 'group'. You can override
## using the `.groups` argument.
grouped_ggbetweenstats(
data = session2.avgs.ellrep %>% mutate(pairType= ifelse(cRank %in% c("12","23","34","45","56"),"premise", ifelse(cRank %in% c("24","25","35"), "inference","anchor"))) %>% group_by(participant,group,learningCriteria, pairType) %>% summarise(meanPerf=mean(key_resp_im.corr)) %>% ungroup() %>% filter(participant %in% subs.normalrange$participant),
x = group,
y = meanPerf,
grouping.var = pairType,
type="r",
bf.messsage = FALSE
)
## `summarise()` has grouped output by 'participant', 'group', 'learningCriteria'.
## You can override using the `.groups` argument.

grouped_ggbetweenstats(
data = session2.avgs.ellrep %>% mutate(pairType= ifelse(cRank %in% c("12","23","34","45","56"),"premise", ifelse(cRank %in% c("24","25","35"), "inference","anchor"))) %>% group_by(participant,group,learningCriteria, pairType) %>% summarise(meanPerf=mean(key_resp_im.corr)) %>% ungroup() %>% filter(learningCriteria== "66%") %>% filter(participant %in% subs.normalrange$participant),
x = group,
y = meanPerf,
grouping.var = pairType,
type="p",
bf.messsage = FALSE
)
## `summarise()` has grouped output by 'participant', 'group', 'learningCriteria'.
## You can override using the `.groups` argument.

grouped_ggbetweenstats(
data = session2.avgs.ellrep %>% mutate(pairType= ifelse(cRank %in% c("12","23","34","45","56"),"premise", ifelse(cRank %in% c("24","25","35"), "inference","anchor"))) %>% group_by(participant,group,learningCriteria, pairType) %>% summarise(meanPerf=mean(key_resp_im.corr)) %>% ungroup() %>% filter(learningCriteria== "75%") %>% filter(participant %in% subs.normalrange$participant),
x = group,
y = meanPerf,
grouping.var = pairType,
type="p",
bf.messsage = FALSE
)
## `summarise()` has grouped output by 'participant', 'group', 'learningCriteria'.
## You can override using the `.groups` argument.

Stats
library(tidyverse) # data wrangling and visualization
library(sjPlot) # to visualizing mixed-effects models
# library(effects) # to visualizing mixed-effects models
library(lme4) # "golden standard" for mixed-effects modelling in R (no p-values)
library(lmerTest) # p-values for MEMs based on the Satterthwaite approximation
##
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
##
## lmer
## The following object is masked from 'package:stats':
##
## step
library(report) # mainly for an "report" function
# library(emmeans) # post-hoc analysis
# library(knitr) # beautifying tables
# library(sjstats) # ICC - intraclass-correlation coefficient
# library(caret) # ML, model comparison & utility functions
str(session2.ellrep.tidy)
## tibble [9,720 × 15] (S3: tbl_df/tbl/data.frame)
## $ participant : chr [1:9720] "AFERQVTO" "AFERQVTO" "AFERQVTO" "AFERQVTO" ...
## $ group : chr [1:9720] "wake" "wake" "wake" "wake" ...
## $ session : num [1:9720] 2 2 2 2 2 2 2 2 2 2 ...
## $ stimCategory : chr [1:9720] "faces" "faces" "faces" "faces" ...
## $ corr : num [1:9720] 0 0 1 1 0 0 0 1 0 0 ...
## $ rt : num [1:9720] 1.168 0.244 0.154 0.177 0.134 ...
## $ Rank1 : num [1:9720] 5 5 2 1 6 4 3 2 2 5 ...
## $ Rank2 : num [1:9720] 3 2 3 2 1 3 2 5 1 4 ...
## $ date : POSIXct[1:9720], format: "2022-02-08 17:41:31" "2022-02-08 17:41:31" ...
## $ cRank : chr [1:9720] "35" "25" "23" "12" ...
## $ part : chr [1:9720] "delayedTest" "delayedTest" "delayedTest" "delayedTest" ...
## $ learningCriteria: chr [1:9720] "66%" "66%" "66%" "66%" ...
## $ participant_u_id: chr [1:9720] "AFERQVTO_wake_2022-02-08 17:41:31" "AFERQVTO_wake_2022-02-08 17:41:31" "AFERQVTO_wake_2022-02-08 17:41:31" "AFERQVTO_wake_2022-02-08 17:41:31" ...
## $ distance : num [1:9720] 2 3 1 1 5 1 1 3 1 1 ...
## $ pairType : chr [1:9720] "inference" "inference" "premise" "premise" ...
session2.ellrep.tidy %>% group_by(group, learningCriteria) %>% summarise(subs = n_distinct(participant_u_id))
## `summarise()` has grouped output by 'group'. You can override using the
## `.groups` argument.
## # A tibble: 4 × 3
## # Groups: group [2]
## group learningCriteria subs
## <chr> <chr> <int>
## 1 sleep 66% 38
## 2 sleep 75% 29
## 3 wake 66% 34
## 4 wake 75% 34
#good plot
ggplot(aes(x=group, y=corr, group=interaction(learningCriteria,pairType), color=pairType, linetype=learningCriteria), data=session2.ellrep.tidy) +
stat_summary(fun.data="mean_cl_boot", geom='line') + ggtitle("All data..") + stat_summary(fun.data=mean_cl_boot,fun.args=list(conf.int=.68), geom='errorbar', width=0.1) + ylab("Inference %")

# grouped_ggbetweenstats(
# data = session2.ellrep.tidy %>% filter(learningCriteria=="75%",participant %in% subs.normalrange$participant)%>% group_by(group, pairType,participant_u_id) %>% summarise(meanPerf= mean(corr)),
# x = group,
# y = meanPerf,
# grouping.var = pairType,
# type="r",
# bf.messsage = FALSE
# )
ggplot(aes(x=group, y=corr, group=interaction(learningCriteria,pairType), color=pairType, linetype=learningCriteria), data=session2.ellrep.tidy%>% filter(participant %in% subs.normalrange$participant)) +
stat_summary(fun.data="mean_cl_boot", geom='line') + stat_summary(fun.data=mean_cl_boot,fun.args=list(conf.int=.68), geom='errorbar', width=0.1) + ggtitle("Filtered data..") + ylab("Inference %")

#p=0.064 groupXpairType interaction
m1 <- glmer(corr ~ group*pairType + (1 | participant_u_id ), data = session2.ellrep.tidy%>% filter(participant %in% subs.normalrange$participant, pairType %in% c("premise","inference")), family = binomial, control = glmerControl(optimizer = "bobyqa"))
m2 <- glmer(corr ~ group*pairType + meanIm + (1 | participant_u_id ), data = session2.ellrep.tidy%>% filter(participant %in% subs.normalrange$participant,pairType %in% c("premise","inference")) %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")), family = binomial, control = glmerControl(optimizer = "bobyqa"))
tab_model(m1,m2)
|
Â
|
corr
|
corr
|
|
Predictors
|
Odds Ratios
|
CI
|
p
|
Odds Ratios
|
CI
|
p
|
|
(Intercept)
|
1.45
|
1.12 – 1.87
|
0.005
|
0.12
|
0.08 – 0.20
|
<0.001
|
|
group [wake]
|
0.82
|
0.57 – 1.18
|
0.288
|
0.82
|
0.63 – 1.07
|
0.137
|
|
pairType [premise]
|
1.81
|
1.56 – 2.11
|
<0.001
|
1.81
|
1.56 – 2.10
|
<0.001
|
group [wake] × pairType [premise]
|
1.09
|
0.88 – 1.35
|
0.426
|
1.09
|
0.88 – 1.35
|
0.412
|
|
meanIm
|
|
|
|
32.08
|
16.81 – 61.22
|
<0.001
|
|
Random Effects
|
|
σ2
|
3.29
|
3.29
|
|
τ00
|
0.77 participant_u_id
|
0.30 participant_u_id
|
|
ICC
|
0.19
|
0.08
|
|
N
|
110 participant_u_id
|
110 participant_u_id
|
|
Observations
|
7040
|
7040
|
|
Marginal R2 / Conditional R2
|
0.024 / 0.209
|
0.123 / 0.196
|
#mean inf sig above chance
m1.inf <- glmer(corr ~ group + learningCriteria + (1 | participant_u_id ), data = session2.ellrep.tidy%>% filter(participant %in% subs.normalrange$participant) %>% filter(pairType == "inference"), family = binomial, control = glmerControl(optimizer = "bobyqa"))
tab_model(m1.inf)
|
Â
|
corr
|
|
Predictors
|
Odds Ratios
|
CI
|
p
|
|
(Intercept)
|
1.57
|
1.05 – 2.36
|
0.027
|
|
group [wake]
|
0.80
|
0.49 – 1.31
|
0.379
|
|
learningCriteria [75%]
|
0.90
|
0.55 – 1.48
|
0.684
|
|
Random Effects
|
|
σ2
|
3.29
|
|
τ00 participant_u_id
|
1.47
|
|
ICC
|
0.31
|
|
N participant_u_id
|
110
|
|
Observations
|
2640
|
|
Marginal R2 / Conditional R2
|
0.003 / 0.311
|
# relevant
m1.inf.subset <- glmer(corr ~ group+ (1 | participant_u_id ), data = session2.ellrep.tidy %>% filter(pairType == "inference", learningCriteria =="75%") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant %in% subs.normalrange$participant), family = binomial, control = glmerControl(optimizer = "bobyqa"))
m2.inf.subset <- glmer(corr ~ group+meanIm+ (1 | participant_u_id ), data = session2.ellrep.tidy %>% filter(pairType == "inference", learningCriteria =="75%") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant %in% subs.normalrange$participant), family = binomial, control = glmerControl(optimizer = "bobyqa"))
m3.inf.subset <- glmer(corr ~ group*meanIm+ (1 | participant_u_id ), data = session2.ellrep.tidy %>% filter(pairType == "inference", learningCriteria =="75%") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant %in% subs.normalrange$participant), family = binomial, control = glmerControl(optimizer = "bobyqa"))
tab_model(m1.inf.subset,m2.inf.subset,m3.inf.subset)
|
Â
|
corr
|
corr
|
corr
|
|
Predictors
|
Odds Ratios
|
CI
|
p
|
Odds Ratios
|
CI
|
p
|
Odds Ratios
|
CI
|
p
|
|
(Intercept)
|
1.60
|
0.87 – 2.95
|
0.132
|
0.25
|
0.05 – 1.36
|
0.109
|
0.14
|
0.01 – 1.96
|
0.143
|
|
group [wake]
|
0.66
|
0.27 – 1.57
|
0.345
|
0.71
|
0.31 – 1.63
|
0.418
|
1.82
|
0.07 – 50.29
|
0.724
|
|
meanIm
|
|
|
|
12.33
|
1.42 – 107.41
|
0.023
|
27.95
|
0.81 – 958.91
|
0.065
|
|
group [wake] × meanIm
|
|
|
|
|
|
|
0.27
|
0.00 – 23.38
|
0.566
|
|
Random Effects
|
|
σ2
|
3.29
|
3.29
|
3.29
|
|
τ00
|
2.07 participant_u_id
|
1.82 participant_u_id
|
1.82 participant_u_id
|
|
ICC
|
0.39
|
0.36
|
0.36
|
|
N
|
47 participant_u_id
|
47 participant_u_id
|
47 participant_u_id
|
|
Observations
|
1128
|
1128
|
1128
|
|
Marginal R2 / Conditional R2
|
0.008 / 0.391
|
0.051 / 0.389
|
0.054 / 0.391
|
# relevant
m1.inf <- glmer(corr ~ group+ (1 | participant_u_id ) + (1 |stimCategory), data = session2.ellrep.tidy %>% filter(pairType == "inference") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant %in% subs.normalrange$participant), family = binomial, control = glmerControl(optimizer = "bobyqa"))
m2.inf <- glmer(corr ~ group+meanIm+ (1 | participant_u_id ) + (1 |stimCategory), data = session2.ellrep.tidy %>% filter(pairType == "inference") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant %in% subs.normalrange$participant), family = binomial, control = glmerControl(optimizer = "bobyqa"))
m3.inf <- glmer(corr ~ group*meanIm+ (1 | participant_u_id )+ (1 |stimCategory), data = session2.ellrep.tidy %>% filter(pairType == "inference") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant %in% subs.normalrange$participant), family = binomial, control = glmerControl(optimizer = "bobyqa"))
tab_model(m1.inf,m2.inf,m3.inf)
|
Â
|
corr
|
corr
|
corr
|
|
Predictors
|
Odds Ratios
|
CI
|
p
|
Odds Ratios
|
CI
|
p
|
Odds Ratios
|
CI
|
p
|
|
(Intercept)
|
1.48
|
0.98 – 2.24
|
0.064
|
0.32
|
0.12 – 0.84
|
0.021
|
0.25
|
0.06 – 1.01
|
0.052
|
|
group [wake]
|
0.80
|
0.49 – 1.30
|
0.368
|
0.80
|
0.50 – 1.27
|
0.345
|
1.25
|
0.19 – 8.21
|
0.816
|
|
meanIm
|
|
|
|
9.07
|
2.48 – 33.14
|
0.001
|
13.06
|
1.83 – 93.30
|
0.010
|
|
group [wake] × meanIm
|
|
|
|
|
|
|
0.53
|
0.04 – 7.15
|
0.630
|
|
Random Effects
|
|
σ2
|
3.29
|
3.29
|
3.29
|
|
τ00
|
1.44 participant_u_id
|
1.30 participant_u_id
|
1.31 participant_u_id
|
|
|
0.04 stimCategory
|
0.01 stimCategory
|
0.01 stimCategory
|
|
ICC
|
0.31
|
0.29
|
0.29
|
|
N
|
110 participant_u_id
|
110 participant_u_id
|
110 participant_u_id
|
|
|
3 stimCategory
|
3 stimCategory
|
3 stimCategory
|
|
Observations
|
2640
|
2640
|
2640
|
|
Marginal R2 / Conditional R2
|
0.003 / 0.312
|
0.037 / 0.312
|
0.038 / 0.313
|
m1.inf.ext <- glmer(corr ~ group*meanIm + (1 | participant_u_id ), data = session2.ellrep.tidy %>% filter(pairType == "inference") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant %in% subs.normalrange$participant) %>% na.omit(), family = binomial, control = glmerControl(optimizer = "bobyqa"))
tab_model(m1.inf.ext)
|
Â
|
corr
|
|
Predictors
|
Odds Ratios
|
CI
|
p
|
|
(Intercept)
|
0.24
|
0.06 – 0.97
|
0.045
|
|
group [wake]
|
1.30
|
0.20 – 8.42
|
0.783
|
|
meanIm
|
13.58
|
1.96 – 94.12
|
0.008
|
|
group [wake] × meanIm
|
0.50
|
0.04 – 6.57
|
0.595
|
|
Random Effects
|
|
σ2
|
3.29
|
|
τ00 participant_u_id
|
1.32
|
|
ICC
|
0.29
|
|
N participant_u_id
|
110
|
|
Observations
|
2640
|
|
Marginal R2 / Conditional R2
|
0.038 / 0.314
|
m1.inf.ext.sleep <- glmer(corr ~ learningCriteria+meanIm + (1 | participant_u_id ), data = session2.ellrep.tidy %>% filter(pairType == "inference", group=="sleep") %>% left_join(s1.im.aggr, by = c("participant"="participant","group"="group")) %>% filter(participant %in% subs.normalrange$participant) %>% na.omit(), family = binomial, control = glmerControl(optimizer = "bobyqa"))
tab_model(m1.inf.ext.sleep)
|
Â
|
corr
|
|
Predictors
|
Odds Ratios
|
CI
|
p
|
|
(Intercept)
|
0.25
|
0.06 – 0.97
|
0.046
|
|
learningCriteria [75%]
|
0.90
|
0.46 – 1.75
|
0.761
|
|
meanIm
|
14.07
|
2.01 – 98.68
|
0.008
|
|
Random Effects
|
|
σ2
|
3.29
|
|
τ00 participant_u_id
|
1.28
|
|
ICC
|
0.28
|
|
N participant_u_id
|
56
|
|
Observations
|
1344
|
|
Marginal R2 / Conditional R2
|
0.043 / 0.311
|
session2.ellrep.tidy %>% filter(pairType == "inference") %>% left_join(s1.im.aggr, by = c("participant"="participant")) %>% filter(participant %in% subs.normalrange$participant) %>% na.omit() %>% group_by(group.x,learningCriteria) %>% summarise(subs = n_distinct(participant_u_id))
## `summarise()` has grouped output by 'group.x'. You can override using the
## `.groups` argument.
## # A tibble: 4 × 3
## # Groups: group.x [2]
## group.x learningCriteria subs
## <chr> <chr> <int>
## 1 sleep 66% 32
## 2 sleep 75% 24
## 3 wake 66% 31
## 4 wake 75% 23
plot_model(m1.inf.ext,"pred", terms = c("meanIm[all]","group"))

plot_model(m1.inf.ext,"int", terms = c("group"))
