dmirman-zz / gazer

Functions for reading and pre-processing eye tracking data.
16 stars 14 forks source link

Set baseline window using sample message event #10

Closed bzuck-temple closed 4 years ago

bzuck-temple commented 5 years ago

Currently baseline_window within the baseline_correction_pupil function can only take numeric values. Our group would like to use a sample message to mark the onset and offset of a baseline. Some experiments have variable baseline lengths or baselines occur at different times due to an added jitter. Thanks!

something like this:

baseline_pupil <- baseline_correction_pupil(
  rolling_mean_pupil_average, 
  pupil_colnames = "movingavgpup", 
  baseline_window = c(event= c("Baseline_Onset"), event=c("Baseline_Offset")), 
  baseline_method = "sub"
)
jgeller112 commented 5 years ago

I do not think I am going to merge this into the package just yet, but something like this will probably do the trick:

baseline_correction_pupil<-function(datafile, pupil_colnames=NULL, baseline_start=NULL, event_onset=NULL, event_offset=NULL, baseline_method="sub") { message("Calculating baseline")

baseline <- datafile %>% dplyr::group_by(subject, trial) %>% dplyr::filter(time > time[event_onset], time < time[event_offset] %>% dplyr::rename(pupil_avg = pupil_colnames) %>% dplyr::summarise(baseline = median(pupil_avg)) %>% ungroup()

message("Merging baseline") merge_baseline <- merge(baseline,datafile,all=TRUE) # merge median pupil size with raw dataset

if (baseline_method=="sub") { message("Performing subtractive baseline correction")

corrected_baseline <- merge_baseline %>% 
dplyr::rename(pupil_avg = pupil_colnames) %>% 
dplyr::mutate(baselinecorrectedp = pupil_avg - baseline) %>%
  dplyr::rename(movingavgpup = pupil_avg) %>%
  dplyr::arrange(subject, trial, time)

} if (baseline_method=="div") { message("Performing divisive baseline correction")

corrected_baseline <- merge_baseline %>% 
  dplyr::rename(pupil_avg = pupil_colnames) %>% 
  dplyr::mutate(baselinecorrectedp = (pupil_avg - baseline)/baseline) %>%
  dplyr::rename(movingavgpup = pupil_avg) %>%
  dplyr::arrange(subject, trial, time)

}
jgeller112 commented 5 years ago

The first bit of code should be:

baseline <- datafile %>%
dplyr::group_by(subject, trial) %>%
dplyr::filter(time > time[message==event_onset],
time < time[message==event_offset] %>%
dplyr::rename(pupil_avg = pupil_colnames) %>%
dplyr::summarise(baseline = median(pupil_avg)) %>%
ungroup()
bzuck-temple commented 5 years ago

Hi Jason,

Thanks for this. I've been trying to get it working but am getting hung up (I believe) with the baseline code. I changed time[message==event_onset] to time[sample_message==event_onset]. I assumed you were calling the column with the messages from eyelink. When I run this code outside of the function the baseline dataframe is empty.

Additionally, is anything intended for the argument: baseline_start=NULL found in the function? I didn't see it in the code.

I will keep working on this myself but if you can see anything that stands out let me know.

Thanks again for your help, Bonnie

On Sat, May 11, 2019 at 2:59 PM jgeller112 notifications@github.com wrote:

The first bit of code should be:

baseline <- datafile %>% dplyr::group_by(subject, trial) %>% dplyr::filter(time > time[message==event_onset], time < time[message==event_offset] %>% dplyr::rename(pupil_avg = pupil_colnames) %>% dplyr::summarise(baseline = median(pupil_avg)) %>% ungroup()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dmirman/gazer/issues/10#issuecomment-491535943, or mute the thread https://github.com/notifications/unsubscribe-auth/ALVSILRXIVRMMTTSZ2WNLVLPU4JRXANCNFSM4HLKZ56A .

-- Bonnie M Zuckerman, MS Temple University www.reilly-coglab.com

jgeller112 commented 5 years ago

Sorry about that! I didn’t test the code before posting. I started to write the function, but decided against it in lieu of writing something specific for you. This is why the ‘baseline_start’ argument is not defined. If you solve it before I get to it, please post it here :).

bzuck-temple commented 5 years ago

Hi, We managed to get this working! Thank you again. We found that, with both filtering the outliers and MAD, rows with our sample messages were being removed. Is there something wrong with changing the items that normally are removed to NAs?

jgeller112 commented 5 years ago

I am glad you got this working! I think that is a great suggestion.

jgeller112 commented 4 years ago

Sorry for the delay! I think you were using an older version of the paper. The newest version on PsyArXiv does filtering after baseline correction which eliminates that issue you mentioned.