last updated: 2018-12-10, source file (will be): here
### loads from raw data
read_raw = TRUE
### load relevant libraries
# for tidy data
library("tidyverse")
# for plotting
library("ggplot2")
library("png")
library("grid")
### for modelling
library("lme4")
# create path variables
## paths to log
files_raw_rt <- list.files(path = "..\\experiment\\data\\raw", # log in the future
pattern = "*.csv",
full.names = TRUE)
# files_que <- list.files(path = "../01_experiment/data/questionnaires/",
# pattern = "*.csv",
# full.names = TRUE)
# path_save <- "../01_experiment/data/"
(from: R for Data Science)
### import reaction time data
df_rt_raw <-
files_raw_rt %>%
map_df(read_csv,
col_types=cols(subject = col_character(),
session = col_character())) #%>%
### decrypt with sodium at one point
df_rt_raw
### tidy data
df_rt_tidy <-
df_rt_raw %>%
### three observations from one collumn into separate collumns
separate(img, c("img", "c_dir", "filename_extension")) %>%
### create
mutate(
### fix issue with session id
session = case_when(str_count(session) <2 ~ str_c(0,session),
str_count(session) >= 2 ~ session),
### fix experiment: add version that has been run
exp = "gcc-1.0", # should be ommited in the future
### extract context
context = case_when(ctx == "modified\\face_only\\" ~ "covered",
ctx == "modified\\context\\" ~ "present"),
### round reaction time
reaction_time = round(reaction_time, 3)) %>%
# ### tidy structer of dataframe
# mutate_at(
# vars(exp:computer, block,img, t_pos:correct_response, c_dir:format, t_pos:correct_response, context:gaze_cue),
# funs(as.factor(.))) %>%
### count trials for each block
group_by(subject, block) %>% mutate(trial = 1:n()) %>% ungroup()
head(df_rt_tidy)
df_rt <-
df_rt_tidy %>%
mutate(
### identify congruency
gaze_cue = case_when(c_dir == t_pos ~ "congruent",
c_dir != t_pos ~ "incongruent"),
outlier = if_else(reaction_time < .150 | reaction_time > .800,
"yes",
"no")) %>%
### select relevant vars
select(exp, experimenter, session, subject, block, trial, img, context, gaze_cue,
correct_response, reaction_time, outlier
#, everything()
) %>%
### tidy (factorial) structer of dataframe
mutate_at(
vars(exp:experimenter, img:correct_response, outlier),
funs(factor(.))) %>%
mutate_at(vars(session:trial),
funs(factor(., ordered=TRUE)))
### write tidy data
write_tsv(df_rt, "data/data_gcc-1.0.tsv")
head(df_rt)
### if read_raw is FALSE run:
df_rt <- read_tsv("data/data_gcc-1.0.tsv") %>%
### tidy (factorial) structer of dataframe
mutate_at(
vars(exp:experimenter, img:correct_response, outlier),
funs(factor(.))) %>%
mutate_at(vars(session:trial),
funs(factor(., ordered=TRUE)))
head(df_rt)
df_rt %>% filter(correct_response=="correct") %>%
ggplot(aes(x=as.numeric(trial), y = reaction_time, color=subject)) +
geom_smooth(method = "loess") +
geom_line(aes(y=mean(reaction_time)), color="black") #+
#facet_grid(block~context)
df_rt %>%
group_by(subject) %>%
### correct responses = 1, incorrect responses = 0
summarise(percentage_correct=sum(2-as.numeric(correct_response))/n()*100) %>%
ggplot(aes(x=percentage_correct)) +
geom_bar(aes()) +
geom_vline(aes(xintercept = mean(percentage_correct)-2.5*sd(percentage_correct)), color = "red") +
geom_vline(xintercept = 70)
df_rt %>%
# filter(correct_response=="correct", outlier == "no") %>%
ggplot(aes(x=reaction_time)) +
geom_histogram(aes(fill=outlier), bins = 250) +
geom_vline(aes(xintercept = mean(reaction_time)-2.5*sd(reaction_time)), color = "red") +
geom_vline(aes(xintercept = mean(reaction_time)+2.5*sd(reaction_time)), color = "red") +
geom_vline(xintercept = .150) +
geom_vline(xintercept = .800) #+
#facet_wrap(~context)
df_rt %>% filter(correct_response=="correct", outlier=="no") %>% #group_by(gaze_cue, context) %>% summarise(mean = mean(reaction_time))
ggplot(aes(y=reaction_time, fill=gaze_cue)) +
geom_boxplot() +
facet_wrap(~context)
# library(sodium)
#
# key_string = "This is a passphrase"
# source("https://osf.io/73thx/download?version=3")
#
# # Generate keypair:
# key <- keygen()
# pub <- pubkey(key)
#
# # Encrypt message with pubkey
# msg <- serialize(iris, NULL)
# ciphertext <- simple_encrypt(msg, pub)
#
# # Decrypt message with private key
# out <- simple_decrypt(ciphertext, key)
#
# unserialize(msg)
# lmer(reaction_time ~ gaze_cue + context + (gaze_cue:context) +
# (gaze_cue|stimulus) + (context|img) + (context:gaze_cue|img) +
# (gaze_cue|experimenter) + (context|experimenter) + (gaze_cue:context|experimenter) +
# (gaze_cue|experimenter:session) + (context|experimenter:session) + (gaze_cue:context|experimenter:session) +
# (gaze_cue|session:subject) + (context|session:subject) + (gaze_cue:context|session:subject) +
# (gaze_cue|experimenter:session:subject) + (context|experimenter:session:subject) + (gaze_cue:context|experimenter:session:subject), data = df_rt)
#
# lmer(reaction_time ~ gaze_cue * context + (gaze_cue*context|img) + (gaze_cue*context|experimenter/session/subject),
# data=df_rt)