{setup_include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

load packages

library(R.matlab)
library(tidyverse)
library(janitor)
library(here)
library(skimr)
library(ggbeeswarm)

read .mat files

Leaving out p1 because it doesn’t seem to have the right number of observations

dataP2 <- readMat(here("data", "mat", "ArianaDotProbe_SpectrumP2.mat")) 
dataP3 <- readMat(here("data", "mat", "ArianaDotProbe_SpectrumP3.mat")) 
dataP4 <- readMat(here("data", "mat", "ArianaDotProbe_SpectrumP4.mat")) 
dataP5 <- readMat(here("data", "mat", "ArianaDotProbe_SpectrumP5.mat")) 

Trying to work out how to get raw trial data to read straight from MatLab, need to work out how to read element 9 from this list. Ah ha, so .mat reads in as a nested list, top level = data, next level = DATA. We want the 9th element from the DATA list. For each file, get 9th element, make it a df, and write it to csv to make it easy to read in all at once.

trialsP2 <-  dataP2[["DATA"]][[9]] %>%
  as.data.frame() %>%
  write_csv(here("data", "csv", "dotp2.csv"))

trialsP3 <-  dataP3[["DATA"]][[9]] %>%
  as.data.frame() %>%
  write_csv(here("data", "csv", "dotp3.csv"))
  

trialsP4 <-  dataP4[["DATA"]][[9]] %>%
  as.data.frame() %>% 
  write_csv(here("data", "csv", "dotp4.csv"))

trialsP5 <-  dataP5[["DATA"]][[9]] %>%
  as.data.frame() %>% 
  write_csv(here("data", "csv", "dotp5.csv"))

read in ALL the csvs

More detailed notes about this time saving method here http://jenrichmond.rbind.io/post/use-map-to-read-many-csv-files/

data_path <- "./data/csv"   # tell R where the data is

files <- dir(data_path, pattern = "*.csv")  # get a list of all the csvs at that location

# use map from the purrr package pull the contents of all the csvs into a single dataframe

data <- data_frame(filename = files) %>%
  mutate(file_contents = map(filename,      
           ~ read_csv(file.path(data_path, .))))  

trials <- unnest(data) # then unnest that to get a dataframe with all participants files combined- MAGIC!!

rename the variables V1-V24 in the dotprobe data.

renamed <- trials %>%
  rename(pp_no = filename,
    trial = V1,
  stim_emo = V2,
  stim_rel = V3,
    probe_position = V4,
      fix_timeout = V5,
      fix_duration = V6,
      fix_propgood = V7,
      face_propgood = V8,
      probe_propgood = V9,
      face_duration = V10,
      probe_duration = V11,
      probe_timeout = V12,
      first_stim_select =V13,
      last_stim_select = V14,
      probe_select = V15,
      probe_rt = V16,
      time_probe = V17,
      time_l_eyes = V18,
      time_l_mouth = V19,
      time_r_eyes = V20,
      time_r_mouth = V21,
      time_l_face = V22,
      time_r_face = V23,
      time_elsewhere = V24) 


renamed$pp_no <- str_sub(renamed$pp_no, end = -5) #drop .csv from filename to make pp_no
glimpse(renamed)
## Observations: 432
## Variables: 25
## $ pp_no             <chr> "dotp2", "dotp2", "dotp2", "dotp2", "dotp2",...
## $ trial             <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1...
## $ stim_emo          <int> 4, 8, 9, 1, 7, 4, 9, 6, 5, 7, 8, 7, 6, 1, 3,...
## $ stim_rel          <int> 2, 2, 2, 1, 1, 1, 1, 1, 2, 2, 1, 2, 2, 2, 2,...
## $ probe_position    <int> 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 1,...
## $ fix_timeout       <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ fix_duration      <dbl> 0.2833422, 0.2167386, 0.4167768, 0.2167205, ...
## $ fix_propgood      <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ face_propgood     <dbl> 1.0000000, 1.0000000, 1.0000000, 1.0000000, ...
## $ probe_propgood    <dbl> 1.0000000, 0.9687500, 1.0000000, 1.0000000, ...
## $ face_duration     <dbl> 1.016735, 1.016742, 1.016743, 1.016969, 1.01...
## $ probe_duration    <dbl> 0.4834606, 0.4834355, 0.4833941, 0.4832441, ...
## $ probe_timeout     <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ first_stim_select <int> 3, 6, 6, 5, 5, 5, 6, 3, 5, 3, 5, 3, 3, 3, 1,...
## $ last_stim_select  <int> 1, 1, 3, 3, 1, 3, 6, 3, 1, 7, 7, 1, 3, 1, 1,...
## $ probe_select      <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ probe_rt          <dbl> 0.3687239, 0.2313045, 0.0035462, 0.0035258, ...
## $ time_probe        <dbl> 0.1066667, 0.1033333, 0.1000000, 0.1033333, ...
## $ time_l_eyes       <dbl> 0.1416667, 0.2583333, 0.0000000, 0.3183333, ...
## $ time_l_mouth      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ time_r_eyes       <dbl> 0.5483333, 0.4100000, 0.7116667, 0.3283333, ...
## $ time_r_mouth      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ time_l_face       <dbl> 0.008333333, 0.000000000, 0.000000000, 0.015...
## $ time_r_face       <dbl> 0.000000000, 0.011666667, 0.013333333, 0.010...
## $ time_elsewhere    <dbl> 0.3050000, 0.3200000, 0.2783333, 0.3233333, ...

At the moment stim_emo is not tidy. It contains information about both what emotion was displayed and in what position. We need to make that info appear in two separate variables.

First less replace the nubmers 1-9 with something meaningful.

1 = An_L_ 2 = An_R_ 3 = Ha_L_ 4 = Ha_R_ 5 = Sd_L_ 6 = Sd_R_ 7 = Fe_L_ 8 = Fe_R_ 9 = Ne_

This chunk takes the renamed data, creates a new variable called stim_emo as per info above, then separates info in that column into separate emotion and position columns and uses select to pull those two new columns to the front of the list.

emo_pos <- renamed %>%
  mutate(emo_pos = ifelse(stim_emo == 1, "An_L", 
                          ifelse(stim_emo == 2, "An_R", 
                          ifelse(stim_emo == 3, "Ha_L", 
                          ifelse(stim_emo == 4, "Ha_R", 
                          ifelse(stim_emo == 5, "Sd_L", 
                          ifelse(stim_emo == 6, "Sd_R", 
                          ifelse(stim_emo == 7, "Fe_L", 
                          ifelse(stim_emo == 8, "Fe_R", 
                          ifelse(stim_emo == 9, "Ne_NA", "other")))))))))) %>%
                separate(emo_pos, c("emotion", "emo_pos")) %>%
                  select(pp_no, trial, emotion, emo_pos, 2:26)

Look at the top few rows

head(emo_pos)
## # A tibble: 6 x 27
##   pp_no trial emotion emo_pos stim_emo stim_rel probe_position fix_timeout
##   <chr> <int> <chr>   <chr>      <int>    <int>          <int>       <int>
## 1 dotp2     1 Ha      R              4        2              2           0
## 2 dotp2     2 Fe      R              8        2              2           0
## 3 dotp2     3 Ne      NA             9        2              2           0
## 4 dotp2     4 An      L              1        1              2           0
## 5 dotp2     5 Fe      L              7        1              2           0
## 6 dotp2     6 Ha      R              4        1              1           0
## # ... with 19 more variables: fix_duration <dbl>, fix_propgood <dbl>,
## #   face_propgood <dbl>, probe_propgood <dbl>, face_duration <dbl>,
## #   probe_duration <dbl>, probe_timeout <int>, first_stim_select <int>,
## #   last_stim_select <int>, probe_select <int>, probe_rt <dbl>,
## #   time_probe <dbl>, time_l_eyes <dbl>, time_l_mouth <dbl>,
## #   time_r_eyes <dbl>, time_r_mouth <dbl>, time_l_face <dbl>,
## #   time_r_face <dbl>, time_elsewhere <dbl>

Create a new column called congruence

If the Emotion stimulus is on the R and probe position is 2 (i.e. R), then congruent etc. Fill in congruence column and then use select to pull congruence next to probe_position.

congruence <- emo_pos %>%
  mutate(congruence = ifelse(emo_pos == "R" & probe_position == 2, "congruent", 
                             ifelse(emo_pos == "L" & probe_position == 2, "incongruent", 
                             ifelse(emo_pos == "R" & probe_position == 1, "incongruent",
                             ifelse(emo_pos == "L" & probe_position == 1, "congruent", "no"))))) %>%
  select(pp_no, trial, emotion, emo_pos, stim_emo, stim_rel, probe_position, congruence, 7:26)
names(congruence)
##  [1] "pp_no"             "trial"             "emotion"          
##  [4] "emo_pos"           "stim_emo"          "stim_rel"         
##  [7] "probe_position"    "congruence"        "fix_timeout"      
## [10] "fix_duration"      "fix_propgood"      "face_propgood"    
## [13] "probe_propgood"    "face_duration"     "probe_duration"   
## [16] "probe_timeout"     "first_stim_select" "last_stim_select" 
## [19] "probe_select"      "probe_rt"          "time_probe"       
## [22] "time_l_eyes"       "time_l_mouth"      "time_r_eyes"      
## [25] "time_r_mouth"      "time_l_face"       "time_r_face"

Summary stats

Key DV- we expect that probe RT will be slower for incongruent than for congruent trials. For kids that might also differ by stim_rel (relationship), but in this data it shouldn’t. Filter out trials where they didn’t fixate the probe.

#summary averaged across pp_no
summary <- congruence %>%
  filter(probe_timeout != 1) %>%
  group_by(emotion, congruence) %>%
  summarise(meanrt = mean(probe_rt), sd = sd(probe_rt), n = n(), stderr = sd/sqrt(n))

#summary by pp_no
summary_pp <- congruence %>%
  filter(probe_timeout != 1) %>%
  group_by(pp_no, emotion, congruence) %>%
  summarise(meanrt = mean(probe_rt), sd = sd(probe_rt), n = n(), stderr = sd/sqrt(n))

RT plots averaged across 4 participants (Filtering out neutral)

summary %>%
  filter(emotion != "Ne") %>%
  ggplot(aes(x = emotion, y = meanrt, fill = congruence)) +
  geom_col(position = "dodge") 

RT plots per participant (Filtering out neutral)

summary_pp %>%
  filter(emotion != "Ne") %>%
  ggplot(aes(x = emotion, y = meanrt, fill = congruence)) +
  geom_col(position = "dodge") +
  facet_wrap(~ pp_no)

what is going on with p5 happy??

Just a couple of REALLY slow trials, need to think about outliers??

congruence %>%
  filter(emotion != "Ne") %>%
  filter(pp_no == "dotp5") %>%
  ggplot(aes(x = emotion, y = probe_rt, colour = congruence)) +
  geom_quasirandom()