Mixed Models Tutorial: Contrast Coding

Author

Reinhold Kliegl

Published

2025-12-17

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 2017 S106082 C058365 8.56126 4.65116
2 S20_r Boys 2011 S101485 C041707 8.41889 4.65116
3 S20_r Boys 2019 S103305 C057023 8.55578 4.65116
4 S20_r Boys 2015 S101758 C083951 8.79671 3.92157
5 S20_r Boys 2018 S106781 C091332 8.85421 4.87805
6 S20_r Boys 2019 S102350 C028317 8.3039 4.65116
7 S20_r Boys 2015 S103639 C018651 8.21629 4.44444
8 S20_r Boys 2018 S110346 C101972 8.93908 4.44444
9 S20_r Boys 2018 S101710 C043098 8.44079 4.25532
10 S20_r Boys 2012 S110346 C022817 8.24914 4.44444
11 S20_r Boys 2018 S105170 C043726 8.44079 4.54545
12 S20_r Boys 2018 S101047 C100959 8.93908 5.12821
13 S20_r Boys 2013 S104206 C013046 8.16427 4.7619
4989 Run Girls 2018 S104784 C101583 8.93908 918.0
4990 Run Girls 2011 S111430 C089421 8.83231 1134.0
4991 Run Girls 2016 S104954 C044379 8.44627 1023.0
4992 Run Girls 2018 S100341 C033677 8.35866 990.0
4993 Run Girls 2019 S104280 C085142 8.80219 864.0
4994 Run Girls 2011 S104711 C008355 8.08487 1130.0
4995 Run Girls 2019 S110346 C038261 8.38877 855.0
4996 Run Girls 2012 S106677 C032124 8.33402 1140.0
4997 Run Girls 2019 S103330 C005643 8.05202 810.0
4998 Run Girls 2016 S111168 C094413 8.87885 1200.0
4999 Run Girls 2016 S100572 C083185 8.79398 918.0
5000 Run Girls 2016 S105764 C063541 8.61602 900.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 2017 S106082 C058365 8.56126 4.65116 0.0612594 0.330263
2 S20_r Boys 2011 S101485 C041707 8.41889 4.65116 -0.0811088 0.330263
3 S20_r Boys 2019 S103305 C057023 8.55578 4.65116 0.0557837 0.330263
4 S20_r Boys 2015 S101758 C083951 8.79671 3.92157 0.296715 -1.44817
5 S20_r Boys 2018 S106781 C091332 8.85421 4.87805 0.354209 0.883313
6 S20_r Boys 2019 S102350 C028317 8.3039 4.65116 -0.196099 0.330263
7 S20_r Boys 2015 S103639 C018651 8.21629 4.44444 -0.28371 -0.173627
8 S20_r Boys 2018 S110346 C101972 8.93908 4.44444 0.439083 -0.173627
9 S20_r Boys 2018 S101710 C043098 8.44079 4.25532 -0.059206 -0.634632
10 S20_r Boys 2012 S110346 C022817 8.24914 4.44444 -0.250856 -0.173627
11 S20_r Boys 2018 S105170 C043726 8.44079 4.54545 -0.059206 0.0725921
12 S20_r Boys 2018 S101047 C100959 8.93908 5.12821 0.439083 1.49309
13 S20_r Boys 2013 S104206 C013046 8.16427 4.7619 -0.335729 0.600204
4989 Run Girls 2018 S104784 C101583 8.93908 918.0 0.439083 -0.556439
4990 Run Girls 2011 S111430 C089421 8.83231 1134.0 0.332307 0.843864
4991 Run Girls 2016 S104954 C044379 8.44627 1023.0 -0.0537303 0.124264
4992 Run Girls 2018 S100341 C033677 8.35866 990.0 -0.141342 -0.0896712
4993 Run Girls 2019 S104280 C085142 8.80219 864.0 0.30219 -0.906514
4994 Run Girls 2011 S104711 C008355 8.08487 1130.0 -0.415127 0.817932
4995 Run Girls 2019 S110346 C038261 8.38877 855.0 -0.111225 -0.96486
4996 Run Girls 2012 S106677 C032124 8.33402 1140.0 -0.165982 0.882761
4997 Run Girls 2019 S103330 C005643 8.05202 810.0 -0.447981 -1.25659
4998 Run Girls 2016 S111168 C094413 8.87885 1200.0 0.37885 1.27173
4999 Run Girls 2016 S100572 C083185 8.79398 918.0 0.293977 -0.556439
5000 Run Girls 2016 S105764 C063541 8.61602 900.0 0.116016 -0.67313
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.59711 0.417368 0.198513 1.01736
2 BPT Boys 3.9978 0.703395 0.319386 0.988361
3 SLJ Boys 129.812 19.4448 0.224684 0.9993
4 Star_r Boys 2.05916 0.298452 0.131318 1.05729
5 Run Boys 1044.56 161.515 0.264048 1.04708
6 S20_r Girls 4.43423 0.386591 -0.198513 0.94234
7 BPT Girls 3.5432 0.644736 -0.319386 0.905938
8 SLJ Girls 121.068 18.4828 -0.224684 0.949861
9 Star_r Girls 1.98503 0.260185 -0.131318 0.921729
10 Run Girls 963.102 134.996 -0.264048 0.87516

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.0006 0.0143 -0.04 0.9641 0.7660
Test: Star_r 0.0046 0.0443 0.10 0.9173
Test: S20_r 0.0108 0.0443 0.24 0.8076
Test: SLJ -0.0127 0.0441 -0.29 0.7733
Test: BPT -0.0028 0.0442 -0.06 0.9492
Residual 0.6438

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.0204 0.0146 -1.40 0.1621 0.7581
Test: Star_r -0.0017 0.0452 -0.04 0.9695
Test: S20_r 0.0118 0.0451 0.26 0.7941
Test: SLJ -0.0075 0.0450 -0.17 0.8678
Test: BPT -0.0154 0.0452 -0.34 0.7327
a1 0.3024 0.0487 6.21 <1e-09
Test: Star_r & a1 0.1028 0.1487 0.69 0.4892
Test: S20_r & a1 0.0273 0.1525 0.18 0.8581
Test: SLJ & a1 -0.1173 0.1520 -0.77 0.4402
Test: BPT & a1 0.1914 0.1512 1.27 0.2056
Residual 0.6466

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.0179 0.0142 -1.26 0.2071 0.7147
Test: Star_r -0.0028 0.0441 -0.06 0.9500
Test: S20_r 0.0103 0.0440 0.23 0.8143
Test: SLJ -0.0063 0.0438 -0.14 0.8865
Test: BPT -0.0150 0.0440 -0.34 0.7328
a1 0.2658 0.0474 5.61 <1e-07
Sex: Boys 0.2231 0.0139 16.10 <1e-57
Test: Star_r & a1 0.1233 0.1451 0.85 0.3953
Test: S20_r & a1 0.0110 0.1488 0.07 0.9409
Test: SLJ & a1 -0.1118 0.1483 -0.75 0.4506
Test: BPT & a1 0.1864 0.1475 1.26 0.2063
Test: Star_r & Sex: Boys -0.1332 0.0431 -3.09 0.0020
Test: S20_r & Sex: Boys 0.0691 0.0431 1.61 0.1084
Test: SLJ & Sex: Boys 0.0321 0.0428 0.75 0.4542
Test: BPT & Sex: Boys 0.0809 0.0429 1.88 0.0594
Residual 0.6542

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.0189 0.0142 -1.33 0.1830 0.7098
Test: Star_r -0.0003 0.0441 -0.01 0.9938
Test: S20_r 0.0082 0.0440 0.19 0.8516
Test: SLJ -0.0052 0.0438 -0.12 0.9058
Test: BPT -0.0154 0.0441 -0.35 0.7266
a1 0.2652 0.0474 5.60 <1e-07
Sex: Boys 0.2185 0.0142 15.39 <1e-52
Test: Star_r & a1 0.1224 0.1451 0.84 0.3990
Test: S20_r & a1 0.0049 0.1489 0.03 0.9740
Test: SLJ & a1 -0.1032 0.1484 -0.70 0.4870
Test: BPT & a1 0.1882 0.1476 1.28 0.2023
Test: Star_r & Sex: Boys -0.1227 0.0441 -2.78 0.0054
Test: S20_r & Sex: Boys 0.0598 0.0440 1.36 0.1745
Test: SLJ & Sex: Boys 0.0380 0.0438 0.87 0.3864
Test: BPT & Sex: Boys 0.0792 0.0441 1.80 0.0723
a1 & Sex: Boys 0.0714 0.0474 1.51 0.1317
Test: Star_r & a1 & Sex: Boys -0.1528 0.1451 -1.05 0.2924
Test: S20_r & a1 & Sex: Boys 0.1616 0.1489 1.08 0.2779
Test: SLJ & a1 & Sex: Boys -0.1063 0.1484 -0.72 0.4737
Test: BPT & a1 & Sex: Boys 0.0267 0.1476 0.18 0.8564
Residual 0.6587

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.0189 0.0142 -1.33 0.1830 0.7098
Test: Star_r -0.0002 0.0221 -0.01 0.9938
Test: S20_r 0.0027 0.0127 0.21 0.8323
Test: SLJ 0.0000 0.0090 0.01 0.9957
Test: BPT -0.0031 0.0070 -0.44 0.6613
a1 0.2652 0.0474 5.60 <1e-07
Sex: Boys 0.2185 0.0142 15.39 <1e-52
Test: Star_r & a1 0.0612 0.0726 0.84 0.3990
Test: S20_r & a1 0.0220 0.0430 0.51 0.6084
Test: SLJ & a1 -0.0148 0.0300 -0.49 0.6217
Test: BPT & a1 0.0288 0.0233 1.23 0.2177
Test: Star_r & Sex: Boys -0.0614 0.0221 -2.78 0.0054
Test: S20_r & Sex: Boys -0.0005 0.0127 -0.04 0.9670
Test: SLJ & Sex: Boys 0.0092 0.0090 1.03 0.3028
Test: BPT & Sex: Boys 0.0214 0.0070 3.07 0.0022
a1 & Sex: Boys 0.0714 0.0474 1.51 0.1317
Test: Star_r & a1 & Sex: Boys -0.0764 0.0726 -1.05 0.2924
Test: S20_r & a1 & Sex: Boys 0.0284 0.0430 0.66 0.5088
Test: SLJ & a1 & Sex: Boys -0.0124 0.0300 -0.41 0.6792
Test: BPT & a1 & Sex: Boys -0.0021 0.0233 -0.09 0.9286
Residual 0.6587

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.0189 0.0142 -1.33 0.1830 0.7098
Test: BPT-other -0.0611 0.1394 -0.44 0.6613
Test: Star-End -0.0003 0.0441 -0.01 0.9938
Test: S20-Star 0.0082 0.0440 0.19 0.8516
Test: SLJ-S20 -0.0052 0.0438 -0.12 0.9058
a1 0.2652 0.0474 5.60 <1e-07
Sex: Boys 0.2185 0.0142 15.39 <1e-52
Test: BPT-other & a1 0.5754 0.4668 1.23 0.2177
Test: Star-End & a1 0.1224 0.1451 0.84 0.3990
Test: S20-Star & a1 0.0049 0.1489 0.03 0.9740
Test: SLJ-S20 & a1 -0.1032 0.1484 -0.70 0.4870
Test: BPT-other & Sex: Boys 0.4277 0.1394 3.07 0.0022
Test: Star-End & Sex: Boys -0.1227 0.0441 -2.78 0.0054
Test: S20-Star & Sex: Boys 0.0598 0.0440 1.36 0.1745
Test: SLJ-S20 & Sex: Boys 0.0380 0.0438 0.87 0.3864
a1 & Sex: Boys 0.0714 0.0474 1.51 0.1317
Test: BPT-other & a1 & Sex: Boys -0.0418 0.4668 -0.09 0.9286
Test: Star-End & a1 & Sex: Boys -0.1528 0.1451 -1.05 0.2924
Test: S20-Star & a1 & Sex: Boys 0.1616 0.1489 1.08 0.2779
Test: SLJ-S20 & a1 & Sex: Boys -0.1063 0.1484 -0.72 0.4737
Residual 0.6587

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.0189 0.0142 -1.33 0.1830 0.7098
Test: Star-Run -0.0003 0.0441 -0.01 0.9938
Test: S20-Star 0.0082 0.0440 0.19 0.8516
Test: SLJ-S20 -0.0052 0.0438 -0.12 0.9058
Test: BPT-SLJ -0.0154 0.0441 -0.35 0.7266
a1 0.2652 0.0474 5.60 <1e-07
Sex: Boys 0.2185 0.0142 15.39 <1e-52
Test: Star-Run & a1 0.1224 0.1451 0.84 0.3990
Test: S20-Star & a1 0.0049 0.1489 0.03 0.9740
Test: SLJ-S20 & a1 -0.1032 0.1484 -0.70 0.4870
Test: BPT-SLJ & a1 0.1882 0.1476 1.28 0.2023
Test: Star-Run & Sex: Boys -0.1227 0.0441 -2.78 0.0054
Test: S20-Star & Sex: Boys 0.0598 0.0440 1.36 0.1745
Test: SLJ-S20 & Sex: Boys 0.0380 0.0438 0.87 0.3864
Test: BPT-SLJ & Sex: Boys 0.0792 0.0441 1.80 0.0723
a1 & Sex: Boys 0.0714 0.0474 1.51 0.1317
Test: Star-Run & a1 & Sex: Boys -0.1528 0.1451 -1.05 0.2924
Test: S20-Star & a1 & Sex: Boys 0.1616 0.1489 1.08 0.2779
Test: SLJ-S20 & a1 & Sex: Boys -0.1063 0.1484 -0.72 0.4737
Test: BPT-SLJ & a1 & Sex: Boys 0.0267 0.1476 0.18 0.8564
Residual 0.6587
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.0189 0.0144 -1.31 0.1887 0.8012
Test: Star-Run -0.0404 0.0433 -0.93 0.3510 0.3754
Test: S20-Star 0.0341 0.0421 0.81 0.4178 0.7695
Test: SLJ-S20 -0.0253 0.0415 -0.61 0.5429 0.6663
Test: BPT-SLJ 0.0069 0.0446 0.16 0.8763 0.5233
a1 0.2667 0.0479 5.57 <1e-07
Sex: Boys 0.2183 0.0144 15.21 <1e-51
Test: Star-Run & a1 0.2908 0.1414 2.06 0.0397
Test: S20-Star & a1 -0.0721 0.1422 -0.51 0.6122
Test: SLJ-S20 & a1 -0.0768 0.1406 -0.55 0.5850
Test: BPT-SLJ & a1 0.1405 0.1496 0.94 0.3474
Test: Star-Run & Sex: Boys -0.1449 0.0433 -3.34 0.0008
Test: S20-Star & Sex: Boys 0.0720 0.0421 1.71 0.0875
Test: SLJ-S20 & Sex: Boys 0.0374 0.0415 0.90 0.3677
Test: BPT-SLJ & Sex: Boys 0.0759 0.0446 1.70 0.0886
a1 & Sex: Boys 0.0712 0.0479 1.49 0.1372
Test: Star-Run & a1 & Sex: Boys -0.0404 0.1414 -0.29 0.7749
Test: S20-Star & a1 & Sex: Boys 0.0797 0.1422 0.56 0.5753
Test: SLJ-S20 & a1 & Sex: Boys -0.0914 0.1406 -0.65 0.5153
Test: BPT-SLJ & a1 & Sex: Boys 0.0453 0.1496 0.30 0.7622
Residual 0.0000
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.0190 0.0141 -1.34 0.1793
Test: Star-Run -0.0073 0.0445 -0.16 0.8695
Test: S20-Star 0.0136 0.0448 0.30 0.7615
Test: SLJ-S20 -0.0116 0.0439 -0.26 0.7917
Test: BPT-SLJ -0.0128 0.0435 -0.29 0.7681
a1 0.2690 0.0472 5.70 <1e-07
Sex: Boys 0.2189 0.0141 15.49 <1e-53
Test: Star-Run & a1 0.1483 0.1461 1.01 0.3102
Test: S20-Star & a1 -0.0269 0.1512 -0.18 0.8586
Test: SLJ-S20 & a1 -0.0950 0.1485 -0.64 0.5225
Test: BPT-SLJ & a1 0.1903 0.1456 1.31 0.1911
Test: Star-Run & Sex: Boys -0.1214 0.0445 -2.73 0.0063
Test: S20-Star & Sex: Boys 0.0567 0.0448 1.27 0.2053
Test: SLJ-S20 & Sex: Boys 0.0394 0.0439 0.90 0.3689
Test: BPT-SLJ & Sex: Boys 0.0834 0.0435 1.92 0.0552
a1 & Sex: Boys 0.0693 0.0472 1.47 0.1418
Test: Star-Run & a1 & Sex: Boys -0.1716 0.1461 -1.17 0.2404
Test: S20-Star & a1 & Sex: Boys 0.1681 0.1512 1.11 0.2660
Test: SLJ-S20 & a1 & Sex: Boys -0.0967 0.1485 -0.65 0.5151
Test: BPT-SLJ & a1 & Sex: Boys 0.0090 0.1456 0.06 0.9506
Test: Run 0.9592
Test: Star_r 0.9874
Test: S20_r 0.9709
Test: SLJ 0.9677
Test: BPT 0.9398
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.0190 0.0141 -1.34 0.1793
Test: Star-Run -0.0073 0.0445 -0.16 0.8695
Test: S20-Star 0.0136 0.0448 0.30 0.7615
Test: SLJ-S20 -0.0116 0.0439 -0.26 0.7917
Test: BPT-SLJ -0.0128 0.0435 -0.29 0.7681
a1 0.2690 0.0472 5.70 <1e-07
Sex: Boys 0.2189 0.0141 15.49 <1e-53
Test: Star-Run & a1 0.1483 0.1461 1.01 0.3102
Test: S20-Star & a1 -0.0269 0.1512 -0.18 0.8586
Test: SLJ-S20 & a1 -0.0950 0.1485 -0.64 0.5225
Test: BPT-SLJ & a1 0.1903 0.1456 1.31 0.1911
Test: Star-Run & Sex: Boys -0.1214 0.0445 -2.73 0.0063
Test: S20-Star & Sex: Boys 0.0567 0.0448 1.27 0.2053
Test: SLJ-S20 & Sex: Boys 0.0394 0.0439 0.90 0.3689
Test: BPT-SLJ & Sex: Boys 0.0834 0.0435 1.92 0.0552
a1 & Sex: Boys 0.0693 0.0472 1.47 0.1418
Test: Star-Run & a1 & Sex: Boys -0.1716 0.1461 -1.17 0.2404
Test: S20-Star & a1 & Sex: Boys 0.1681 0.1512 1.11 0.2660
Test: SLJ-S20 & a1 & Sex: Boys -0.0967 0.1485 -0.65 0.5151
Test: BPT-SLJ & a1 & Sex: Boys 0.0090 0.1456 0.06 0.9506
Test: Run 0.9592
Test: Star_r 0.9874
Test: S20_r 0.9709
Test: SLJ 0.9677
Test: BPT 0.9398
Residual 0.0001
VarCorr(m_cpx_0_SeqDiff)
Column Variance Std.Dev Corr.
Child Test: Run 0.920050551 0.959192656
Test: Star_r 0.974948912 0.987395013 +0.51
Test: S20_r 0.942704181 0.970929545 +0.02 -0.20
Test: SLJ 0.936426387 0.967691266 +0.29 +0.55 +0.65
Test: BPT 0.883247904 0.939812696 +0.44 +0.71 +0.05 +0.39
Residual 0.000000015 0.000123095
m_cpx_0_SeqDiff.PCA
(Child = 
Principal components based on correlation matrix
 Test: Run      1.0     .      .      .      .
 Test: Star_r   0.51   1.0     .      .      .
 Test: S20_r    0.02  -0.2    1.0     .      .
 Test: SLJ      0.29   0.55   0.65   1.0     .
 Test: BPT      0.44   0.71   0.05   0.39   1.0

Normalized cumulative variances:
[0.5003, 0.7898, 0.9144, 0.9972, 1.0]

Component loadings
                 PC1    PC2    PC3    PC4    PC5
 Test: Run     -0.43   0.2   -0.88  -0.04   0.06
 Test: Star_r  -0.54   0.31   0.31  -0.4   -0.6
 Test: S20_r   -0.16  -0.78  -0.14   0.27  -0.51
 Test: SLJ     -0.48  -0.46   0.19  -0.43   0.58
 Test: BPT     -0.51   0.19   0.27   0.77   0.2,)
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.0188 0.0142 -1.32 0.1857 0.7466
Test: Star-Run -0.0148 0.0441 -0.34 0.7372 0.6418
Test: S20-Star 0.0194 0.0445 0.44 0.6634 1.0510
Test: SLJ-S20 -0.0193 0.0436 -0.44 0.6576 0.7025
Test: BPT-SLJ -0.0042 0.0432 -0.10 0.9230 0.7572
a1 0.2680 0.0474 5.66 <1e-07
Sex: Boys 0.2187 0.0142 15.41 <1e-52
Test: Star-Run & a1 0.1768 0.1447 1.22 0.2219
Test: S20-Star & a1 -0.0288 0.1504 -0.19 0.8481
Test: SLJ-S20 & a1 -0.0933 0.1476 -0.63 0.5273
Test: BPT-SLJ & a1 0.1826 0.1448 1.26 0.2073
Test: Star-Run & Sex: Boys -0.1280 0.0441 -2.90 0.0037
Test: S20-Star & Sex: Boys 0.0622 0.0445 1.40 0.1619
Test: SLJ-S20 & Sex: Boys 0.0364 0.0436 0.84 0.4035
Test: BPT-SLJ & Sex: Boys 0.0830 0.0432 1.92 0.0550
a1 & Sex: Boys 0.0706 0.0474 1.49 0.1362
Test: Star-Run & a1 & Sex: Boys -0.1405 0.1447 -0.97 0.3317
Test: S20-Star & a1 & Sex: Boys 0.1337 0.1504 0.89 0.3740
Test: SLJ-S20 & a1 & Sex: Boys -0.0809 0.1476 -0.55 0.5836
Test: BPT-SLJ & a1 & Sex: Boys 0.0088 0.1448 0.06 0.9515
Residual 0.0001
m_cpx_1_SeqDiff.PCA
(Child = 
Principal components based on correlation matrix
 (Intercept)      1.0     .      .     .      .
 Test: Star-Run   0.42   1.0     .     .      .
 Test: S20-Star  -0.21   0.03   1.0    .      .
 Test: SLJ-S20    0.22   0.26  -0.6   1.0     .
 Test: BPT-SLJ   -0.16  -0.02  -0.5   0.15   1.0

Normalized cumulative variances:
[0.3925, 0.6872, 0.8366, 0.9589, 1.0]

Component loadings
                   PC1    PC2    PC3    PC4    PC5
 (Intercept)     -0.33   0.57   0.14   0.68   0.3
 Test: Star-Run  -0.28   0.56  -0.63  -0.33  -0.32
 Test: S20-Star   0.6    0.3   -0.24  -0.2    0.67
 Test: SLJ-S20   -0.58   0.03   0.35  -0.57   0.46
 Test: BPT-SLJ   -0.34  -0.52  -0.64   0.23   0.38,)

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.0191 0.0142 -1.34 0.1790 0.7527
Test: c5.1 -0.0146 0.0436 -0.33 0.7386 1.2292
Test: c234.15 0.0461 0.1691 0.27 0.7850 0.4860
Test: c2.34 -0.0182 0.0767 -0.24 0.8127 1.2339
Test: c3.4 0.0011 0.0441 0.02 0.9808 1.2935
a1 0.2661 0.0474 5.61 <1e-07
Sex: Boys 0.2185 0.0142 15.39 <1e-52
Test: c5.1 & a1 0.2222 0.1440 1.54 0.1228
Test: c234.15 & a1 0.0043 0.5620 0.01 0.9938
Test: c2.34 & a1 0.1420 0.2576 0.55 0.5814
Test: c3.4 & a1 0.1032 0.1494 0.69 0.4896
Test: c5.1 & Sex: Boys 0.0552 0.0436 1.27 0.2059
Test: c234.15 & Sex: Boys -0.6048 0.1691 -3.58 0.0003
Test: c2.34 & Sex: Boys -0.1553 0.0767 -2.02 0.0430
Test: c3.4 & Sex: Boys -0.0443 0.0441 -1.00 0.3154
a1 & Sex: Boys 0.0708 0.0474 1.49 0.1351
Test: c5.1 & a1 & Sex: Boys -0.0790 0.1440 -0.55 0.5830
Test: c234.15 & a1 & Sex: Boys -0.2611 0.5620 -0.46 0.6422
Test: c2.34 & a1 & Sex: Boys -0.1988 0.2576 -0.77 0.4403
Test: c3.4 & a1 & Sex: Boys 0.1219 0.1494 0.82 0.4145
Residual 0.0001
VarCorr(m_cpx_1_PC)
Column Variance Std.Dev Corr.
Child (Intercept) 0.56651366 0.75267102
Test: c5.1 1.51095117 1.22920754 +0.02
Test: c234.15 0.23621476 0.48601930 +0.45 +0.57
Test: c2.34 1.52250094 1.23389665 +0.37 -0.09 -0.11
Test: c3.4 1.67304027 1.29346058 -0.08 -0.09 -0.06 -0.31
Residual 0.00000001 0.00011939
m_cpx_1_PC.PCA
(Child = 
Principal components based on correlation matrix
 (Intercept)     1.0     .      .      .      .
 Test: c5.1      0.02   1.0     .      .      .
 Test: c234.15   0.45   0.57   1.0     .      .
 Test: c2.34     0.37  -0.09  -0.11   1.0     .
 Test: c3.4     -0.08  -0.09  -0.06  -0.31   1.0

Normalized cumulative variances:
[0.3552, 0.6475, 0.8423, 0.9565, 1.0]

Component loadings
                  PC1    PC2    PC3    PC4    PC5
 (Intercept)    -0.5    0.34  -0.55   0.25   0.52
 Test: c5.1     -0.49  -0.4    0.36  -0.53   0.43
 Test: c234.15  -0.65  -0.28  -0.11   0.21  -0.66
 Test: c2.34    -0.18   0.69  -0.0   -0.62  -0.32
 Test: c3.4      0.23  -0.4   -0.74  -0.48  -0.08,)

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.0182 0.0143 -1.27 0.2034 0.7264
Test: c5.1 0.0149 0.0310 0.48 0.6317 0.6716
Test: c234.15 -0.0039 0.0316 -0.12 0.9026 0.7525
Test: c12.34 -0.0069 0.0313 -0.22 0.8247 0.9499
Test: c3.4 0.0111 0.0305 0.37 0.7145 0.3841
a1 0.2639 0.0477 5.53 <1e-07
Sex: Boys 0.2133 0.0143 14.93 <1e-49
Test: c5.1 & a1 -0.1454 0.1032 -1.41 0.1588
Test: c234.15 & a1 -0.0489 0.1043 -0.47 0.6391
Test: c12.34 & a1 0.0059 0.1051 0.06 0.9551
Test: c3.4 & a1 0.0808 0.1029 0.79 0.4319
Test: c5.1 & Sex: Boys -0.0638 0.0310 -2.06 0.0398
Test: c234.15 & Sex: Boys 0.1101 0.0316 3.48 0.0005
Test: c12.34 & Sex: Boys -0.0361 0.0313 -1.16 0.2478
Test: c3.4 & Sex: Boys -0.0175 0.0305 -0.57 0.5656
a1 & Sex: Boys 0.0692 0.0477 1.45 0.1474
Test: c5.1 & a1 & Sex: Boys 0.0631 0.1032 0.61 0.5410
Test: c234.15 & a1 & Sex: Boys 0.0885 0.1043 0.85 0.3962
Test: c12.34 & a1 & Sex: Boys -0.0679 0.1051 -0.65 0.5186
Test: c3.4 & a1 & Sex: Boys 0.0396 0.1029 0.39 0.7002
Residual 0.0000
VarCorr(m_cpx_1_PC_2)
Column Variance Std.Dev Corr.
Child (Intercept) 0.52767098 0.72640965
Test: c5.1 0.45104956 0.67160223 -0.15
Test: c234.15 0.56628817 0.75252121 -0.17 +0.21
Test: c12.34 0.90228176 0.94988513 +0.02 -0.13 +0.49
Test: c3.4 0.14753671 0.38410508 -0.61 -0.41 -0.48 -0.50
Residual 0.00000000 0.00000325
m_cpx_1_PC_2.PCA
(Child = 
Principal components based on correlation matrix
 (Intercept)     1.0     .      .      .     .
 Test: c5.1     -0.15   1.0     .      .     .
 Test: c234.15  -0.17   0.21   1.0     .     .
 Test: c12.34    0.02  -0.13   0.49   1.0    .
 Test: c3.4     -0.61  -0.41  -0.48  -0.5   1.0

Normalized cumulative variances:
[0.4243, 0.6909, 0.9188, 0.9998, 1.0]

Component loadings
                  PC1    PC2    PC3    PC4   PC5
 (Intercept)    -0.25   0.8   -0.07  -0.2   0.51
 Test: c5.1     -0.24  -0.3   -0.8    0.3   0.36
 Test: c234.15  -0.49  -0.45   0.14  -0.71  0.21
 Test: c12.34   -0.48  -0.14   0.55   0.61  0.29
 Test: c3.4      0.64  -0.24   0.19  -0.05  0.7,)
# 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.0184 0.0143 -1.29 0.1964 0.7418
Test: c5.1 0.0138 0.0313 0.44 0.6596 0.6423
Test: c234.15 -0.0059 0.0313 -0.19 0.8504 0.7125
Test: c12.34 -0.0062 0.0311 -0.20 0.8413 0.6803
Test: c3.4 0.0003 0.0313 0.01 0.9930 0.7335
a1 0.2615 0.0477 5.48 <1e-07
Sex: Boys 0.2135 0.0143 14.95 <1e-49
Test: c5.1 & a1 -0.1404 0.1042 -1.35 0.1779
Test: c234.15 & a1 -0.0360 0.1035 -0.35 0.7277
Test: c12.34 & a1 -0.0010 0.1046 -0.01 0.9921
Test: c3.4 & a1 0.0880 0.1055 0.83 0.4042
Test: c5.1 & Sex: Boys -0.0624 0.0313 -1.99 0.0463
Test: c234.15 & Sex: Boys 0.1128 0.0313 3.60 0.0003
Test: c12.34 & Sex: Boys -0.0357 0.0311 -1.15 0.2517
Test: c3.4 & Sex: Boys -0.0216 0.0313 -0.69 0.4894
a1 & Sex: Boys 0.0684 0.0477 1.43 0.1514
Test: c5.1 & a1 & Sex: Boys 0.0564 0.1042 0.54 0.5887
Test: c234.15 & a1 & Sex: Boys 0.0855 0.1035 0.83 0.4087
Test: c12.34 & a1 & Sex: Boys -0.0727 0.1046 -0.69 0.4873
Test: c3.4 & a1 & Sex: Boys 0.0664 0.1055 0.63 0.5292
Residual 0.0000
VarCorr(m_zcp_1_PC_2)
Column Variance Std.Dev Corr.
Child (Intercept) 0.55032486 0.74183883
Test: c5.1 0.41249146 0.64225498 .
Test: c234.15 0.50764132 0.71248953 . .
Test: c12.34 0.46284655 0.68032827 . . .
Test: c3.4 0.53798322 0.73347339 . . . .
Residual 0.00000000 0.00002321
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 -13100
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -12954 146 10 <1e-25

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}:
 13821.527421405826
 13821.527421405826
 13821.527421405826

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.55499402 0.74497921
Test: Star_r 0.63307744 0.79566164 +0.50
Test: S20_r 0.91425140 0.95616494 -0.31 +0.40
Test: SLJ 0.36814993 0.60675360 +0.31 -0.46 -0.54
Test: BPT 0.37194511 0.60987303 -0.00 -0.54 -0.54 +0.59
Residual 0.00000000 0.00000369
VarCorr(m_Child_HeC)
Column Variance Std.Dev Corr.
Child (Intercept) 0.06234925 0.24969832
Test: Star_r 0.22234231 0.47153188 -0.19
Test: S20_r 0.25559719 0.50556621 +0.12 -0.05
Test: SLJ 0.10514213 0.32425628 -0.38 +0.17 +0.68
Test: BPT 0.05665358 0.23802012 -0.14 -0.29 +0.13 -0.29
Residual 0.00000000 0.00000032
VarCorr(m_Child_HyC)
Column Variance Std.Dev Corr.
Child (Intercept) 0.22635614 0.47576900
Test: BPT-other 16.62936852 4.07791227 +0.05
Test: Star-End 0.57500656 0.75829187 +0.14 -0.69
Test: S20-Star 2.40524329 1.55088468 -0.00 +0.60 -0.08
Test: SLJ-S20 0.83962174 0.91630876 +0.05 -0.66 +0.00 -0.50
Residual 0.00000000 0.00000032
VarCorr(m_Child_PCA)
Column Variance Std.Dev Corr.
Child (Intercept) 0.47829129 0.69158607
Test: c5.1 1.02921043 1.01450009 -0.00
Test: c234.15 4.85477599 2.20335562 -0.28 -0.05
Test: c2.34 4.52641644 2.12753765 +0.07 +0.20 +0.23
Test: c3.4 1.69069088 1.30026569 -0.10 -0.58 -0.55 -0.60
Residual 0.00000000 0.00000027

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 -13157
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -12987 170 10 <1e-30
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 -13108
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -12807 301 10 <1e-57
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 -13137
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -12772 365 10 <1e-71
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 -13062
zScore ~ 1 + Test + a1 + Sex + Test & a1 + Test & Sex + a1 & Sex + Test & a1 & Sex + (1 + Test | Child) 36 -12759 303 10 <1e-58

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 PCA0 26 13062.0 13114.0 13283.5
2 HeC0 26 13108.3 13160.3 13329.7
3 HyC0 26 13136.9 13188.9 13358.3
4 SDC0 26 13156.7 13208.7 13378.1

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.031571 0.177681
Test: Star_r 0.114445 0.338297 -0.20
Test: S20_r 0.072370 0.269017 +0.23 -0.79
Test: SLJ 0.105410 0.324669 -0.22 +0.38 -0.82
Test: BPT 0.091810 0.303002 -0.05 -0.05 +0.50 -0.89
a1 0.011235 0.105995 +0.48 -0.09 +0.16 +0.15 -0.52
Residual 0.874445 0.935118
VarCorr(m_School_Helmert)
Column Variance Std.Dev Corr.
School (Intercept) 0.0315752 0.1776940
Test: Star_r 0.0286125 0.1691523 -0.20
Test: S20_r 0.0032625 0.0571182 +0.15 -0.25
Test: SLJ 0.0031817 0.0564067 -0.25 +0.42 -0.80
Test: BPT 0.0011214 0.0334875 -0.33 +0.33 +0.52 -0.62
a1 0.0112450 0.1060424 +0.48 -0.09 +0.16 +0.30 -0.64
Residual 0.8744416 0.9351158
VarCorr(m_School_Hypo)
Column Variance Std.Dev Corr.
School (Intercept) 0.031574 0.177689
Test: BPT-other 0.448362 0.669599 -0.33
Test: Star-End 0.114409 0.338243 -0.20 +0.33
Test: S20-Star 0.072308 0.268901 +0.23 +0.13 -0.79
Test: SLJ-S20 0.105380 0.324623 -0.22 -0.61 +0.38 -0.82
a1 0.011223 0.105940 +0.48 -0.64 -0.08 +0.16 +0.15
Residual 0.874450 0.935120
VarCorr(m_School_PCA)
Column Variance Std.Dev Corr.
School (Intercept) 0.031567 0.177670
Test: c5.1 0.078039 0.279354 -0.34
Test: c234.15 0.822957 0.907170 -0.03 +0.39
Test: c2.34 0.108701 0.329699 -0.15 +0.77 +0.60
Test: c3.4 0.105401 0.324655 +0.22 +0.13 -0.71 -0.35
a1 0.011150 0.105595 +0.48 -0.34 +0.41 -0.40 -0.15
Residual 0.874482 0.935138

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 13820.2 13874.2 14050.2
2 PCA0 27 13820.3 13874.3 14050.3
3 HyC0 27 13822.8 13876.8 14052.8
4 SDC0 27 13822.9 13876.9 14052.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 60d6e27 using Quarto 1.7.33.

Back to top