library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(here)
## here() starts at /Users/caoanjie/Desktop/projects/MB5_AP/data_analysis
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
## 
##     flatten
source(here("helper/webgazer_helper.r"))

merged_d <- read_csv(here("data/01_merged_data.csv"))
## Rows: 656 Columns: 30
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (20): failed_images, failed_audio, failed_video, trial_type, internal_no...
## dbl  (8): trial_index, time_elapsed, window_width, window_height, load_time,...
## lgl  (2): success, timeout
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

get data set

merged_d %>% 
  distinct(subject) %>% 
  count()
## # A tibble: 1 × 1
##       n
##   <int>
## 1     8

get a tidy df for gaze info on the fam phase

fam_phase_target_info_df <- get_fam_phase_target_info(merged_d)
fam_phase_gaze_info_df <- get_fam_phase_gaze_info(merged_d, fam_phase_target_info_df, smooth = 50)

get a tidy df for gaze info on paired preferece phase

pref_phase_target_info_df <- get_pref_phase_target_info(merged_d)
pref_phase_gaze_info_df <- get_pref_phase_gaze_info(merged_d, pref_phase_target_info_df, smooth = 50)

put the two dataframes together

gaze_info_df <- fam_phase_gaze_info_df %>% 
  mutate(phase = "fam") %>% 
  bind_rows(pref_phase_gaze_info_df %>% mutate(phase = "pref")) %>% 
  group_by(subject, block_number,phase) %>% 
  mutate(dwell_time = t - lag(t))

visualizing dwell time for target

gaze_info_df %>% 
  filter(phase == "fam") %>% 
  group_by(block_number, gaze_location, exposure_time,
           complexity, subject) %>% 
  summarise(sum_dwell_time = sum(dwell_time, na.rm = TRUE)) %>% 
  ggplot(aes(x = gaze_location, 
              y = sum_dwell_time, 
             color = complexity)) + 
  stat_summary(fun.data = "mean_cl_boot", position = position_dodge(width = .3)) + 
  geom_jitter(width = 0.2, alpha = .3) + 
facet_grid(~exposure_time)
## `summarise()` has grouped output by 'block_number', 'gaze_location',
## 'exposure_time', 'complexity'. You can override using the `.groups` argument.

## visualizing dwell time for pref phase

gaze_info_df %>% 
  filter(phase == "pref") %>% 
  group_by(block_number, exposure_time,
           complexity, gaze_location_type, subject) %>% 
  summarise(sum_dwell_time = sum(dwell_time, na.rm = TRUE)) %>% 
  filter(gaze_location_type != "not_on_target") %>% 
  ggplot(aes(x = gaze_location_type, 
              y = sum_dwell_time)) + 
  stat_summary(fun.data = "mean_cl_boot", position = position_dodge(width = .3)) + 
  geom_jitter(width = 0.2, alpha = .3) + 
facet_grid(~exposure_time)
## `summarise()` has grouped output by 'block_number', 'exposure_time',
## 'complexity', 'gaze_location_type'. You can override using the `.groups`
## argument.