-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update behavioral preprocessing notebook with relying on new script s…
…ource
- Loading branch information
1 parent
a01d2cb
commit 59ab527
Showing
7 changed files
with
173 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#' @title Get hit times vector from signals and responses per participant | ||
#' @description This function uses input of both signal times and response times | ||
#' from a single participant's extracted behavioral data to determine the hit | ||
#' indices, if any, and in turn, the hit times. | ||
#' Get hit timestamp from a vector of signal times and a vector of response times | ||
#' @note Use the .interval variable if 8 seconds is not the desired interval | ||
#' @export | ||
get_hit_times <- function(signal_times, response_times, .interval = 8.0) { | ||
signal_times %>% | ||
map_dbl(function(signal_time) { | ||
|
||
hit_index <- first(which( | ||
response_times %>% | ||
map_lgl(~ between(.x, signal_time, signal_time + .interval)), | ||
arr.ind = TRUE | ||
)) | ||
|
||
hit_time <- response_times[hit_index] | ||
|
||
return(hit_time) | ||
|
||
}) | ||
} | ||
|
||
#' @title Get all participants' hits with reaction times | ||
#' @description Creates a dataframe composed of each participant's hit times and | ||
#' reaction times for those hits, row-by-row with signal times. | ||
#' @export | ||
get_all_hits_with_reaction_times <- function(participants, combined_df) { | ||
|
||
# Extract only rows where a signal is present | ||
all_signals_df <- combined_df %>% | ||
filter(is_signal == 1) %>% | ||
mutate( | ||
signal_time = step_time | ||
) %>% | ||
select(trial, id, image_index, signal_time) | ||
|
||
# Extract only rows where a response attempt is present | ||
all_responses_df <- combined_df %>% | ||
filter(is_response == 1) %>% | ||
select(trial, id, image_index, resp_time) | ||
|
||
# Map over the unlisted participants' ids to get the per-participant | ||
# signals and responses, then return a combined dataframe of all participant | ||
# including trial rows for signals, and if it exists, hit time and reaction time | ||
map_dfr(unlist(participants), function(participant) { | ||
signals <- all_signals_df %>% | ||
filter(id == participant) | ||
|
||
responses <- all_responses_df %>% | ||
filter(id == participant) | ||
|
||
signals %>% mutate( | ||
hit_time = get_hit_times(signals$signal_time, responses$resp_time), | ||
reaction_time = hit_time - signal_time | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.