Skip to content

Commit

Permalink
update function for getting hits with index tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
aridyckovsky committed May 11, 2021
1 parent 59ab527 commit 87f2c4b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
23 changes: 21 additions & 2 deletions R/behavioral_data_preprocessing.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,36 @@
#' @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) {

# Create vector to hold hit indices
hit_indices <- c()

signal_times %>%
map_dbl(function(signal_time) {

hit_index <- first(which(
# Find indices for potential hits within the signal interval
potential_hit_indices <- which(
response_times %>%
map_lgl(~ between(.x, signal_time, signal_time + .interval)),
arr.ind = TRUE
))
)

# If there are zero potential hits for this signal time, return NA
if (!length(potential_hit_indices)) return(NA)

# Remove existing hit indices from potential hit indices for this signal
potential_hit_indices <- setdiff(potential_hit_indices, hit_indices)

# Extract the first hit index (closest to signal time)
hit_index <- first(potential_hit_indices)

# Extract hit time from response times using hit index
hit_time <- response_times[hit_index]

# Update vector of hit indices
hit_indices <- c(hit_indices, hit_index)

# Return the hit time
return(hit_time)

})
Expand Down
4 changes: 2 additions & 2 deletions notebooks/behavioral_data_preprocessing.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ combined_df %>%
summarise(mean(n), sd(n), min(n), max(n))
```

## Function definitions
## Combined hits dataframe for all participants

The function to get each participant's hit times vector is `itrackvalr::get_hit_times`, and the function to get a dataframe of all participants' hits is `itrackvalr::get_all_hits_with_reaction_times`, including each hit time and reaction time between that hit time and the nearest signal prompting a response within a fixed interval.

## Get the combined hits using the function
Get the combined hits using the function:

```{r}
combined_hits_df <- get_all_hits_with_reaction_times(participants, combined_df)
Expand Down
7 changes: 3 additions & 4 deletions notebooks/behavioral_data_preprocessing.html
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,7 @@ <h1 id="behavioral-data-preprocessing">Behavioral Data Preprocessing</h1>
<ul>
<li><a href="#load-extracted-csv-files">Load extracted CSV files</a></li>
<li><a href="#sanity-checks">Sanity checks</a></li>
<li><a href="#function-definitions">Function definitions</a></li>
<li><a href="#get-the-combined-hits-using-the-function">Get the combined hits using the function</a></li>
<li><a href="#combined-hits-dataframe-for-all-participants">Combined hits dataframe for all participants</a></li>
<li><a href="#check-out-a-quick-preview-of-the-table-of-hits">Check out a quick preview of the table of hits</a></li>
<li><a href="#check-out-the-reaction-time-summary-statistics-by-id">Check out the reaction time summary statistics by id:</a></li>
<li><a href="#gut-check-plot-of-reaction-times-by-signal-times">Gut-check plot of reaction times by signal times</a></li>
Expand Down Expand Up @@ -645,9 +644,9 @@ <h2 id="sanity-checks">Sanity checks</h2>
## &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;int&gt;
## 1 37.3 32.9 4 170
</code></pre>
<h2 id="function-definitions">Function definitions</h2>
<h2 id="combined-hits-dataframe-for-all-participants">Combined hits dataframe for all participants</h2>
<p>The function to get each participant’s hit times vector is <code>itrackvalr::get_hit_times</code>, and the function to get a dataframe of all participants’ hits is <code>itrackvalr::get_all_hits_with_reaction_times</code>, including each hit time and reaction time between that hit time and the nearest signal prompting a response within a fixed interval.</p>
<h2 id="get-the-combined-hits-using-the-function">Get the combined hits using the function</h2>
<p>Get the combined hits using the function:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1"></a>combined_hits_df &lt;-<span class="st"> </span><span class="kw">get_all_hits_with_reaction_times</span>(participants, combined_df)</span></code></pre></div>
<h2 id="check-out-a-quick-preview-of-the-table-of-hits">Check out a quick preview of the table of hits</h2>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1"></a>knitr<span class="op">::</span><span class="kw">kable</span>(<span class="kw">head</span>(combined_hits_df, <span class="dv">10</span>))</span></code></pre></div>
Expand Down
9 changes: 4 additions & 5 deletions notebooks/behavioral_data_preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ Ari Dyckovsky

- [Load extracted CSV files](#load-extracted-csv-files)
- [Sanity checks](#sanity-checks)
- [Function definitions](#function-definitions)
- [Get the combined hits using the
function](#get-the-combined-hits-using-the-function)
- [Combined hits dataframe for all
participants](#combined-hits-dataframe-for-all-participants)
- [Check out a quick preview of the table of
hits](#check-out-a-quick-preview-of-the-table-of-hits)
- [Check out the reaction time summary statistics by
Expand Down Expand Up @@ -70,15 +69,15 @@ combined_df %>%
## <dbl> <dbl> <int> <int>
## 1 37.3 32.9 4 170

## Function definitions
## Combined hits dataframe for all participants

The function to get each participant’s hit times vector is
`itrackvalr::get_hit_times`, and the function to get a dataframe of all
participants’ hits is `itrackvalr::get_all_hits_with_reaction_times`,
including each hit time and reaction time between that hit time and the
nearest signal prompting a response within a fixed interval.

## Get the combined hits using the function
Get the combined hits using the function:

``` r
combined_hits_df <- get_all_hits_with_reaction_times(participants, combined_df)
Expand Down

0 comments on commit 87f2c4b

Please sign in to comment.