Mixed Models Tutorial: Contrast Coding

Author

Reinhold Kliegl

Published

2025-12-26

This script uses a subset of data reported in Fühner et al. (2021).

To circumvent delays associated with model fitting we work with models that are less complex than those in the reference publication. All the data to reproduce the models in the publication are used here, too; the script requires only a few changes to specify the more complex models in the paper.

All children were between 6.0 and 6.99 years at legal keydate (30 September) of school enrollment, that is in their ninth year of life in the third grade. To avoid delays associated with model fitting we work with a reduced data set and less complex models than those in the reference publication. The script requires only a few changes to specify the more complex models in the paper.

The script is structured in three main sections:

  1. Setup with reading and examining the data

  2. Contrasts coding

  1. Other topics

1 Setup

1.1 Packages and functions

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

using MixedModels: likelihoodratiotest
using SMLP2026: dataset

progress = isinteractive()

1.2 Readme for dataset("fggk21")

Number of scores: 525126

  1. Cohort: 9 levels; 2011-2019

  2. School: 515 levels

  3. Child: 108295 levels; all children are between 8.0 and 8.99 years old

  4. Sex: “Girls” (n=55,086), “Boys” (n= 53,209)

  5. age: testdate - middle of month of birthdate

  6. Test: 5 levels

    • Endurance (Run): 6 minute endurance run [m]; to nearest 9m in 9x18m field
    • Coordination (Star_r): star coordination run [m/s]; 9x9m field, 4 x diagonal = 50.912 m
    • Speed(S20_r): 20-meters sprint [m/s]
    • Muscle power low (SLJ): standing long jump [cm]
    • Muscle power up (BPT): 1-kg medicine ball push test [m]
  7. score - see units

1.3 Preprocessing

1.3.1 Read data

tbl = dataset(:fggk21)
Arrow.Table with 525126 rows, 7 columns, and schema:
 :Cohort  String
 :School  String
 :Child   String
 :Sex     String
 :age     Float64
 :Test    String
 :score   Float64
df = DataFrame(tbl)
describe(df)
7×7 DataFrame
Row variable mean min median max nmissing eltype
Symbol Union… Any Union… Any Int64 DataType
1 Cohort 2011 2019 0 String
2 School S100043 S800200 0 String
3 Child C002352 C117966 0 String
4 Sex female male 0 String
5 age 8.56073 7.99452 8.55852 9.10609 0 Float64
6 Test BPT Star_r 0 String
7 score 226.141 1.14152 4.65116 1530.0 0 Float64

1.3.2 Extract a stratified subsample

We extract a random sample of 500 children from the Sex (2) x Test (5) cells of the design. Cohort and School are random.

dat = @chain df begin
  @transform(:Sex = :Sex == "female" ? "Girls" : "Boys")
  @groupby(:Test, :Sex)
  combine(x -> x[sample(1:nrow(x), 500), :])
end
5000×7 DataFrame
4975 rows omitted
Row Test Sex Cohort School Child age score
String String String String String Float64 Float64
1 S20_r Boys 2012 S101000 C022078 8.24914 4.87805
2 S20_r Boys 2019 S111570 C085710 8.80219 5.0
3 S20_r Boys 2018 S102167 C090434 8.85421 4.44444
4 S20_r Boys 2016 S113207 C075483 8.71184 4.7619
5 S20_r Boys 2012 S103251 C022356 8.24914 4.54545
6 S20_r Boys 2012 S112239 C007893 8.08214 4.44444
7 S20_r Boys 2018 S100754 C052533 8.52567 4.87805
8 S20_r Boys 2015 S112410 C094974 8.88159 4.16667
9 S20_r Boys 2019 S104206 C065907 8.63244 3.77358
10 S20_r Boys 2017 S104899 C102481 8.95003 4.87805
11 S20_r Boys 2013 S102570 C002577 7.99452 4.25532
12 S20_r Boys 2013 S101527 C078261 8.7447 4.87805
13 S20_r Boys 2016 S102271 C045811 8.4627 4.54545
4989 Run Girls 2013 S105223 C006769 8.0794 918.0
4990 Run Girls 2017 S105685 C086254 8.80767 992.0
4991 Run Girls 2012 S100420 C069255 8.6653 1041.0
4992 Run Girls 2013 S103640 C006616 8.0794 850.0
4993 Run Girls 2017 S103329 C026956 8.29569 846.0
4994 Run Girls 2019 S112458 C106262 8.9692 1098.0
4995 Run Girls 2017 S103810 C012354 8.15058 954.0
4996 Run Girls 2018 S110127 C044003 8.44079 756.0
4997 Run Girls 2011 S111405 C004303 8.0 1125.0
4998 Run Girls 2018 S103512 C090661 8.85421 1026.0
4999 Run Girls 2017 S112963 C020640 8.22724 972.0
5000 Run Girls 2016 S111168 C027204 8.29569 1120.0

1.3.3 Transformations

transform!(dat, :age, :age => (x -> x .- 8.5) => :a1) # centered age (linear)
select!(groupby(dat, :Test), :, :score => zscore => :zScore) # z-score
5000×9 DataFrame
4975 rows omitted
Row Test Sex Cohort School Child age score a1 zScore
String String String String String Float64 Float64 Float64 Float64
1 S20_r Boys 2012 S101000 C022078 8.24914 4.87805 -0.250856 0.864618
2 S20_r Boys 2019 S111570 C085710 8.80219 5.0 0.30219 1.14654
3 S20_r Boys 2018 S102167 C090434 8.85421 4.44444 0.354209 -0.137778
4 S20_r Boys 2016 S113207 C075483 8.71184 4.7619 0.211841 0.596119
5 S20_r Boys 2012 S103251 C022356 8.24914 4.54545 -0.250856 0.0957351
6 S20_r Boys 2012 S112239 C007893 8.08214 4.44444 -0.417864 -0.137778
7 S20_r Boys 2018 S100754 C052533 8.52567 4.87805 0.0256674 0.864618
8 S20_r Boys 2015 S112410 C094974 8.88159 4.16667 0.381588 -0.779937
9 S20_r Boys 2019 S104206 C065907 8.63244 3.77358 0.132444 -1.68865
10 S20_r Boys 2017 S104899 C102481 8.95003 4.87805 0.450034 0.864618
11 S20_r Boys 2013 S102570 C002577 7.99452 4.25532 -0.505476 -0.574993
12 S20_r Boys 2013 S101527 C078261 8.7447 4.87805 0.244695 0.864618
13 S20_r Boys 2016 S102271 C045811 8.4627 4.54545 -0.0373032 0.0957351
4989 Run Girls 2013 S105223 C006769 8.0794 918.0 -0.420602 -0.558287
4990 Run Girls 2017 S105685 C086254 8.80767 992.0 0.307666 -0.06987
4991 Run Girls 2012 S100420 C069255 8.6653 1041.0 0.165298 0.253541
4992 Run Girls 2013 S103640 C006616 8.0794 850.0 -0.420602 -1.0071
4993 Run Girls 2017 S103329 C026956 8.29569 846.0 -0.204312 -1.0335
4994 Run Girls 2019 S112458 C106262 8.9692 1098.0 0.469199 0.629754
4995 Run Girls 2017 S103810 C012354 8.15058 954.0 -0.349418 -0.320679
4996 Run Girls 2018 S110127 C044003 8.44079 756.0 -0.059206 -1.62752
4997 Run Girls 2011 S111405 C004303 8.0 1125.0 -0.5 0.80796
4998 Run Girls 2018 S103512 C090661 8.85421 1026.0 0.354209 0.154538
4999 Run Girls 2017 S112963 C020640 8.22724 972.0 -0.272758 -0.201875
5000 Run Girls 2016 S111168 C027204 8.29569 1120.0 -0.204312 0.774959
dat2 = combine(
  groupby(dat, [:Test, :Sex]),
  :score => mean,
  :score => std,
  :zScore => mean,
  :zScore => std,
)
10×6 DataFrame
Row Test Sex score_mean score_std zScore_mean zScore_std
String String Float64 Float64 Float64 Float64
1 S20_r Boys 4.58038 0.437231 0.176486 1.01078
2 BPT Boys 3.94 0.689689 0.326166 0.980188
3 SLJ Boys 130.014 20.7866 0.178935 0.988687
4 Star_r Boys 2.08243 0.306162 0.10234 1.07774
5 Run Boys 1040.69 158.249 0.251482 1.04448
6 S20_r Girls 4.4277 0.414432 -0.176486 0.958075
7 BPT Girls 3.481 0.64028 -0.326166 0.909968
8 SLJ Girls 122.49 20.6033 -0.178935 0.979968
9 Star_r Girls 2.02429 0.257161 -0.10234 0.905252
10 Run Girls 964.484 134.18 -0.251482 0.885621

1.3.4 Figure of age x Sex x Test interactions

The main results of relevance here are shown in Figure 2 of Scientific Reports 11:17566.

2 Contrast coding

Contrast coding is part of StatsModels.jl. Here is the primary author’s (i.e., Dave Kleinschmidt’s) documentation of Modeling Categorical Data.

The random factors Child, School, and Cohort are assigned a Grouping contrast. This contrast is needed when the number of groups (i.e., units, levels) is very large. This is the case for Child (i.e., the 108,925 children in the full and probably also the 11,566 children in the reduced data set). The assignment is not necessary for the typical sample size of experiments. However, we use this coding of random factors irrespective of the number of units associated with them to be transparent about the distinction between random and fixed factors.

A couple of general remarks about the following examples. First, all contrasts defined in this tutorial return an estimate of the Grand Mean (GM) in the intercept, that is they are so-called sum-to-zero contrasts. In both Julia and R the default contrast is Dummy coding which is not a sum-to-zero contrast, but returns the mean of the reference (control) group - unfortunately for (quasi-)experimentally minded scientists.

Second, The factor Sex has only two levels. We use EffectCoding (also known as Sum coding in R) to estimate the difference of the levels from the Grand Mean. Unlike in R, the default sign of the effect is for the second level (base is the first, not the last level), but this can be changed with the base kwarg in the command. Effect coding is a sum-to-zero contrast, but when applied to factors with more than two levels does not yield orthogonal contrasts.

Finally, contrasts for the five levels of the fixed factor Test represent the hypotheses about differences between them. In this tutorial, we use this factor to illustrate various options.

We (initially) include only Test as fixed factor and Child as random factor. More complex LMMs can be specified by simply adding other fixed or random factors to the formula.

2.1 SeqDiffCoding: contr1

SeqDiffCoding was used in the publication. This specification tests pairwise differences between the five neighboring levels of Test, that is:

  • SDC1: 2-1
  • SDC2: 3-2
  • SDC3: 4-3
  • SDC4: 5-4

The levels were sorted such that these contrasts map onto four a priori hypotheses; in other words, they are theoretically motivated pairwise comparisons. The motivation also encompasses theoretically motivated interactions with Sex. The order of levels can also be explicitly specified during contrast construction. This is very useful if levels are in a different order in the dataframe. We recommend the explicit specification to increase transparency of the code.

The statistical disadvantage of SeqDiffCoding is that the contrasts are not orthogonal, that is the contrasts are correlated. This is obvious from the fact that levels 2, 3, and 4 are all used in two contrasts. One consequence of this is that correlation parameters estimated between neighboring contrasts (e.g., 2-1 and 3-2) are difficult to interpret. Usually, they will be negative because assuming some practical limitation on the overall range (e.g., between levels 1 and 3), a small “2-1” effect “correlates” negatively with a larger “3-2” effect for mathematical reasons.

Obviously, the tradeoff between theoretical motivation and statistical purity is something that must be considered carefully when planning the analysis.

contr1 = Dict(
    :Sex => EffectsCoding(; levels=["Girls", "Boys"]),
    :Test => SeqDiffCoding(;
      levels=["Run", "Star_r", "S20_r", "SLJ", "BPT"]
    ),
  )
Dict{Symbol, StatsModels.AbstractContrasts} with 2 entries:
  :Test => SeqDiffCoding(["Run", "Star_r", "S20_r", "SLJ", "BPT"])
  :Sex  => EffectsCoding(nothing, ["Girls", "Boys"])
m_ovi_SeqDiff_1 = lmm(@formula(zScore ~ 1 + Test + (1 | Child)),
                      dat; contrasts=contr1, progress)
Est. SE z p σ_Child
(Intercept) -0.0009 0.0142 -0.06 0.9486 0.7654
Test: Star_r -0.0068 0.0442 -0.15 0.8783
Test: S20_r 0.0086 0.0442 0.19 0.8455
Test: SLJ -0.0053 0.0443 -0.12 0.9053
Test: BPT -0.0041 0.0443 -0.09 0.9268
Residual 0.6429

In this case, any differences between tests identified by the contrasts would be spurious because each test was standardized (i.e., M=0, \(SD\)=1). The differences could also be due to an imbalance in the number of boys and girls or in the number of missing observations for each test.

The primary interest in this study related to interactions of the test contrasts with and age and Sex. We start with age (linear) and its interaction with the four test contrasts.

m_ovi_SeqDiff_2 = lmm(@formula(zScore ~ 1 + Test * a1 + (1 | Child)),
                      dat; contrasts=contr1, progress)
Est. SE z p σ_Child
(Intercept) -0.0165 0.0145 -1.14 0.2545 0.7633
Test: Star_r -0.0197 0.0448 -0.44 0.6593
Test: S20_r -0.0006 0.0452 -0.01 0.9887
Test: SLJ 0.0035 0.0451 0.08 0.9383
Test: BPT -0.0249 0.0447 -0.56 0.5770
a1 0.2690 0.0487 5.52 <1e-07
Test: Star_r & a1 0.2202 0.1526 1.44 0.1491
Test: S20_r & a1 0.0879 0.1531 0.57 0.5662
Test: SLJ & a1 0.0095 0.1514 0.06 0.9499
Test: BPT & a1 0.2927 0.1500 1.95 0.0510
Residual 0.6379

The difference between older and younger children is larger for Star_r than for Run (0.2473). S20_r did not differ significantly from Star_r (-0.0377) and SLJ (-0.0113) The largest difference in developmental gain was between BPT and SLJ (0.3355).

Please note that standard errors of this LMM are anti-conservative because the LMM is missing a lot of information in the RES (e..g., contrast-related VCs snd CPs for Child, School, and Cohort.

Next we add the main effect of Sex and its interaction with the four test contrasts.

m_ovi_SeqDiff_3 = lmm(@formula(zScore ~ 1 + Test * (a1 + Sex) + (1 | Child)),
                      dat; contrasts=contr1, progress)
Est. SE z p σ_Child
(Intercept) -0.0144 0.0141 -1.02 0.3060 0.7409
Test: Star_r -0.0193 0.0437 -0.44 0.6594
Test: S20_r -0.0008 0.0441 -0.02 0.9850
Test: SLJ 0.0013 0.0440 0.03 0.9762
Test: BPT -0.0221 0.0436 -0.51 0.6128
a1 0.2479 0.0476 5.21 <1e-06
Sex: Boys 0.2047 0.0138 14.79 <1e-48
Test: Star_r & a1 0.2189 0.1491 1.47 0.1422
Test: S20_r & a1 0.0823 0.1497 0.55 0.5827
Test: SLJ & a1 0.0362 0.1479 0.24 0.8067
Test: BPT & a1 0.2475 0.1465 1.69 0.0911
Test: Star_r & Sex: Boys -0.1554 0.0430 -3.61 0.0003
Test: S20_r & Sex: Boys 0.0764 0.0430 1.78 0.0757
Test: SLJ & Sex: Boys -0.0004 0.0431 -0.01 0.9930
Test: BPT & Sex: Boys 0.1435 0.0430 3.33 0.0009
Residual 0.6272

The significant interactions with Sex reflect mostly differences related to muscle power, where the physiological constitution gives boys an advantage. The sex difference is smaller when coordination and cognition play a role – as in the Star_r test. (Caveat: SEs are estimated with an underspecified RES.)

The final step in this first series is to add the interactions between the three covariates. A significant interaction between any of the four Test contrasts and age (linear) x Sex was hypothesized to reflect a prepubertal signal (i.e., hormones start to rise in girls’ ninth year of life). However, this hypothesis is linked to a specific shape of the interaction: Girls would need to gain more than boys in tests of muscular power.

# we'll be re-using this formula with other contrast specifications,
# so we assign it to its own variable
f_ovi = @formula(zScore ~ 1 + Test * a1 * Sex + (1 | Child))
m_ovi_SeqDiff = lmm(f_ovi, dat; contrasts=contr1, progress)
Est. SE z p σ_Child
(Intercept) -0.0141 0.0141 -1.00 0.3166 0.7416
Test: Star_r -0.0194 0.0437 -0.44 0.6576
Test: S20_r 0.0000 0.0442 0.00 0.9998
Test: SLJ 0.0003 0.0440 0.01 0.9950
Test: BPT -0.0221 0.0436 -0.51 0.6119
a1 0.2482 0.0476 5.22 <1e-06
Sex: Boys 0.2068 0.0141 14.66 <1e-47
Test: Star_r & a1 0.2166 0.1491 1.45 0.1463
Test: S20_r & a1 0.0821 0.1497 0.55 0.5835
Test: SLJ & a1 0.0366 0.1478 0.25 0.8047
Test: BPT & a1 0.2468 0.1465 1.68 0.0920
Test: Star_r & Sex: Boys -0.1587 0.0437 -3.63 0.0003
Test: S20_r & Sex: Boys 0.0822 0.0442 1.86 0.0631
Test: SLJ & Sex: Boys -0.0065 0.0440 -0.15 0.8832
Test: BPT & Sex: Boys 0.1430 0.0436 3.28 0.0010
a1 & Sex: Boys -0.0330 0.0476 -0.69 0.4875
Test: Star_r & a1 & Sex: Boys 0.0667 0.1491 0.45 0.6546
Test: S20_r & a1 & Sex: Boys -0.0735 0.1497 -0.49 0.6234
Test: SLJ & a1 & Sex: Boys 0.0775 0.1478 0.52 0.5999
Test: BPT & a1 & Sex: Boys 0.0080 0.1465 0.05 0.9565
Residual 0.6263

The results are very clear: Despite an abundance of statistical power there is no evidence for the differences between boys and girls in how much they gain in the ninth year of life in these five tests. The authors argue that, in this case, absence of evidence looks very much like evidence of absence of a hypothesized interaction.

In the next two sections we use different contrasts. Does this have a bearing on this result? We still ignore for now that we are looking at anti-conservative test statistics.

2.2 HelmertCoding: contr2

The second set of contrasts uses HelmertCoding. Helmert coding codes each level as the difference from the average of the lower levels. With the default order of Test levels we get the following test statistics which we describe in reverse order of appearance in model output

  • HeC4: 5 - mean(1,2,3,4)
  • HeC3: 4 - mean(1,2,3)
  • HeC2: 3 - mean(1,2)
  • HeC1: 2 - 1

In the model output, HeC1 will be reported first and HeC4 last.

There is some justification for the HeC4 specification in a post-hoc manner because the fifth test (BPT) turned out to be different from the other four tests in that high performance is most likely not only related to physical fitness, but also to overweight/obesity, that is for a subset of children high scores on this test might be indicative of physical unfitness. A priori the SDC4 contrast 5-4 between BPT (5) and SLJ (4) was motivated because conceptually both are tests of the physical fitness component Muscular Power, BPT for upper limbs and SLJ for lower limbs, respectively.

One could argue that there is justification for HeC3 because Run (1), Star_r (2), and S20 (3) involve running but SLJ (4) does not. Sports scientists, however, recoil. For them it does not make much sense to average the different running tests, because they draw on completely different physiological resources; it is a variant of the old apples-and-oranges problem.

The justification for HeC3 is thatRun (1) and Star_r (2) draw more strongly on cardiorespiratory Endurance than S20 (3) due to the longer duration of the runs compared to sprinting for 20 m which is a pure measure of the physical-fitness component Speed. Again, sports scientists are not very happy with this proposal.

Finally, HeC1 contrasts the fitness components Endurance, indicated best by Run (1), and Coordination, indicated by Star_r (2). Endurance (i.e., running for 6 minutes) is considered to be the best indicator of health-related status among the five tests because it is a rather pure measure of cardiorespiratory fitness. The Star_r test requires execution of a pre-instructed sequence of forward, sideways, and backward runs. This coordination of body movements implies a demand on working memory (i.e., remembering the order of these subruns) and executive control processes, but performance also depends on endurance. HeC1 yields a measure of Coordination “corrected” for the contribution of Endurance.

The statistical advantage of HelmertCoding is that the resulting contrasts are orthogonal (uncorrelated). This allows for optimal partitioning of variance and statistical power. It is also more efficient to estimate “orthogonal” than “non-orthogonal” random-effect structures.

contr2 = Dict(
  :Sex => EffectsCoding(; levels=["Girls", "Boys"]),
  :Test => HelmertCoding(;
    levels=["Run", "Star_r", "S20_r", "SLJ", "BPT"],
  ),
);
m_ovi_Helmert = lmm(f_ovi, dat; contrasts=contr2, progress)
Est. SE z p σ_Child
(Intercept) -0.0141 0.0141 -1.00 0.3166 0.7416
Test: Star_r -0.0097 0.0219 -0.44 0.6576
Test: S20_r -0.0032 0.0128 -0.25 0.8008
Test: SLJ -0.0015 0.0089 -0.17 0.8622
Test: BPT -0.0054 0.0069 -0.77 0.4403
a1 0.2482 0.0476 5.22 <1e-06
Sex: Boys 0.2068 0.0141 14.66 <1e-47
Test: Star_r & a1 0.1083 0.0746 1.45 0.1463
Test: S20_r & a1 0.0635 0.0430 1.48 0.1399
Test: SLJ & a1 0.0409 0.0302 1.35 0.1755
Test: BPT & a1 0.0739 0.0231 3.20 0.0014
Test: Star_r & Sex: Boys -0.0794 0.0219 -3.63 0.0003
Test: S20_r & Sex: Boys 0.0009 0.0128 0.07 0.9419
Test: SLJ & Sex: Boys -0.0011 0.0089 -0.13 0.8972
Test: BPT & Sex: Boys 0.0279 0.0069 4.02 <1e-04
a1 & Sex: Boys -0.0330 0.0476 -0.69 0.4875
Test: Star_r & a1 & Sex: Boys 0.0334 0.0746 0.45 0.6546
Test: S20_r & a1 & Sex: Boys -0.0134 0.0430 -0.31 0.7557
Test: SLJ & a1 & Sex: Boys 0.0127 0.0302 0.42 0.6739
Test: BPT & a1 & Sex: Boys 0.0092 0.0231 0.40 0.6900
Residual 0.6263

We forego a detailed discussion of the effects, but note that again none of the interactions between age x Sex with the four test contrasts was significant.

The default labeling of Helmert contrasts may lead to confusions with other contrasts. Therefore, we could provide our own labels:

labels=["c2.1", "c3.12", "c4.123", "c5.1234"]

Once the order of levels is memorized the proposed labelling is very transparent.

2.3 HypothesisCoding: contr3

The third set of contrasts uses HypothesisCoding. Hypothesis coding allows the user to specify their own a priori contrast matrix, subject to the mathematical constraint that the matrix has full rank. For example, sport scientists agree that the first four tests can be contrasted with BPT, because the difference is akin to a correction of overall physical fitness. However, they want to keep the pairwise comparisons for the first four tests.

  • HyC1: BPT - mean(1,2,3,4)
  • HyC2: Star_r - Run_r
  • HyC3: Run_r - S20_r
  • HyC4: S20_r - SLJ
contr3 = Dict(
  :Sex => EffectsCoding(; levels=["Girls", "Boys"]),
  :Test => HypothesisCoding(
    [
      -1 -1 -1 -1 +4
      -1 +1  0  0  0
       0 -1 +1  0  0
       0  0 -1 +1  0
    ];
    levels=["Run", "Star_r", "S20_r", "SLJ", "BPT"],
    labels=["BPT-other", "Star-End", "S20-Star", "SLJ-S20"],
  ),
);
m_ovi_Hypo = lmm(f_ovi, dat; contrasts=contr3, progress)
Est. SE z p σ_Child
(Intercept) -0.0141 0.0141 -1.00 0.3166 0.7416
Test: BPT-other -0.1070 0.1387 -0.77 0.4403
Test: Star-End -0.0194 0.0437 -0.44 0.6576
Test: S20-Star 0.0000 0.0442 0.00 0.9998
Test: SLJ-S20 0.0003 0.0440 0.01 0.9950
a1 0.2482 0.0476 5.22 <1e-06
Sex: Boys 0.2068 0.0141 14.66 <1e-47
Test: BPT-other & a1 1.4778 0.4622 3.20 0.0014
Test: Star-End & a1 0.2166 0.1491 1.45 0.1463
Test: S20-Star & a1 0.0821 0.1497 0.55 0.5835
Test: SLJ-S20 & a1 0.0366 0.1478 0.25 0.8047
Test: BPT-other & Sex: Boys 0.5582 0.1387 4.02 <1e-04
Test: Star-End & Sex: Boys -0.1587 0.0437 -3.63 0.0003
Test: S20-Star & Sex: Boys 0.0822 0.0442 1.86 0.0631
Test: SLJ-S20 & Sex: Boys -0.0065 0.0440 -0.15 0.8832
a1 & Sex: Boys -0.0330 0.0476 -0.69 0.4875
Test: BPT-other & a1 & Sex: Boys 0.1843 0.4622 0.40 0.6900
Test: Star-End & a1 & Sex: Boys 0.0667 0.1491 0.45 0.6546
Test: S20-Star & a1 & Sex: Boys -0.0735 0.1497 -0.49 0.6234
Test: SLJ-S20 & a1 & Sex: Boys 0.0775 0.1478 0.52 0.5999
Residual 0.6263

With HypothesisCoding we must generate our own labels for the contrasts. The default labeling of contrasts is usually not interpretable. Therefore, we provide our own.

Anyway, none of the interactions between age x Sex with the four Test contrasts was significant for these contrasts.

contr1b = Dict(
  :Sex => EffectsCoding(; levels=["Girls", "Boys"]),
  :Test => HypothesisCoding(
    [
      -1 +1  0  0  0
       0 -1 +1  0  0
       0  0 -1 +1  0
       0  0  0 -1 +1
    ];
    levels=["Run", "Star_r", "S20_r", "SLJ", "BPT"],
    labels=["Star-Run", "S20-Star", "SLJ-S20", "BPT-SLJ"],
  ),
);
m_ovi_SeqDiff_v2 = lmm(f_ovi, dat; contrasts=contr1b, progress)
Est. SE z p σ_Child
(Intercept) -0.0141 0.0141 -1.00 0.3166 0.7416
Test: Star-Run -0.0194 0.0437 -0.44 0.6576
Test: S20-Star 0.0000 0.0442 0.00 0.9998
Test: SLJ-S20 0.0003 0.0440 0.01 0.9950
Test: BPT-SLJ -0.0221 0.0436 -0.51 0.6119
a1 0.2482 0.0476 5.22 <1e-06
Sex: Boys 0.2068 0.0141 14.66 <1e-47
Test: Star-Run & a1 0.2166 0.1491 1.45 0.1463
Test: S20-Star & a1 0.0821 0.1497 0.55 0.5835
Test: SLJ-S20 & a1 0.0366 0.1478 0.25 0.8047
Test: BPT-SLJ & a1 0.2468 0.1465 1.68 0.0920
Test: Star-Run & Sex: Boys -0.1587 0.0437 -3.63 0.0003
Test: S20-Star & Sex: Boys 0.0822 0.0442 1.86 0.0631
Test: SLJ-S20 & Sex: Boys -0.0065 0.0440 -0.15 0.8832
Test: BPT-SLJ & Sex: Boys 0.1430 0.0436 3.28 0.0010
a1 & Sex: Boys -0.0330 0.0476 -0.69 0.4875
Test: Star-Run & a1 & Sex: Boys 0.0667 0.1491 0.45 0.6546
Test: S20-Star & a1 & Sex: Boys -0.0735 0.1497 -0.49 0.6234
Test: SLJ-S20 & a1 & Sex: Boys 0.0775 0.1478 0.52 0.5999
Test: BPT-SLJ & a1 & Sex: Boys 0.0080 0.1465 0.05 0.9565
Residual 0.6263
m_zcp_SeqD = lmm(@formula(zScore ~ 1 + Test * a1 * Sex + zerocorr(1 + Test | Child)),
                 dat; contrasts=contr1b, progress)
Est. SE z p σ_Child
(Intercept) -0.0142 0.0141 -1.01 0.3134 0.7445
Test: Star-Run -0.0179 0.0442 -0.41 0.6852 0.1133
Test: S20-Star 0.0003 0.0443 0.01 0.9939 0.4097
Test: SLJ-S20 -0.0005 0.0436 -0.01 0.9908 0.0674
Test: BPT-SLJ -0.0228 0.0433 -0.53 0.5983 0.0000
a1 0.2483 0.0476 5.22 <1e-06
Sex: Boys 0.2069 0.0141 14.66 <1e-47
Test: Star-Run & a1 0.2196 0.1508 1.46 0.1454
Test: S20-Star & a1 0.0794 0.1499 0.53 0.5963
Test: SLJ-S20 & a1 0.0334 0.1466 0.23 0.8199
Test: BPT-SLJ & a1 0.2511 0.1453 1.73 0.0840
Test: Star-Run & Sex: Boys -0.1582 0.0442 -3.58 0.0003
Test: S20-Star & Sex: Boys 0.0820 0.0443 1.85 0.0639
Test: SLJ-S20 & Sex: Boys -0.0064 0.0436 -0.15 0.8827
Test: BPT-SLJ & Sex: Boys 0.1426 0.0433 3.30 0.0010
a1 & Sex: Boys -0.0333 0.0476 -0.70 0.4836
Test: Star-Run & a1 & Sex: Boys 0.0655 0.1508 0.43 0.6640
Test: S20-Star & a1 & Sex: Boys -0.0710 0.1499 -0.47 0.6357
Test: SLJ-S20 & a1 & Sex: Boys 0.0755 0.1466 0.52 0.6065
Test: BPT-SLJ & a1 & Sex: Boys 0.0093 0.1453 0.06 0.9491
Residual 0.5872
m_zcp_SeqD_2 = lmm(@formula(zScore ~ 1 + Test * a1 * Sex + (0 + Test | Child)),
                   dat; contrasts=contr1b, progress)
Est. SE z p σ_Child
(Intercept) -0.0150 0.0141 -1.07 0.2869
Test: Star-Run -0.0166 0.0444 -0.37 0.7086
Test: S20-Star 0.0050 0.0442 0.11 0.9103
Test: SLJ-S20 -0.0093 0.0438 -0.21 0.8312
Test: BPT-SLJ -0.0127 0.0430 -0.30 0.7677
a1 0.2485 0.0474 5.24 <1e-06
Sex: Boys 0.2066 0.0141 14.68 <1e-48
Test: Star-Run & a1 0.2031 0.1515 1.34 0.1800
Test: S20-Star & a1 0.0993 0.1501 0.66 0.5082
Test: SLJ-S20 & a1 0.0183 0.1470 0.12 0.9008
Test: BPT-SLJ & a1 0.2475 0.1443 1.72 0.0862
Test: Star-Run & Sex: Boys -0.1490 0.0444 -3.36 0.0008
Test: S20-Star & Sex: Boys 0.0710 0.0442 1.61 0.1082
Test: SLJ-S20 & Sex: Boys -0.0059 0.0438 -0.14 0.8925
Test: BPT-SLJ & Sex: Boys 0.1475 0.0430 3.43 0.0006
a1 & Sex: Boys -0.0351 0.0474 -0.74 0.4591
Test: Star-Run & a1 & Sex: Boys 0.0773 0.1515 0.51 0.6097
Test: S20-Star & a1 & Sex: Boys -0.0681 0.1501 -0.45 0.6501
Test: SLJ-S20 & a1 & Sex: Boys 0.0424 0.1470 0.29 0.7730
Test: BPT-SLJ & a1 & Sex: Boys 0.0201 0.1443 0.14 0.8894
Test: Run 0.9681
Test: Star_r 0.9869
Test: S20_r 0.9800
Test: SLJ 0.9758
Test: BPT 0.9302
Residual 0.0001
m_cpx_0_SeqDiff = lmm(@formula(zScore ~ 1 + Test * a1 * Sex + (0 + Test | Child)),
                      dat; contrasts=contr1b, progress)
Est. SE z p σ_Child
(Intercept) -0.0150 0.0141 -1.07 0.2869
Test: Star-Run -0.0166 0.0444 -0.37 0.7086
Test: S20-Star 0.0050 0.0442 0.11 0.9103
Test: SLJ-S20 -0.0093 0.0438 -0.21 0.8312
Test: BPT-SLJ -0.0127 0.0430 -0.30 0.7677
a1 0.2485 0.0474 5.24 <1e-06
Sex: Boys 0.2066 0.0141 14.68 <1e-48
Test: Star-Run & a1 0.2031 0.1515 1.34 0.1800
Test: S20-Star & a1 0.0993 0.1501 0.66 0.5082
Test: SLJ-S20 & a1 0.0183 0.1470 0.12 0.9008
Test: BPT-SLJ & a1 0.2475 0.1443 1.72 0.0862
Test: Star-Run & Sex: Boys -0.1490 0.0444 -3.36 0.0008
Test: S20-Star & Sex: Boys 0.0710 0.0442 1.61 0.1082
Test: SLJ-S20 & Sex: Boys -0.0059 0.0438 -0.14 0.8925
Test: BPT-SLJ & Sex: Boys 0.1475 0.0430 3.43 0.0006
a1 & Sex: Boys -0.0351 0.0474 -0.74 0.4591
Test: Star-Run & a1 & Sex: Boys 0.0773 0.1515 0.51 0.6097
Test: S20-Star & a1 & Sex: Boys -0.0681 0.1501 -0.45 0.6501
Test: SLJ-S20 & a1 & Sex: Boys 0.0424 0.1470 0.29 0.7730
Test: BPT-SLJ & a1 & Sex: Boys 0.0201 0.1443 0.14 0.8894
Test: Run 0.9681
Test: Star_r 0.9869
Test: S20_r 0.9800
Test: SLJ 0.9758
Test: BPT 0.9302
Residual 0.0001
VarCorr(m_cpx_0_SeqDiff)
Column Variance Std.Dev Corr.
Child Test: Run 0.937219628 0.968101042
Test: Star_r 0.973977562 0.986903016 -0.09
Test: S20_r 0.960441075 0.980020957 +0.39 +0.83
Test: SLJ 0.952186829 0.975800609 +0.57 +0.40 +0.82
Test: BPT 0.865324962 0.930228446 +0.18 +0.50 +0.50 +0.29
Residual 0.000000016 0.000125318
m_cpx_0_SeqDiff.PCA
(Child = 
Principal components based on correlation matrix
 Test: Run      1.0     .      .      .      .
 Test: Star_r  -0.09   1.0     .      .      .
 Test: S20_r    0.39   0.83   1.0     .      .
 Test: SLJ      0.57   0.4    0.82   1.0     .
 Test: BPT      0.18   0.5    0.5    0.29   1.0

Normalized cumulative variances:
[0.5747, 0.8192, 0.9516, 0.9996, 1.0]

Component loadings
                 PC1    PC2    PC3    PC4    PC5
 Test: Run     -0.28   0.73   0.26   0.54  -0.15
 Test: Star_r  -0.45  -0.52  -0.25   0.45  -0.51
 Test: S20_r   -0.58  -0.06  -0.24   0.11   0.77
 Test: SLJ     -0.49   0.33  -0.29  -0.66  -0.34
 Test: BPT     -0.38  -0.28   0.85  -0.23  -0.0,)
f_cpx_1 = @formula(zScore ~ 1 + Test * a1 * Sex + (1 + Test | Child))
m_cpx_1_SeqDiff = lmm(f_cpx_1, dat; contrasts=contr1b, progress)
Est. SE z p σ_Child
(Intercept) -0.0146 0.0141 -1.03 0.3013 0.7751
Test: Star-Run -0.0200 0.0440 -0.46 0.6489 1.0005
Test: S20-Star 0.0027 0.0442 0.06 0.9509 0.5566
Test: SLJ-S20 -0.0062 0.0440 -0.14 0.8878 0.6230
Test: BPT-SLJ -0.0173 0.0430 -0.40 0.6874 0.9671
a1 0.2484 0.0476 5.22 <1e-06
Sex: Boys 0.2070 0.0141 14.68 <1e-48
Test: Star-Run & a1 0.2066 0.1502 1.38 0.1689
Test: S20-Star & a1 0.1010 0.1500 0.67 0.5007
Test: SLJ-S20 & a1 0.0177 0.1477 0.12 0.9047
Test: BPT-SLJ & a1 0.2551 0.1443 1.77 0.0771
Test: Star-Run & Sex: Boys -0.1489 0.0440 -3.38 0.0007
Test: S20-Star & Sex: Boys 0.0722 0.0442 1.63 0.1024
Test: SLJ-S20 & Sex: Boys -0.0051 0.0440 -0.11 0.9085
Test: BPT-SLJ & Sex: Boys 0.1438 0.0430 3.35 0.0008
a1 & Sex: Boys -0.0347 0.0476 -0.73 0.4659
Test: Star-Run & a1 & Sex: Boys 0.0752 0.1502 0.50 0.6167
Test: S20-Star & a1 & Sex: Boys -0.0768 0.1500 -0.51 0.6088
Test: SLJ-S20 & a1 & Sex: Boys 0.0551 0.1477 0.37 0.7089
Test: BPT-SLJ & a1 & Sex: Boys 0.0185 0.1443 0.13 0.8979
Residual 0.0001
m_cpx_1_SeqDiff.PCA
(Child = 
Principal components based on correlation matrix
 (Intercept)      1.0     .      .      .      .
 Test: Star-Run   0.01   1.0     .      .      .
 Test: S20-Star   0.29  -0.63   1.0     .      .
 Test: SLJ-S20   -0.24  -0.26   0.53   1.0     .
 Test: BPT-SLJ   -0.15  -0.17  -0.47  -0.23   1.0

Normalized cumulative variances:
[0.4179, 0.6736, 0.8943, 0.9879, 1.0]

Component loadings
                   PC1    PC2    PC3    PC4    PC5
 (Intercept)     -0.12  -0.68  -0.51  -0.46   0.24
 Test: Star-Run   0.45  -0.39   0.53  -0.36  -0.48
 Test: S20-Star  -0.67  -0.11  -0.12   0.08  -0.72
 Test: SLJ-S20   -0.48   0.32   0.41  -0.66   0.25
 Test: BPT-SLJ    0.33   0.53  -0.53  -0.45  -0.36,)

2.4 PCA-based HypothesisCoding: contr4

The fourth set of contrasts uses HypothesisCoding to specify the set of contrasts implementing the loadings of the four principal components of the published LMM based on test scores, not test effects (contrasts) — coarse-grained, that is roughly according to their signs. This is actually a very interesting and plausible solution nobody had proposed a priori.

  • PC1: BPT - Run_r
  • PC2: (Star_r + S20_r + SLJ) - (BPT + Run_r)
  • PC3: Star_r - (S20_r + SLJ)
  • PC4: S20_r - SLJ

PC1 contrasts the worst and the best indicator of physical health; PC2 contrasts these two against the core indicators of physical fitness; PC3 contrasts the cognitive and the physical tests within the narrow set of physical fitness components; and PC4, finally, contrasts two types of lower muscular fitness differing in speed and power.

contr4 = Dict(
  :Sex => EffectsCoding(; levels=["Girls", "Boys"]),
  :Test => HypothesisCoding(
    [
      -1  0  0  0 +1
      -3 +2 +2 +2 -3
       0 +2 -1 -1  0
       0  0 +1 -1  0
    ];
    levels=["Run", "Star_r", "S20_r", "SLJ", "BPT"],
    labels=["c5.1", "c234.15", "c2.34", "c3.4"],
  ),
);
m_cpx_1_PC = lmm(f_cpx_1, dat; contrasts=contr4, progress)
Est. SE z p σ_Child
(Intercept) -0.0146 0.0141 -1.04 0.2996 0.7936
Test: c5.1 -0.0346 0.0433 -0.80 0.4246 1.2906
Test: c234.15 0.0068 0.1682 0.04 0.9678 0.9130
Test: c2.34 -0.0067 0.0760 -0.09 0.9297 1.4367
Test: c3.4 0.0036 0.0440 0.08 0.9346 0.8450
a1 0.2472 0.0476 5.19 <1e-06
Sex: Boys 0.2070 0.0141 14.66 <1e-47
Test: c5.1 & a1 0.5727 0.1452 3.95 <1e-04
Test: c234.15 & a1 -0.0007 0.5666 -0.00 0.9991
Test: c2.34 & a1 -0.2267 0.2599 -0.87 0.3831
Test: c3.4 & a1 -0.0174 0.1479 -0.12 0.9061
Test: c5.1 & Sex: Boys 0.0632 0.0433 1.46 0.1445
Test: c234.15 & Sex: Boys -0.8152 0.1682 -4.85 <1e-05
Test: c2.34 & Sex: Boys -0.1368 0.0760 -1.80 0.0719
Test: c3.4 & Sex: Boys 0.0073 0.0440 0.17 0.8678
a1 & Sex: Boys -0.0355 0.0476 -0.74 0.4565
Test: c5.1 & a1 & Sex: Boys 0.0749 0.1452 0.52 0.6059
Test: c234.15 & a1 & Sex: Boys -0.0185 0.5666 -0.03 0.9740
Test: c2.34 & a1 & Sex: Boys 0.1083 0.2599 0.42 0.6770
Test: c3.4 & a1 & Sex: Boys -0.0885 0.1479 -0.60 0.5495
Residual 0.0001
VarCorr(m_cpx_1_PC)
Column Variance Std.Dev Corr.
Child (Intercept) 0.629825102 0.793615210
Test: c5.1 1.665691713 1.290616795 -0.04
Test: c234.15 0.833651232 0.913045033 +0.95 +0.03
Test: c2.34 2.064063986 1.436685068 +0.02 +0.28 +0.10
Test: c3.4 0.714025360 0.845000213 +0.22 +0.22 +0.07 +0.70
Residual 0.000000017 0.000130218
m_cpx_1_PC.PCA
(Child = 
Principal components based on correlation matrix
 (Intercept)     1.0     .      .      .     .
 Test: c5.1     -0.04   1.0     .      .     .
 Test: c234.15   0.95   0.03   1.0     .     .
 Test: c2.34     0.02   0.28   0.1    1.0    .
 Test: c3.4      0.22   0.22   0.07   0.7   1.0

Normalized cumulative variances:
[0.4199, 0.762, 0.9325, 1.0, 1.0]

Component loadings
                  PC1    PC2    PC3    PC4    PC5
 (Intercept)    -0.57   0.41  -0.0    0.21  -0.67
 Test: c5.1     -0.18  -0.37  -0.91   0.11  -0.04
 Test: c234.15  -0.56   0.41  -0.12  -0.28   0.65
 Test: c2.34    -0.38  -0.54   0.22  -0.68  -0.23
 Test: c3.4     -0.42  -0.48   0.34   0.64   0.27,)

There is a numerical interaction with a z-value > 2.0 for the first PCA (i.e., BPT - Run_r). This interaction would really need to be replicated to be taken seriously. It is probably due to larger “unfitness” gains in boys than girls (i.e., in BPT) relative to the slightly larger health-related “fitness” gains of girls than boys (i.e., in Run_r).

contr4b = Dict(
    :Sex => EffectsCoding(; levels=["Girls", "Boys"]),
    :Test => HypothesisCoding(
      [
        0.49 -0.04  0.20  0.03 -0.85
        0.70 -0.56 -0.21 -0.13  0.37
        0.31  0.68 -0.56 -0.35  0.00
        0.04  0.08  0.61 -0.78  0.13
      ];
      levels=["Run", "Star_r", "S20_r", "SLJ", "BPT"],
      labels=["c5.1", "c234.15", "c12.34", "c3.4"],
    )
);
# LN_NEWUOA does a poor job here
m_cpx_1_PC_2 = lmm(f_cpx_1, dat; contrasts=contr4b, progress, optimizer=:LN_BOBYQA)
Est. SE z p σ_Child
(Intercept) -0.0142 0.0142 -0.99 0.3200 0.7779
Test: c5.1 0.0275 0.0305 0.90 0.3671 0.6199
Test: c234.15 0.0042 0.0313 0.13 0.8938 0.8361
Test: c12.34 0.0033 0.0311 0.11 0.9146 0.5474
Test: c3.4 0.0018 0.0312 0.06 0.9540 0.5816
a1 0.2407 0.0481 5.00 <1e-06
Sex: Boys 0.1994 0.0142 13.99 <1e-43
Test: c5.1 & a1 -0.3788 0.1017 -3.72 0.0002
Test: c234.15 & a1 -0.0548 0.1062 -0.52 0.6055
Test: c12.34 & a1 -0.1716 0.1059 -1.62 0.1050
Test: c3.4 & a1 0.0049 0.1050 0.05 0.9631
Test: c5.1 & Sex: Boys -0.0735 0.0305 -2.41 0.0158
Test: c234.15 & Sex: Boys 0.1447 0.0313 4.63 <1e-05
Test: c12.34 & Sex: Boys -0.0203 0.0311 -0.65 0.5144
Test: c3.4 & Sex: Boys 0.0186 0.0312 0.60 0.5503
a1 & Sex: Boys -0.0357 0.0481 -0.74 0.4586
Test: c5.1 & a1 & Sex: Boys -0.0661 0.1017 -0.65 0.5158
Test: c234.15 & a1 & Sex: Boys -0.0301 0.1062 -0.28 0.7770
Test: c12.34 & a1 & Sex: Boys 0.0320 0.1059 0.30 0.7622
Test: c3.4 & a1 & Sex: Boys -0.0406 0.1050 -0.39 0.6992
Residual 0.0000
VarCorr(m_cpx_1_PC_2)
Column Variance Std.Dev Corr.
Child (Intercept) 0.60518808 0.77793836
Test: c5.1 0.38431538 0.61993175 +0.05
Test: c234.15 0.69901697 0.83607235 -0.15 +0.07
Test: c12.34 0.29963320 0.54738762 -0.21 +0.13 +0.03
Test: c3.4 0.33828266 0.58162072 +0.06 -0.54 -0.53 +0.53
Residual 0.00000000 0.00001238
m_cpx_1_PC_2.PCA
(Child = 
Principal components based on correlation matrix
 (Intercept)     1.0     .      .      .      .
 Test: c5.1      0.05   1.0     .      .      .
 Test: c234.15  -0.15   0.07   1.0     .      .
 Test: c12.34   -0.21   0.13   0.03   1.0     .
 Test: c3.4      0.06  -0.54  -0.53   0.53   1.0

Normalized cumulative variances:
[0.3795, 0.6371, 0.8447, 0.995, 1.0]

Component loadings
                  PC1    PC2    PC3    PC4    PC5
 (Intercept)    -0.02  -0.63   0.41  -0.65  -0.12
 Test: c5.1      0.41   0.19   0.76   0.2    0.42
 Test: c234.15   0.44   0.37  -0.35  -0.65   0.34
 Test: c12.34   -0.35   0.66   0.37  -0.31  -0.47
 Test: c3.4     -0.72   0.04   0.03  -0.12   0.69,)
# LN_BOBYQA does a poor job here
m_zcp_1_PC_2 = lmm(@formula(zScore ~ 1 + Test*a1*Sex + zerocorr(1 + Test | Child)),
                   dat; contrasts=contr4b, progress, optimizer=:LN_NEWUOA)
Est. SE z p σ_Child
(Intercept) -0.0136 0.0142 -0.96 0.3366 0.7604
Test: c5.1 0.0283 0.0311 0.91 0.3634 0.6097
Test: c234.15 0.0050 0.0313 0.16 0.8735 0.7150
Test: c12.34 0.0056 0.0311 0.18 0.8563 0.6571
Test: c3.4 -0.0012 0.0311 -0.04 0.9703 0.7024
a1 0.2401 0.0479 5.01 <1e-06
Sex: Boys 0.1997 0.0142 14.07 <1e-44
Test: c5.1 & a1 -0.3848 0.1039 -3.70 0.0002
Test: c234.15 & a1 -0.0590 0.1061 -0.56 0.5783
Test: c12.34 & a1 -0.1566 0.1057 -1.48 0.1386
Test: c3.4 & a1 -0.0046 0.1049 -0.04 0.9652
Test: c5.1 & Sex: Boys -0.0722 0.0311 -2.32 0.0203
Test: c234.15 & Sex: Boys 0.1467 0.0313 4.69 <1e-05
Test: c12.34 & Sex: Boys -0.0306 0.0311 -0.98 0.3263
Test: c3.4 & Sex: Boys 0.0180 0.0311 0.58 0.5632
a1 & Sex: Boys -0.0355 0.0479 -0.74 0.4586
Test: c5.1 & a1 & Sex: Boys -0.0626 0.1039 -0.60 0.5469
Test: c234.15 & a1 & Sex: Boys -0.0265 0.1061 -0.25 0.8028
Test: c12.34 & a1 & Sex: Boys 0.0181 0.1057 0.17 0.8643
Test: c3.4 & a1 & Sex: Boys -0.0431 0.1049 -0.41 0.6814
Residual 0.0000
VarCorr(m_zcp_1_PC_2)
Column Variance Std.Dev Corr.
Child (Intercept) 0.57825046 0.76042781
Test: c5.1 0.37173689 0.60970229 .
Test: c234.15 0.51125007 0.71501753 . .
Test: c12.34 0.43172795 0.65706008 . . .
Test: c3.4 0.49343314 0.70244796 . . . .
Residual 0.00000000 0.00001523
likelihoodratiotest(m_zcp_1_PC_2, m_cpx_1_PC_2)
model-dof -2 logLik χ² χ²-dof P(>χ²)
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + zerocorr(1 + Test | Child) 26 -13338
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -13316 21 10 0.0182

3 Other topics

3.1 Contrasts are re-parameterizations of the same model

The choice of contrast does not affect the model objective, in other words, they all yield the same goodness of fit. It does not matter whether a contrast is orthogonal or not.

[
  objective(m_ovi_SeqDiff),
  objective(m_ovi_Helmert),
  objective(m_ovi_Hypo),
]
3-element Vector{Float64}:
 13850.583681910179
 13850.583681910199
 13850.583681910199

3.2 VCs and CPs depend on contrast coding

Trivially, the meaning of a contrast depends on its definition. Consequently, the contrast specification has a big effect on the random-effect structure. As an illustration, we refit the LMMs with variance components (VCs) and correlation parameters (CPs) for Child-related contrasts of Test. Unfortunately, it is not easy, actually rather quite difficult, to grasp the meaning of correlations of contrast-based effects; they represent two-way interactions.

f_Child = @formula(zScore ~ 1 + Test * a1 * Sex + (1 + Test | Child))
m_Child_SDC = lmm(f_Child, dat; contrasts=contr1, progress, optimizer=:LN_BOBYQA)
m_Child_HeC = lmm(f_Child, dat; contrasts=contr2, progress, optimizer=:LN_NELDERMEAD)
m_Child_HyC = lmm(f_Child, dat; contrasts=contr3, progress, optimizer=:LN_NELDERMEAD)
m_Child_PCA = lmm(f_Child, dat; contrasts=contr4, progress, optimizer=:LN_NELDERMEAD)
VarCorr(m_Child_SDC)
Column Variance Std.Dev Corr.
Child (Intercept) 0.48783889 0.69845465
Test: Star_r 1.62767855 1.27580506 +0.19
Test: S20_r 0.34498305 0.58735258 +0.23 -0.45
Test: SLJ 0.37753928 0.61444225 -0.17 -0.84 +0.57
Test: BPT 1.55499912 1.24699604 -0.27 +0.39 -0.65 -0.46
Residual 0.00000000 0.00001256
VarCorr(m_Child_HeC)
Column Variance Std.Dev Corr.
Child (Intercept) 0.33333945 0.57735557
Test: Star_r 0.24798903 0.49798497 +0.25
Test: S20_r 0.02079654 0.14421007 +0.21 +0.16
Test: SLJ 0.06967644 0.26396295 -0.18 +0.56 +0.42
Test: BPT 0.07933472 0.28166419 -0.49 -0.14 -0.66 +0.06
Residual 0.00000000 0.00000053
VarCorr(m_Child_HyC)
Column Variance Std.Dev Corr.
Child (Intercept) 0.22886955 0.47840313
Test: BPT-other 10.79841528 3.28609423 +0.23
Test: Star-End 1.63382873 1.27821310 +0.28 +0.64
Test: S20-Star 2.67241412 1.63475200 +0.10 -0.27 -0.44
Test: SLJ-S20 0.20241726 0.44990806 +0.01 +0.84 +0.53 -0.31
Residual 0.00000000 0.00000045
VarCorr(m_Child_PCA)
Column Variance Std.Dev Corr.
Child (Intercept) 0.5806513 0.7620048
Test: c5.1 0.3762040 0.6133547 +0.12
Test: c234.15 5.4762773 2.3401447 -0.47 +0.32
Test: c2.34 6.4207912 2.5339280 -0.23 +0.66 +0.28
Test: c3.4 0.7566857 0.8698768 +0.27 +0.23 +0.11 +0.59
Residual 0.0000000 0.0000005

The CPs for the various contrasts are in line with expectations. For the SDC we observe substantial negative CPs between neighboring contrasts. For the orthogonal HeC, all CPs are small; they are uncorrelated. HyC contains some of the SDC contrasts and we observe again the negative CPs. The (roughly) PCA-based contrasts are small with one exception; there is a sizeable CP of +.41 between GM and the core of adjusted physical fitness (c234.15).

Do these differences in CPs imply that we can move to zcpLMMs when we have orthogonal contrasts? We pursue this question with by refitting the four LMMs with zerocorr() and compare the goodness of fit.

f_Child0 = @formula(zScore ~ 1 + Test * a1 * Sex + zerocorr(1 + Test | Child))
m_Child_SDC0 = lmm(f_Child0, dat; contrasts=contr1, progress, optimizer=:LN_NEWUOA)
m_Child_HeC0 = lmm(f_Child0, dat; contrasts=contr2, progress, optimizer=:LN_NEWUOA)
m_Child_HyC0 = lmm(f_Child0, dat; contrasts=contr3, progress, optimizer=:LN_NEWUOA)
m_Child_PCA0 = lmm(f_Child0, dat; contrasts=contr4, progress, optimizer=:LN_NEWUOA)
likelihoodratiotest(m_Child_SDC0, m_Child_SDC)
model-dof -2 logLik χ² χ²-dof P(>χ²)
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + zerocorr(1 + Test | Child) 26 -13849
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -13314 535 10 <1e-99
likelihoodratiotest(m_Child_HeC0, m_Child_HeC)
model-dof -2 logLik χ² χ²-dof P(>χ²)
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + zerocorr(1 + Test | Child) 26 -13326
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -13189 137 10 <1e-23
likelihoodratiotest(m_Child_HyC0, m_Child_HyC)
model-dof -2 logLik χ² χ²-dof P(>χ²)
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + zerocorr(1 + Test | Child) 26 -13427
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -13194 233 10 <1e-43
likelihoodratiotest(m_Child_PCA0, m_Child_PCA)
model-dof -2 logLik χ² χ²-dof P(>χ²)
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + zerocorr(1 + Test | Child) 26 -13335
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -13184 151 10 <1e-26

Obviously, we can not drop CPs from any of the LMMs. The full LMMs all have the same objective, but we can compare the goodness-of-fit statistics of zcpLMMs more directly.

zcpLMM = ["SDC0", "HeC0", "HyC0", "PCA0"]
mods = [m_Child_SDC0, m_Child_HeC0, m_Child_HyC0, m_Child_PCA0]
gof_summary = sort!(
  DataFrame(;
    zcpLMM=zcpLMM,
    dof=dof.(mods),
    deviance=deviance.(mods),
    AIC=aic.(mods),
    BIC=bic.(mods),
  ),
  :deviance,
)
4×5 DataFrame
Row zcpLMM dof deviance AIC BIC
String Int64 Float64 Float64 Float64
1 HeC0 26 13326.1 13378.1 13547.5
2 PCA0 26 13335.0 13387.0 13556.4
3 HyC0 26 13427.0 13479.0 13648.4
4 SDC0 26 13849.1 13901.1 14070.6

The best fit was obtained for the PCA-based zcpLMM. Somewhat surprisingly the second best fit was obtained for the SDC. The relatively poor performance of HeC-based zcpLMM is puzzling to me. I thought it might be related to imbalance in design in the present data, but this does not appear to be the case. The same comparison of SequentialDifferenceCoding and Helmert Coding also showed a worse fit for the zcp-HeC LMM than the zcp-SDC LMM.

3.3 VCs and CPs depend on random factor

VCs and CPs resulting from a set of test contrasts can also be estimated for the random factor School. Of course, these VCs and CPs may look different from the ones we just estimated for Child.

The effect of age (i.e., developmental gain) varies within School. Therefore, we also include its VCs and CPs in this model; the school-related VC for Sex was not significant.

f_School = @formula(zScore ~ 1 + Test * a1 * Sex + (1 + Test + a1 | School))
m_School_SeqDiff = lmm(f_School, dat; contrasts=contr1, progress)
m_School_Helmert = lmm(f_School, dat; contrasts=contr2, progress)
m_School_Hypo = lmm(f_School, dat; contrasts=contr3, progress)
m_School_PCA = lmm(f_School, dat; contrasts=contr4, progress)
VarCorr(m_School_SeqDiff)
Column Variance Std.Dev Corr.
School (Intercept) 0.0284417 0.1686466
Test: Star_r 0.2121253 0.4605706 +0.13
Test: S20_r 0.1889328 0.4346640 +0.00 -0.69
Test: SLJ 0.1842668 0.4292631 -0.11 +0.36 -0.62
Test: BPT 0.1554465 0.3942671 -0.36 -0.15 +0.17 -0.77
a1 0.0099399 0.0996988 -0.69 -0.04 +0.07 -0.29 +0.64
Residual 0.8534750 0.9238371
VarCorr(m_School_Helmert)
Column Variance Std.Dev Corr.
School (Intercept) 0.0284492 0.1686689
Test: Star_r 0.0530256 0.2302730 +0.13
Test: S20_r 0.0116199 0.1077955 +0.10 -0.21
Test: SLJ 0.0076896 0.0876903 -0.08 +0.31 -0.10
Test: BPT 0.0017617 0.0419723 -0.78 +0.12 +0.10 -0.38
a1 0.0096619 0.0982950 -0.70 -0.04 +0.06 -0.32 +0.82
Residual 0.8535026 0.9238521
VarCorr(m_School_Hypo)
Column Variance Std.Dev Corr.
School (Intercept) 0.0284428 0.1686501
Test: BPT-other 0.7047229 0.8394777 -0.78
Test: Star-End 0.2120779 0.4605192 +0.13 +0.12
Test: S20-Star 0.1888844 0.4346083 +0.00 +0.01 -0.69
Test: SLJ-S20 0.1842659 0.4292621 -0.11 -0.36 +0.36 -0.62
a1 0.0097332 0.0986571 -0.70 +0.83 -0.04 +0.07 -0.29
Residual 0.8534970 0.9238490
VarCorr(m_School_PCA)
Column Variance Std.Dev Corr.
School (Intercept) 0.0284352 0.1686274
Test: c5.1 0.1184426 0.3441548 -0.38
Test: c234.15 2.1883880 1.4793201 +0.44 +0.46
Test: c2.34 0.4737012 0.6882595 +0.06 +0.27 +0.08
Test: c3.4 0.1843236 0.4293292 +0.11 -0.06 -0.48 -0.17
a1 0.0099888 0.0999438 -0.69 +0.41 -0.44 +0.09 +0.29
Residual 0.8534805 0.9238401

We compare again how much of the fit resides in the CPs.

f_School0 = @formula(zScore ~ 1 + Test * a1 * Sex + zerocorr(1 + Test + a1 | School))
m_School_SDC0 = lmm(f_School0, dat; contrasts=contr1, progress)
m_School_HeC0 = lmm(f_School0, dat; contrasts=contr2, progress)
m_School_HyC0 = lmm(f_School0, dat; contrasts=contr3, progress)
m_School_PCA0 = lmm(f_School0, dat; contrasts=contr4, progress)

zcpLMM2 = ["SDC0", "HeC0", "HyC0", "PCA0"]
mods2 = [
  m_School_SDC0, m_School_HeC0, m_School_HyC0, m_School_PCA0
]
gof_summary2 = sort!(
  DataFrame(;
    zcpLMM=zcpLMM2,
    dof=dof.(mods2),
    deviance=deviance.(mods2),
    AIC=aic.(mods2),
    BIC=bic.(mods2),
  ),
  :deviance,
)
4×5 DataFrame
Row zcpLMM dof deviance AIC BIC
String Int64 Float64 Float64 Float64
1 HeC0 27 13835.4 13889.4 14065.4
2 PCA0 27 13839.4 13893.4 14069.3
3 HyC0 27 13846.1 13900.1 14076.1
4 SDC0 27 13856.0 13910.0 14085.9

For the random factor School the Helmert contrast, followed by PCA-based contrasts have least information in the CPs; SDC has the largest contribution from CPs. Interesting.

4 That’s it

That’s it for this tutorial. It is time to try your own contrast coding. You can use these data; there are many alternatives to set up hypotheses for the five tests. Of course and even better, code up some contrasts for data of your own.

Have fun!

Fühner, T., Granacher, U., Golle, K., & Kliegl, R. (2021). Age and sex effects in physical fitness components of 108,295 third graders including 515 primary schools and 9 cohorts. Scientific Reports, 11(1). https://doi.org/10.1038/s41598-021-97000-4

This page was rendered from git revision f875caa using Quarto 1.8.26.

Back to top