IAT-example

library(implicitMeasures)

This vignette illustrates how to use the implicitMeasures package for computing the IAT D score. The illustration is based on the data set raw_data that comes with the package.

First thing first: Import and explore data

Labels containing specification .iat in variable blockcode identify IAT blocks.

data("raw_data")
# explore the dataframe
str(raw_data)
#> 'data.frame':    84726 obs. of  6 variables:
#>  $ Participant: int  4 4 4 4 4 4 4 4 4 4 ...
#>  $ latency    : int  2592 628 808 783 2059 1114 608 663 771 676 ...
#>  $ correct    : int  1 1 1 1 1 1 1 1 1 1 ...
#>  $ trialcode  : Factor w/ 32 levels "age","alert",..: 31 5 3 20 3 20 5 3 20 3 ...
#>  $ blockcode  : Factor w/ 13 levels "demo","practice.iat.Milkbad",..: 4 4 4 4 4 4 4 4 4 4 ...
#>  $ response   : Factor w/ 46 levels "","0","1","19",..: 43 43 43 43 43 43 43 43 43 43 ...
# explore the levels of the blockcode variable to identify the IAT blocks
levels(raw_data$blockcode)
#>  [1] "demo"                      "practice.iat.Milkbad"     
#>  [3] "practice.iat.Milkgood"     "practice.sc_dark.Darkbad" 
#>  [5] "practice.sc_dark.Darkgood" "practice.sc_milk.Milkbad" 
#>  [7] "practice.sc_milk.Milkgood" "test.iat.Milkbad"         
#>  [9] "test.iat.Milkgood"         "test.sc_dark.Darkbad"     
#> [11] "test.sc_dark.Darkgood"     "test.sc_milk.Milkbad"     
#> [13] "test.sc_milk.Milkgood"

Once the IAT blocks have been identified, it is possible to clean the IAT data by using the clean_iat() function. Since the data set also includes respondents’ demographic information (demo in the blockcode variable), it is possible to extract and store these information in a separate data frame:

iat_cleandata <- clean_iat(raw_data, sbj_id = "Participant",
                          block_id = "blockcode",
                          mapA_practice = "practice.iat.Milkbad",
                          mapA_test = "test.iat.Milkbad",
                          mapB_practice = "practice.iat.Milkgood",
                          mapB_test = "test.iat.Milkgood",
                          latency_id = "latency",
                          accuracy_id = "correct",
                          trial_id = "trialcode",
                          trial_eliminate = c("reminder", "reminder1"),
                          demo_id = "blockcode",
                          trial_demo = "demo")

Since also the demographic data has been specified, clean_iat() results in a list of 3 elements:

str(iat_cleandata)
#> List of 3
#>  $ data_keep     :Classes 'iat_clean' and 'data.frame':  19440 obs. of  8 variables:
#>   ..$ participant   : int [1:19440] 4 4 4 4 4 4 4 4 4 4 ...
#>   ..$ latency       : int [1:19440] 1282 1299 1435 1089 967 648 967 615 729 642 ...
#>   ..$ correct       : int [1:19440] 1 1 1 1 1 1 1 1 1 1 ...
#>   ..$ block_original: chr [1:19440] "practice.iat.Milkbad" "practice.iat.Milkbad" "practice.iat.Milkbad" "practice.iat.Milkbad" ...
#>   ..$ condition     : chr [1:19440] "MappingA" "MappingA" "MappingA" "MappingA" ...
#>   ..$ block_pool    : chr [1:19440] "practice" "practice" "practice" "practice" ...
#>   ..$ block         : chr [1:19440] "practice_MappingA" "practice_MappingA" "practice_MappingA" "practice_MappingA" ...
#>   ..$ trial_id      : Factor w/ 32 levels "age","alert",..: 20 6 20 6 20 6 20 6 20 23 ...
#>  $ data_eliminate:'data.frame':  64638 obs. of  6 variables:
#>   ..$ Participant: int [1:64638] 4 4 4 4 4 4 4 4 4 4 ...
#>   ..$ latency    : int [1:64638] 2592 628 808 783 2059 1114 608 663 771 676 ...
#>   ..$ correct    : int [1:64638] 1 1 1 1 1 1 1 1 1 1 ...
#>   ..$ trialcode  : Factor w/ 32 levels "age","alert",..: 31 5 3 20 3 20 5 3 20 3 ...
#>   ..$ blockcode  : Factor w/ 13 levels "demo","practice.iat.Milkbad",..: 4 4 4 4 4 4 4 4 4 4 ...
#>   ..$ response   : Factor w/ 46 levels "","0","1","19",..: 43 43 43 43 43 43 43 43 43 43 ...
#>  $ demo          :'data.frame':  3726 obs. of  6 variables:
#>   ..$ participant: int [1:3726] 4 4 4 4 4 4 4 4 4 4 ...
#>   ..$ latency    : int [1:3726] 53047 53047 53047 53047 21554 21554 11266 11266 11266 11266 ...
#>   ..$ correct    : int [1:3726] 1 1 1 1 1 1 1 1 1 1 ...
#>   ..$ trialcode  : Factor w/ 32 levels "age","alert",..: 19 1 21 8 22 4 10 11 12 13 ...
#>   ..$ blockcode  : Factor w/ 13 levels "demo","practice.iat.Milkbad",..: 1 1 1 1 1 1 1 1 1 1 ...
#>   ..$ response   : Factor w/ 46 levels "","0","1","19",..: 33 15 41 34 16 20 1 1 1 1 ...

data_keep is a data.frame with class iat_clean. It contains the data set for the compute_iat() function.

data_eliminate is a data.frame that contains all the discarded blocks and trials.

demo is a data.frame that contains all the trials identified as demo in the blockcode variable.

Store the first data_keep element in a data frame for the compute_iat() function.

iat_data <- iat_cleandata[[1]]
head(iat_data)
#>       participant latency correct       block_original condition block_pool
#> 60914           4    1282       1 practice.iat.Milkbad  MappingA   practice
#> 60915           4    1299       1 practice.iat.Milkbad  MappingA   practice
#> 60916           4    1435       1 practice.iat.Milkbad  MappingA   practice
#> 60917           4    1089       1 practice.iat.Milkbad  MappingA   practice
#> 60918           4     967       1 practice.iat.Milkbad  MappingA   practice
#> 60919           4     648       1 practice.iat.Milkbad  MappingA   practice
#>                   block  trial_id
#> 60914 practice_MappingA goodright
#> 60915 practice_MappingA darkright
#> 60916 practice_MappingA goodright
#> 60917 practice_MappingA darkright
#> 60918 practice_MappingA goodright
#> 60919 practice_MappingA darkright

Compute IAT D score

Once that IAT data have been cleaned with the clean_iat() function, it is possible to compute the D score by using the compute_iat() function.

This function only takes two arguments. The first argument is the data frame with class iat_clean, the second argument is a character specifying the D score algorithm for the computation. To compute multiple D score algorithms at the same time, use themulti_dscore() function.

dscore <- compute_iat(iat_data, Dscore = "d3")
str(dscore)
#> Classes 'dscore' and 'data.frame':   162 obs. of  23 variables:
#>  $ participant               : int  4 6 8 11 14 17 18 19 20 21 ...
#>  $ n_trial                   : int  120 120 120 120 120 120 120 120 120 120 ...
#>  $ nslow10000                : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ nfast400                  : num  0 0 0 0.01 0.07 0.04 0 0.05 0 0 ...
#>  $ nfast300                  : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ accuracy.practice_MappingA: num  1 0.95 0.7 0.9 0.95 0.9 0.95 0.95 0.85 0.9 ...
#>  $ accuracy.practice_MappingB: num  1 1 0.85 0.95 1 1 1 0.85 1 0.85 ...
#>  $ accuracy.test_MappingA    : num  1 0.9 1 0.95 0.9 0.925 0.95 0.95 0.8 0.9 ...
#>  $ accuracy.test_MappingB    : num  1 1 0.85 0.95 1 0.95 1 0.975 0.975 0.9 ...
#>  $ accuracy.MappingA         : num  1 0.917 0.9 0.933 0.917 ...
#>  $ accuracy.MappingB         : num  1 1 0.85 0.95 1 ...
#>  $ RT_mean.MappingA          : num  1037 1179 954 1297 852 ...
#>  $ RT_mean.MappingB          : num  788 596 926 731 604 ...
#>  $ mean_practice_MappingA    : num  1070 1224 1504 1608 821 ...
#>  $ mean_test_MappingA        : num  1021 1157 679 1141 867 ...
#>  $ mean_practice_MappingB    : num  889 611 1108 702 637 ...
#>  $ mean_test_MappingB        : num  737 589 836 745 587 ...
#>  $ d_practice_d3             : num  -0.647 -1.274 -0.584 -1.114 -0.514 ...
#>  $ d_test_d3                 : num  -0.878 -1.226 0.508 -0.816 -0.672 ...
#>  $ dscore_d3                 : num  -0.762 -1.25 -0.038 -0.965 -0.593 ...
#>  $ cond_ord                  : chr  "MappingA_First" "MappingB_First" "MappingB_First" "MappingA_First" ...
#>  $ legendMappingA            : chr  "practice.iat.Milkbad_and_test.iat.Milkbad" "practice.iat.Milkbad_and_test.iat.Milkbad" "practice.iat.Milkbad_and_test.iat.Milkbad" "practice.iat.Milkbad_and_test.iat.Milkbad" ...
#>  $ legendMappingB            : chr  "practice.iat.Milkgood_and_test.iat.Milkgood" "practice.iat.Milkgood_and_test.iat.Milkgood" "practice.iat.Milkgood_and_test.iat.Milkgood" "practice.iat.Milkgood_and_test.iat.Milkgood" ...

The compute_iat() function results in a data.frame with class dscore containing a number of rows equal to the number of participants. The columns contain the D score and otehr useful information on the performance of each respondent (see the documentation of the compute_iat() function for further details). The IAT_rel(), descript_d(), d_point(), and d_density() functions require the object resulting from function compute_iat().

IAT descriptive statistics and reliability

The descriptive statistics of the D scores computed on the practice and test blocks, and the actual D scores can be easily obtained with the descript_d() function:

descript_d(dscore) # Data frame containing IAT D-scores
#>             Mean   SD   Min  Max
#> D-score    -0.56 0.54 -1.59 1.29
#> D-practice -0.59 0.64 -1.59 1.26
#> D-test     -0.54 0.56 -1.59 1.32

By specifying latex = TRUE, function descript_d() print the results in LaTeX code:

descript_d(dscore, # Data frame containing IAT D-scores
           latex = TRUE) # obtain the code for latex tables
#> % latex table generated in R 4.4.2 by xtable 1.8-4 package
#> % Thu Nov  7 03:26:49 2024
#> \begin{table}[ht]
#> \centering
#> \begin{tabular}{rrrrr}
#>   \hline
#>  & Mean & SD & Min & Max \\ 
#>   \hline
#> D-score & -0.56 & 0.54 & -1.59 & 1.29 \\ 
#>   D-practice & -0.59 & 0.64 & -1.59 & 1.26 \\ 
#>   D-test & -0.54 & 0.56 & -1.59 & 1.32 \\ 
#>    \hline
#> \end{tabular}
#> \end{table}

The IAT_rel() function computes the reliability of the IAT by correlating the D score obtained from practice blocks with the D score obtained from test blocks (see Gawronski et al. 2017 for further details):

IAT_rel(dscore)
#> $`Test-pratice Reliability`
#> [1] 0.65
#> 
#> $`Number of participants`
#> [1] 162
#> 
#> attr(,"class")
#> [1] "IAT_rel"

Plotting the results

The implicitMeasures package comes with several functions for obtaining clear representations of the results at both individual respondent and sample levels. Additionally, it includes a function for computing and plotting multiple IAT D score algorithms at the same time.

Individual respondent plot

The d_point() function plots the IAT D score for each respondent.

d_point(dscore) # Data frame containing IAT D scores
d_point() function with default settings

d_point() function with default settings

In case of large sample size, the label identifying each respondent is difficult to read. It can be eliminated by setting x_values = FALSE.

Respondents can be arranged by increasing or decreasing D scores by setting argument order_sbj equal to "D-increasing" or "D-decreasing", respectively. Descriptive statistics (MD-score ± 2sd) can be added by setting include_stats = TRUE. Finally, the color of the points can be changed by using argument col_point.

d_point(dscore, # dataframe containing IAT D-scores
       order_sbj = "D-decreasing", # change respondents order
       x_values = FALSE, # remove respodents' labels
       include_stats = TRUE, # include descriptive statistics
       col_point = "lightskyblue") # change points color
d_point() function with   settings change

d_point() function with settings change

Sample level plot

The d_density() function plots the distribution of the IAT D scores. It allows for choosing the most appropriate representation.

d_density(dscore) # dataframe containing IAT Dscores
d_density() function with default settings

d_density() function with default settings

The number of bins can be changed with argument n_bin. Argument graph can be used for changing the graphical representation of the data. It is possible to choose an histogram representation (graph = "histogram", default), a representation of the density distribution (graph = "density"), or a violin plot (graph = "violin"). Argument col_fill can be used to change the color of the points representing each respondent’s score in the violin plot. Descriptive statistics (i.e., MD-score ± 2sd) can be added to the graph by setting argument include_stats = TRUE.

d_density(dscore, # dataframe containing IAT Dscores
        graph = "violin", # change graphical representation
        include_stats = TRUE) # include descriptive statistics
\label{fig:sampleSettings}d_density() function with settings change

d_density() function with settings change

Multiple D scores

The multi_dscore() function computes and plot multiple D score algorithms. The D score algorithms that can be computed depend on the IAT administration. If the IAT administration included a feedback strategy (i.e., built-in correction), only D1 and D2 algorithms should be computed. If the IAT administration did not include a feedback strategy, then algorithms D3, D4, D5, and D6 should be computed. An exhaustive and detailed illustration of the D score algorithms is provided in the “Implicit Measures” vignette. To specify the algorithms that can be computed, argument ds must be set equal to either "built-in" (for the computation of D1 and D2) or error-inflation (for the computation of all other algorithms).

multi_scores <- multi_dscore(iat_data, # object with class "iat_clean"
                             ds = "error-inflation") # string specifying the 
                                            # algorithms to compute

The multi_dscore() function results in a list containing two objects. The first object is a data.frame that contains all the computed algorithms and the respondent IDs.

multi_d <- multi_scores[[1]]
head(multi_d)
#>   participant   dscore_d3   dscore_d4   dscore_d5   dscore_d6
#> 1           4 -0.76242699 -0.76242699 -0.76242699 -0.76242699
#> 2           6 -1.24976744 -1.26905702 -1.24976744 -1.26905702
#> 3           8 -0.03801459  0.02021915 -0.03801459  0.02021915
#> 4          11 -0.96488057 -0.97207623 -0.95538961 -0.96294963
#> 5          14 -0.59341709 -0.57385144 -0.59236921 -0.57213017
#> 6          17 -1.30474798 -1.27140694 -1.27997040 -1.26676628
str(multi_d)
#> 'data.frame':    162 obs. of  5 variables:
#>  $ participant: int  4 6 8 11 14 17 18 19 20 21 ...
#>  $ dscore_d3  : num  -0.762 -1.25 -0.038 -0.965 -0.593 ...
#>  $ dscore_d4  : num  -0.7624 -1.2691 0.0202 -0.9721 -0.5739 ...
#>  $ dscore_d5  : num  -0.762 -1.25 -0.038 -0.955 -0.592 ...
#>  $ dscore_d6  : num  -0.7624 -1.2691 0.0202 -0.9629 -0.5721 ...

The second object is a ggplot graph displaying the distribution of the D scores computed with each algorithm in violin plots.

multi_scores[[2]]
Multiple D-scores representation

Multiple D-scores representation

References

Gawronski, Bertram, Mike Morrison, Curtis E Phills, and Silvia Galdi. 2017. “Temporal Stability of Implicit and Explicit Measures: A Longitudinal Analysis.” Personality and Social Psychology Bulletin 43 (3): 300–312. https://doi.org/10.1177/0146167216684131.