RePsychLing Kliegl, Kuschela, & Laubrock (2015)

Author

Phillip Alday, Douglas Bates, and Reinhold Kliegl

Published

2022-09-27

Background

Kliegl et al. (2015) is a follow-up to Kliegl et al. (2010) (see also script mmt_kwdyz11.qmd) from an experiment looking at a variety of effects of visual cueing under four different cue-target relations (CTRs). In this experiment two rectangles are displayed (1) in horizontal orientation , (2) in vertical orientation, (3) in left diagonal orientation, or in (4) right diagonal orientation relative to a central fixation point. Subjects react to the onset of a small or a large visual target occuring at one of the four ends of the two rectangles. The target is cued validly on 70% of trials by a brief flash of the corner of the rectangle at which it appears; it is cued invalidly at the three other locations 10% of the trials each. This implies a latent imbalance in design that is not visiable in the repeated-measures ANOVA, but we will show its effect in the random-effect structure and conditional modes.

There are a couple of differences between the first and this follow-up experiment, rendering it more a conceptual than a direct replication. First, the original experiment was carried out at Peking University and this follow-up at Potsdam University. Second, diagonal orientations of rectangles and large target sizes were not part of the design of Kliegl et al. (2010). To keep matters somewhat simpler and comparable we ignore them in this script.

We specify three contrasts for the four-level factor CTR that are derived from spatial, object-based, and attractor-like features of attention. They map onto sequential differences between appropriately ordered factor levels. Replicating Kliegl et al. (2010), the attraction effect was not significant as a fixed effect, but yielded a highly reliable variance component (VC; i.e., reliable individual differences in positive and negative attraction effects cancel the fixed effect). Moreover, these individual differences in the attraction effect were negatively correlated with those in the spatial effect.

This comparison is of interest because a few years after the publication of Kliegl et al. (2010), the theoretically critical correlation parameter (CP) between the spatial effect and the attraction effect was determined as the source of a non-singular LMM in that paper. The present study served the purpose to estimate this parameter with a larger sample and a wider variety of experimental conditions. Therefore, the code in this script is largely the same as the one in kwdyz.jl.

There will be another vignette modelling the additional experimental manipulations of target size and orientation of cue rectangle. This analysis was reported in the parsimonious mixed-model paper (Bates et al., 2015); they were also used in a paper of GAMMs (Baayen et al., 2017). Data and R scripts are also available in R-package RePsychLing. Here we provide some of the corresponding analyses with MixedModels.jl and a much wider variety of visualizations of LMM results.

Packages

Code
using Arrow
using AlgebraOfGraphics
using CairoMakie
using CategoricalArrays
using Chain
using DataFrameMacros
using DataFrames
using MixedModels
using MixedModelsMakie
using Random
using ProgressMeter
using Statistics
using StatsBase

using AlgebraOfGraphics: density
using AlgebraOfGraphics: boxplot
using MixedModelsMakie: qqnorm
using MixedModelsMakie: ridgeplot
using MixedModelsMakie: scatter
const datadir = joinpath(@__DIR__, "data")

ProgressMeter.ijulia_behavior(:clear)
CairoMakie.activate!(; type="png")

Read data, compute and plot means

dat = @chain "kkl15.arrow" begin
  joinpath(datadir, _)
  Arrow.Table
  DataFrame
  select(
    :subj =>
      (s -> categorical(string.('S', lpad.(s, 3, '0')))) => :Subj,
    :tar => categorical => :CTR,
    :rt => (x -> log.(x)) => :lrt,
    :rt,
  )
end
levels!(dat.CTR, ["val", "sod", "dos", "dod"])
describe(dat)

4 rows × 7 columns

variablemeanminmedianmaxnmissingeltype
SymbolUnion…AnyUnion…AnyInt64DataType
1SubjS001S1470CategoricalValue{String, UInt32}
2CTRvaldod0CategoricalValue{String, UInt32}
3lrt5.644245.01215.622556.619380Float64
4rt293.147150.22276.594749.4810Float64

We recommend to code the levels/units of random factor / grouping variable not as a number, but as a string starting with a letter and of the same length for all levels/units.

We also recommend to sort levels of factors into a meaningful order, that is overwrite the default alphabetic ordering. This is also a good place to choose alternative names for variables in the context of the present analysis.

The LMM analysis is based on log-transformed reaction times lrt, indicated by a boxcox() check of model residuals. With the exception of diagnostic plots of model residuals, the analysis of untransformed reaction times did not lead to different results.

Comparative density plots of all response times by cue-target relation show the times for valid cues to be faster than for the other conditions.

Code
draw(
  data(dat) *
  mapping(
    :lrt => "log(Reaction time [ms])";
    color=:CTR =>
      renamer("val" => "valid cue", "sod" => "some obj/diff pos", "dos" => "diff obj/same pos", "dod" => "diff obj/diff pos") => "Cue-target relation",
  ) *
  density();
  figure=(; resolution=(800, 350)),
)

Figure 1: Compartive density plots of log response time by condition

Boxplots of the mean of log response time by subject under the different conditions show an outlier value under three of the four conditions; they are from the same subject.

dat_subj = combine(
  groupby(dat, [:Subj, :CTR]),
  :rt => length => :n,
  :rt => mean => :rt_m,
  :lrt => mean => :lrt_m,
)
describe(dat_subj)

5 rows × 7 columns

variablemeanminmedianmaxnmissingeltype
SymbolUnion…AnyUnion…AnyInt64DataType
1SubjS001S1470CategoricalValue{String, UInt32}
2CTRvaldod0CategoricalValue{String, UInt32}
3n156.2944964.04480Int64
4rt_m308.223208.194304.862584.710Float64
5lrt_m5.69085.332265.698486.361410Float64
Code
boxplot(
  dat_subj.CTR.refs,
  dat_subj.lrt_m;
  orientation=:horizontal,
  show_notch=true,
  axis=(;
    yticks=(
      1:4,
      [
        "valid cue",
        "same obj/diff pos",
        "diff obj/same pos",
        "diff obj/diff pos",
      ],
    ),
  ),
  figure=(; resolution=(800, 300)),
)

Figure 2: Comparative boxplots of mean response by subject under different conditions

Mean of log reaction times for four cue-target relations. Targets appeared at (a) the cued position (valid) in a rectangle, (b) in the same rectangle cue, but at its other end, (c) on the second rectangle, but at a corresponding horizontal/vertical physical distance, or (d) at the other end of the second rectangle, that is \(\sqrt{2}\) of horizontal/vertical distance diagonally across from the cue, that is also at larger physical distance compared to (c).

We remove the outlier subject and replot, but we model the data points in dat and check whether this subject appears as an outlier in the caterpillar plot of conditional modes.

Code
let
  dat_subj2 = @subset(dat_subj, :rt_m < 510)
  boxplot(
    dat_subj2.CTR.refs,
    dat_subj2.lrt_m;
    orientation=:horizontal,
    show_notch=true,
    axis=(;
      yticks=(
        1:4,
        [
          "valid cue",
          "same obj/diff pos",
          "diff obj/same pos",
          "diff obj/diff pos",
        ],
      ),
    ),
    figure=(; resolution=(800, 300)),
  )
end

Figure 3: Comparative boxplots of mean response by subject under different conditions without outlier

A better alternative to the boxplot is often a dotplot, because it also displays subjects’ condition means.

To be done

For the next set of plots we average subjects’ data within the four experimental conditions. This table could be used as input for a repeated-measures ANOVA.

dat_cond = combine(
  groupby(dat_subj, :CTR),
  :n => length => :N,
  :lrt_m => mean => :lrt_M,
  :lrt_m => std => :lrt_SD,
  :lrt_m => (x -> std(x) / sqrt(length(x))) => :lrt_SE,
)

4 rows × 5 columns

CTRNlrt_Mlrt_SDlrt_SE
Cat…Int64Float64Float64Float64
1val865.614430.158050.0170429
2sod865.688360.1913950.0206386
3dos865.729430.1910270.0205989
4dod865.730990.216280.023322

We can also look at correlations plots based on the four condition means. There are actually two correlation matrices which have correspondences in alternative parameterizatios of the LMM random-effect structure. One matrix is based on the four measures. If you think of the four measures as test scores, this matrix is the usual correlation matrix. The second matrix contains correlations between the Grand Mean (GM) and the three effects defined with the contrasts for the four levels of the condition factor in the next chunk.

To this end, we

  • use the unstack() command to convert data from long to wide format,
  • compute the GM and the three experimental effects.
  • plot the correlation matrix for four measures/scores, and
  • plot the correlation matrix for GM and three effects
dat_subj_w = @chain dat_subj begin
  unstack(:Subj, :CTR, :rt_m)
  disallowmissing!
  @transform(
    :GM = (:val + :sod + :dos + :dod) ./ 4,
    :spatial = :sod - :val,
    :object = :dos - :sod,
    :attraction = :dod - :dos,
  )
end
describe(dat_subj_w)

9 rows × 7 columns

variablemeanminmedianmaxnmissingeltype
SymbolUnion…AnyUnion…AnyInt64DataType
1SubjS001S1470CategoricalValue{String, UInt32}
2val283.688216.35286.376513.530Float64
3sod306.75213.444305.092562.0150Float64
4dos319.896215.787317.527584.710Float64
5dod322.561208.194315.511555.2060Float64
6GM308.223217.819309.381553.8650Float64
7spatial23.0621-47.203620.227287.78730Float64
8object13.1456-13.827311.790361.04240Float64
9attraction2.66497-43.8703-1.1188763.53470Float64
#@df dat_subj_w StatsPlots.corrplot(cols(2:5), grid = false, compact=false)
#@df dat_subj_w StatsPlots.corrplot(cols(6:9), grid = false, compact=false)
Note

Two of the theoreticsally irrelevant within-subject effect correlations have a different sign than the corresponding, non-significant CPs in the LMM; they are negative here, numerically positive in the LMM. This occurs only very rarely in the case of ecological correlations. However, as they are not significant according to shortest coverage interval, it may not be that relevant either. It is the case both for effects based on log-transformed and raw reaction times.

Linear mixed model

contrasts = Dict(
  :Subj => Grouping(),
  :CTR => SeqDiffCoding(; levels=["val", "sod", "dos", "dod"]),
)
m1 = let
  form = @formula lrt ~ 1 + CTR + (1 + CTR | Subj)
  fit(MixedModel, form, dat; contrasts)
end
Minimizing 224   Time: 0:00:00 ( 1.57 ms/it)
Est. SE z p σ_Subj
(Intercept) 5.6907 0.0199 286.42 <1e-99 0.1839
CTR: sod 0.0740 0.0080 9.30 <1e-19 0.0688
CTR: dos 0.0409 0.0038 10.74 <1e-26 0.0011
CTR: dod 0.0016 0.0057 0.28 0.7771 0.0387
Residual 0.1971
VarCorr(m1)
Column Variance Std.Dev Corr.
Subj (Intercept) 0.03382757 0.18392272
CTR: sod 0.00472964 0.06877241 +0.56
CTR: dos 0.00000126 0.00112031 -0.05 +0.80
CTR: dod 0.00149693 0.03869018 +0.60 +0.66 +0.36
Residual 0.03884575 0.19709325
issingular(m1)
true
only(MixedModels.PCA(m1))

Principal components based on correlation matrix
 (Intercept)   1.0     .      .      .
 CTR: sod      0.56   1.0     .      .
 CTR: dos     -0.05   0.8    1.0     .
 CTR: dod      0.6    0.66   0.36   1.0

Normalized cumulative variances:
[0.6326, 0.9136, 1.0, 1.0]

Component loadings
                PC1    PC2    PC3    PC4
 (Intercept)  -0.41   0.66  -0.47   0.42
 CTR: sod     -0.6   -0.18  -0.34  -0.7
 CTR: dos     -0.43  -0.69  -0.07   0.58
 CTR: dod     -0.53   0.25   0.81  -0.0

We note that the critical correlation parameter between spatial (sod) and attraction (dod) is now estimated at .66 – not that close to the 1.0 boundary that caused singularity in Kliegl et al. (2010). However, the LMM based on log reaction times is still singular. Let’s check for untransformed reaction times.

m1_rt = let
  form = @formula rt ~ 1 + CTR + (1 + CTR | Subj)
  fit(MixedModel, form, dat; contrasts)
end
Est. SE z p σ_Subj
(Intercept) 308.2059 6.4150 48.04 <1e-99 59.3769
CTR: sod 23.0720 2.6416 8.73 <1e-17 22.8512
CTR: dos 13.0855 1.4583 8.97 <1e-18 6.8329
CTR: dod 2.6860 2.0609 1.30 0.1925 15.1037
Residual 65.2246
VarCorr(m1_rt)
Column Variance Std.Dev Corr.
Subj (Intercept) 3525.62031 59.37693
CTR: sod 522.17832 22.85122 +0.66
CTR: dos 46.68829 6.83288 +0.35 +0.15
CTR: dod 228.12231 15.10372 +0.53 +0.65 +0.30
Residual 4254.24783 65.22460
issingular(m1_rt)
false

For untransformed reaction times, we see the model is not singular.

Diagnostic plots of LMM residuals

Do model residuals meet LMM assumptions? Classic plots are

  • Residual over fitted
  • Quantiles of model residuals over theoretical quantiles of normal distribution

Residual-over-fitted plot

The slant in residuals show a lower and upper boundary of reaction times, that is we have have too few short and too few long residuals. Not ideal, but at least width of the residual band looks similar across the fitted values, that is there is no evidence for heteroskedasticity.

Code
scatter(fitted(m1), residuals(m1))

Figure 4: Residuals versus fitted values for model m1

With many observations the scatterplot is not that informative. Contour plots or heatmaps may be an alternative.

Code
set_aog_theme!()
draw(
  data((; f=fitted(m1), r=residuals(m1))) *
  mapping(
    :f => "Fitted values from m1", :r => "Residuals from m1"
  ) *
  density();
)

Figure 5: Heatmap of residuals versus fitted values for model m1

Q-Q plot

The plot of quantiles of model residuals over corresponding quantiles of the normal distribution should yield a straight line along the main diagonal.

Code
qqnorm(m1; qqline=:none)

Figure 6: ?(caption)

Code
qqnorm(m1_rt; qqline=:none)

Figure 7: ?(caption)

Observed and theoretical normal distribution

The violation of expectation is again due to the fact that the distribution of residuals is narrower than expected from a normal distribution. We can see this in this plot. Overall, it does not look too bad.

Code
let
  n = nrow(dat)
  dat_rz = (;
    value=vcat(residuals(m1) ./ std(residuals(m1)), randn(n)),
    curve=repeat(["residual", "normal"]; inner=n),
  )
  draw(
    data(dat_rz) *
    mapping(:value; color=:curve) *
    density(; bandwidth=0.1);
  )
end

Figure 8: Kernel density plot of the standardized residuals for model m1 versus a standard normal

Conditional modes

Caterpillar plot

Code
cm1 = only(ranefinfo(m1))
caterpillar!(Figure(; resolution=(800, 1200)), cm1; orderby=2)

Figure 9: Prediction intervals of the subject random effects in model m1

When we order the conditional modes for GM, that is (Intercept), the outlier subject S113 becomes visible; the associated experimental effects are not unusual.

Code
caterpillar!(Figure(; resolution=(800, 1200)), cm1; orderby=1)

Figure 10: Prediction intervals of the subject random effects in model m1 ordered by mean response

The catepillar plot also reveals that credibilty intervals are much shorter for subjects’ Grand Means, shown in (Intercept), than the subjects’ experimental effects, because the latter are based on difference scores not means. Moreover, credibility intervals are shorter for the first spatial effect sod than the other two effects, because the spatial effect involves the valid condition which yielded three times as many trials than the other three conditions. Consequently, the spatial effect is more reliable. Unfortunately, due to differences in scaling of the x-axis of the panels this effect must be inferred. One option to reveal this difference is to reparameterize the LMM such model parameters estimate the conditional modes for the levels of condition rather than the contrast-based effects. This is accomplished by replacing the 1 in the random effect term with 0, as shown next.

m1L = let
  form = @formula rt ~ 1 + CTR + (0 + CTR | Subj)
  fit(MixedModel, form, dat; contrasts)
end
Est. SE z p σ_Subj
(Intercept) 308.2059 6.4148 48.05 <1e-99
CTR: sod 23.0720 2.6414 8.73 <1e-17 60.1753
CTR: dos 13.0855 1.4582 8.97 <1e-18 62.4747
CTR: dod 2.6860 2.0606 1.30 0.1924 71.5374
CTR: val 47.2897
Residual 65.2246
VarCorr(m1L)
Column Variance Std.Dev Corr.
Subj CTR: val 2236.3175 47.2897
CTR: sod 3621.0657 60.1753 +0.94
CTR: dos 3903.0867 62.4747 +0.93 +0.99
CTR: dod 5117.6057 71.5374 +0.89 +0.98 +0.98
Residual 4254.2506 65.2246

The caterpillar plot for levels shows the effect of the number of trials on credibility intervals; they are obviously much shorter for the valid condition. Note that this effect is not visible in a repeated-measure ANOVA with four condition means per subject as input.

Code
@chain m1L begin
  ranefinfo
  only
  caterpillar!(Figure(; resolution=(800, 1000)), _; orderby=1)
end

Figure 11: Prediction intervals of the subject random effects in model m1L

Shrinkage plot

Log-transformed reaction times (LMM m1)

Code
shrinkageplot!(Figure(; resolution=(1000, 1200)), m1)

Figure 12: Shrinkage plots of the subject random effects in model m1L

Three of the CPs are imploded, but not the theoretically critical ones. These implosions did not occur (or were not as visible) for raw reaction times.

Raw reaction times (LMM m1_rt)

Code
shrinkageplot!(Figure(; resolution=(1000, 1200)), m1_rt)

Figure 13: Shrinkage plots of the subject random effects in model m1_rt

The implosion is for three CP visualizations is not observed for raw reaction times. Interesting.

Parametric bootstrap

Here we

  • generate a bootstrap sample
  • compute shortest covergage intervals for the LMM parameters
  • plot densities of bootstrapped parameter estimates for residual, fixed effects, variance components, and correlation parameters

Generate a bootstrap sample

We generate 2500 samples for the 15 model parameters (4 fixed effect, 4 VCs, 6 CPs, and 1 residual).

Random.seed!(1234321)
samp = parametricbootstrap(2500, m1);
dat2 = DataFrame(samp.allpars)
first(dat2, 10)

10 rows × 5 columns

itertypegroupnamesvalue
Int64StringString?String?Float64
11βmissing(Intercept)5.6919
21βmissingCTR: sod0.0777423
31βmissingCTR: dos0.0406776
41βmissingCTR: dod-0.00333782
51σSubj(Intercept)0.182536
61σSubjCTR: sod0.0624235
71ρSubj(Intercept), CTR: sod0.676632
81σSubjCTR: dos0.00373515
91ρSubj(Intercept), CTR: dos-0.306105
101ρSubjCTR: sod, CTR: dos0.493856
nrow(dat2) # 2500 estimates for each of 15 model parameters
37500

Shortest coverage interval

DataFrame(shortestcovint(samp))

15 rows × 5 columns

typegroupnameslowerupper
StringString?String?Float64Float64
1βmissing(Intercept)5.64875.72775
2βmissingCTR: sod0.05795170.0889741
3βmissingCTR: dos0.03291510.0477077
4βmissingCTR: dod-0.009554010.0129631
5σSubj(Intercept)0.1533480.207575
6σSubjCTR: sod0.05680810.0801441
7ρSubj(Intercept), CTR: sod0.3881730.717445
8σSubjCTR: dos0.001042030.0179115
9ρSubj(Intercept), CTR: dos-0.9171360.999993
10ρSubjCTR: sod, CTR: dos-0.8790991.0
11σSubjCTR: dod0.02791860.0484329
12ρSubj(Intercept), CTR: dod0.395750.81969
13ρSubjCTR: sod, CTR: dod0.4438730.892577
14ρSubjCTR: dos, CTR: dod-0.8760050.901814
15σresidualmissing0.1958550.198186

We can also visualize the shortest coverage intervals for fixed effects with the ridgeplot() command:

Code
ridgeplot(samp; show_intercept=false)

Figure 14: Ridge plot of fixed-effects bootstrap samples from model m1L

Comparative density plots of bootstrapped parameter estimates

Residual

Code
draw(
  data(@subset(dat2, :type == "σ" && :group == "residual")) *
  mapping(:value => "Residual") *
  density();
  figure=(; resolution=(800, 400)),
)

Figure 15: Kernel density estimate from bootstrap samples of the residual standard deviation for model m1L

Fixed effects and associated variance components (w/o GM)

The shortest coverage interval for the GM ranges from 376 to 404 ms and the associate variance component from .15 to .21. To keep the plot range small we do not include their densities here.

Code
rn = renamer([
  "(Intercept)" => "GM",
  "CTR: sod" => "spatial effect",
  "CTR: dos" => "object effect",
  "CTR: dod" => "attraction effect",
  "(Intercept), CTR: sod" => "GM, spatial",
  "(Intercept), CTR: dos" => "GM, object",
  "CTR: sod, CTR: dos" => "spatial, object",
  "(Intercept), CTR: dod" => "GM, attraction",
  "CTR: sod, CTR: dod" => "spatial, attraction",
  "CTR: dos, CTR: dod" => "object, attraction",
])
draw(
  data(@subset(dat2, :type == "β" && :names  "(Intercept)")) *
  mapping(
    :value => "Experimental effect size [ms]";
    color=:names => rn => "Experimental effects",
  ) *
  density();
  figure=(; resolution=(800, 350)),
)

Figure 16: Kernel density estimate from bootstrap samples of the fixed effects for model m1L

The densitiies correspond nicely with the shortest coverage intervals.

Code
draw(
  data(@subset(dat2, :type == "σ" && :group == "Subj" && :names  "(Intercept)") ) *
  mapping(
    :value => "Standard deviations [ms]";
    color=:names => rn => "Variance components",
  ) *
  density();
  figure=(; resolution=(800, 350)),
)

Figure 17: Kernel density estimate from bootstrap samples of the standard deviations for model m1L (excluding Grand Mean)

The VC are all very nicely defined.

Correlation parameters (CPs)

Code
draw(
  data(@subset(dat2, :type == "ρ")) *
  mapping(
    :value => "Correlation";
    color=:names => rn => "Correlation parameters",
  ) *
  density();
  figure=(; resolution=(800, 350)),
)

Figure 18: Kernel density estimate from bootstrap samples of the standard deviations for model m1L

Three CPs stand out positively, the correlation between GM and the spatial effect, GM and attraction effect, and the correlation between spatial and attraction effects. The second CP was positive, but not significant in the first study. The third CP replicates a CP that was judged questionable in script kwdyz11.jl.

The three remaining CPs are not well defined for log-transformed reaction times; they only fit noise and should be removed. It is also possible that fitting the complex experimental design (including target size and rectangle orientation) will lead to more acceptable estimates. The corresponding plot based on LMM m1_rt for raw reaction times still shows them with very wide distributions, but acceptable.

References

Baayen, H., Vasishth, S., Kliegl, R., & Bates, D. (2017). The cave of shadows: Addressing the human factor with generalized additive mixed models. Journal of Memory and Language, 94, 206–234. https://doi.org/10.1016/j.jml.2016.11.006
Bates, D., Kliegl, R., Vasishth, S., & Baayen, H. (2015). Parsimonious mixed models. arXiv. https://doi.org/10.48550/ARXIV.1506.04967
Kliegl, R., Kushela, J., & Laubrock, J. (2015). Object orientation and target size modulate the speed of visual attention. Department of Psychology, University of Potsdam.
Kliegl, R., Wei, P., Dambacher, M., Yan, M., & Zhou, X. (2010). Experimental effects and individual differences in linear mixed models: Estimating the relationship between spatial, object, and attraction effects in visual attention. Frontiers in Psychology. https://doi.org/10.3389/fpsyg.2010.00238