# AAT - Double Difference of Medians

    library(splithalfr)

This vignette describes a scoring method similar to Heuer, Rinck, and Becker (2007); double difference of median reaction times (RTs) for correct responses on Approach Avoidance Task data. It is a subtraction comparing approach bias towards test stimuli relative to approach bias towards control stimuli (avoid_test - approach_test) - (avoid_control - approach_control).

# Dataset

Load the included AAT dataset and inspect its documentation.

    data("ds_aat", package = "splithalfr")
    ?ds_aat

## Relevant variables

The columns used in this example are:

- UserID, which identifies participants
- block_type, in order to select only assessment blocks
- appr, approach or avoid trial
- cat, in order to compare test and control stimuli
- response, in order to select only correct first responses
- rt, in order to calculate medians for avoid_test, approach_test, avoid_control, and approach_control
- stim, stimulus ID

## Preprocessing

Only select trials from assessment blocks.

    ds_aat <- subset(ds_aat, block_type == "assess")

## Counterbalancing

The variables `appr` and `stim` were counterbalanced. Below we illustrate this for the first participant.

    ds_1 <- subset(ds_aat, UserID == 1)
    table(ds_1$appr, ds_1$stim)

# Scoring the AAT

## Scoring function

The scoring function calculates the score of a single participant as follows:

1. selects only correct responses
2. calculates the median RT of remaining responses

    fn_score <- function (ds) {
      median_avoid_test <- median(
        ds[ds$appr == "no" & ds$cat == "test" & ds$response == 1, ]$rt
      )
      median_approach_test <- median(
        ds[ds$appr == "yes" & ds$cat == "test" & ds$response == 1, ]$rt
      )
      median_avoid_control <- median(
        ds[ds$appr == "no" & ds$cat == "control" & ds$response == 1, ]$rt
      )
      median_approach_control <- median(
        ds[ds$appr == "yes" & ds$cat == "control" & ds$response == 1, ]$rt
      )
      return (
        (median_avoid_test - median_approach_test) -
        (median_avoid_control - median_approach_control)
      )
    }

## Scoring a single participant

Let’s calculate the AAT score for the participant with UserID 14. NB - This score has also been calculated manually via Excel in the splithalfr repository.

    fn_score(subset(ds_aat, UserID == 14))

## Scoring all participants

To calculate the AAT score for each participant, we will use R’s native `by` function and convert the result to a data frame.

    scores <- by(
      ds_aat,
      ds_aat$UserID,
      fn_score
    )
    data.frame(
      UserID = names(scores),
      score = as.vector(scores)
    )

# Estimating split-half reliability

## Calculating split scores

To calculate split-half scores for each participant, use the function `by_split`. The first three arguments of this function are the same as for `by`. An additional set of arguments allow you to specify how to split the data and how often. In this vignette we will calculate scores of 1000 permutated splits. The trial properties `app` and `stim` were counterbalanced in the AAT design. We will stratify splits by these trial properties. See the vignette on splitting methods for more ways to split the data.

The `by_split` function returns a data frame with the following columns:

- `participant`, which identifies participants
- `replication`, which counts replications
- `score_1` and `score_2`, which are the scores calculated for each of the split datasets

Calculating the split scores may take a while. By default, `by_split` uses all available CPU cores, but no progress bar is displayed. Setting `ncores = 1` will display a progress bar, but processing will be slower.

    split_scores <- by_split(
      ds_aat,
      ds_aat$UserID,
      fn_score,
      replications = 1000,
      stratification = paste(ds_aat$app, ds_aat$stim)
    )

## Calculating reliability coefficients

Next, the output of `by_split` can be analyzed in order to estimate reliability. By default, functions are provided that calculate Spearman-Brown adjusted Pearson correlations (`spearman_brown`), Flanagan-Rulon (`flanagan_rulon`), Angoff-Feldt (`angoff_feldt`), and Intraclass Correlation (`short_icc`) coefficients. Each of these coefficient functions can be used with `split_coef` to calculate the corresponding coefficients per split, which can then be plotted or averaged, for instance via a simple `mean`.

    # Spearman-Brown adjusted Pearson correlations per replication
    coefs <- split_coefs(split_scores, spearman_brown)
    # Distribution of coefficients
    hist(coefs)
    # Mean of coefficients
    mean(coefs)

## Calculating bootstrapped confidence intervals for population reliability coefficient

Finally, we can estimate the Calculate bootstrapped confidence intervals for the value of the reliability coefficient in the population by bootstrapping participants. For this, we’ll need to repeatedly sample participants from the population, calculate a collection of reliability coefficients between the split scores of that sample of participants, and average those coefficients together. Hence, the call to `split_ci` below, takes (1) the split scores produced by calling `by_split` (`split_scores`), (2) the reliability coefficient we used above (`spearman_brown`), and (3) the method for averaging coefficients we used above (`mean`).

The bootstrap can take even longer than the split, and doesn’t show any progress bar, but it also uses all available CPU cores by default.

    # Conduct a bootstrap (of participants)
    bootstrap_result <- split_ci(split_scores, spearman_brown, mean)
    # Report confidence intervals
    library(boot)
    print(boot.ci(bootstrap_result, type="bca"))