# ind_trails_opt()

The analysis of single-trials in the peak procedure was introduced by Church et al. (1994) to test some predictions of the Scalar Expectancy Theory (SET) about the variability of decision thresholds in interval timing. This analysis exhaustively searches for start and stop times such that the sum of three areas is maximized, as shown below:

Selecting the optimal start and stop times in the peak procedure requires the maximization of three areas (gray rectangles). Those areas are defined by the difference of the overall response rate r and the response rate and the rate at the i state (low r1, low r3 or high r2), multiplied by the duration of the state.*

While Church et al. (1994) suggest an exhaustive search (that is, a grid search for s1 and s2, using every combination of start and stop times as a grid subject to \(s_1\) < \(s_2\)), our implementation first defines an objective function:

\[𝑓(s_1,s_2) = \sum_{i=1}^{3} d_i|r-r_i|, \text{ for } i \in 1,2,3\]

where \(d_1\) = \(s_1\), \(d_2 = (s_2 - s_1)\), and \(d_3 = (T - s_2)\), \(T\) is the trial duration, \(r\) is the overall response rate, and \(r1,r2,r3\) the response rates corresponding to \(d_i\). The optimization problem thus becomes:

\[s_1^*, s_2^* = \mathop{\mathrm{argmax}}_{0 \leq s_1 \leq s_2 \leq T} f(s_1, s_2)\]

We obtain the optimal start and stop times using standard optimization procedures, specifically the `optim()` built-in R function, returning a data frame with the following columns:

- `start` the start time of the peak.
- `stop` the stop time of the peak.
- `spread` the spread of the peak (stop - start).
- `middle` the middle of the peak (mean of start and stop)

The `ind_trial_opt()` function takes the following parameters:

- `response_times` a numerical vector of raw response times.
- `trial_duration` numeric value for the duration of the trial.
- `optim_method` the optimization method to be used (see `optim()` for details, the default is “Nelder-Mead”).

Using the same open dataset in Buriticá & Alcalá (2019), we benchmarked the optimized implementation and found that it is between 7x to 60x faster (depending on the number of datapoints) than the exhaustive search.

However, we also provide the exhaustive search method with the `exhaustive_lhl()` function. This function takes the same parameters as `ind_trial_opt()` except `optim_method`. The output is the same as the former, with the addition of the response rate at each section of the trail (r1,r2,r3).

## Example

First let’s load a data sample of response times:

    data("r_times")

    head(r_times, n = 30)

    ##  [1] 28.1 40.7 44.2 44.4 44.7 45.0 45.4 47.9 48.1 48.3 48.6 48.8 49.8 50.2 50.7
    ## [16] 51.2 51.4 51.7 51.9 52.7 53.0 53.5 53.7 53.9 54.1 54.3 54.9 55.3 55.5 55.7

Now lets assign the trial duration value from the data set and call the function with those parameters:

    trial_duration <- max(r_times) |> ceiling() # To take the immediate superior integer.

    optim_times <- ind_trials_opt(r_times, trial_duration) # Note we used the default optimization method.

    optim_times

    ##   start stop spread middle
    ## 1  44.2 89.4   45.2   66.8

Alternatively we can also use the exhaustive method like this:

    exhaus_times <- exhaustive_lhl(r_times, trial_duration)

    exhaus_times

    ##   start stop spread       r2  mid        r1        r3
    ## 1  44.2 89.4   45.2 2.212389 66.8 0.0678733 0.3311258

Note: both methods get the same results, however, if working with big data sets we recommend using the optimized method.

Finally lets plot the results:

## References

Buriticá, J., & Alcalá, E. (2019). Increased generalization in a peak procedure after delayed reinforcement. Behavioural Processes, 169, 103978.
Church, R. M., Meck, W. H., & Gibbon, J. (1994). Application of scalar timing theory to individual trials. Journal of Experimental Psychology: Animal Behavior Processes, 20(2), 135.