Partially-within subjects designs

Author

Phillip Alday, Douglas Bates, and Reinhold Kliegl

Published

2024-09-09

Begin by loading the packages to be used.

Code
using AlgebraOfGraphics
using CairoMakie
using DataFrames
using MixedModels
using MixedModelsMakie
using MixedModelsSim
using ProgressMeter
using Random

const progress=false
Code
n_subj = 40
n_item = 3
# things are expressed as "between", so "within subjects" is "between items"
item_btwn = Dict(:frequency => ["high", "medium", "low"])
design = simdat_crossed(MersenneTwister(42), n_subj, n_item;
                        item_btwn = item_btwn)
design = DataFrame(design)
120×4 DataFrame
95 rows omitted
Row subj item frequency dv
String String String Float64
1 S01 I1 high -0.556027
2 S02 I1 high -0.444383
3 S03 I1 high 0.0271553
4 S04 I1 high -0.299484
5 S05 I1 high 1.77786
6 S06 I1 high -1.1449
7 S07 I1 high -0.468606
8 S08 I1 high 0.156143
9 S09 I1 high -2.64199
10 S10 I1 high 1.00331
11 S11 I1 high 1.08238
12 S12 I1 high 0.187028
13 S13 I1 high 0.518149
109 S29 I3 low -0.338763
110 S30 I3 low -0.0953675
111 S31 I3 low 0.768972
112 S32 I3 low 1.44244
113 S33 I3 low -0.275032
114 S34 I3 low -0.379637
115 S35 I3 low -0.722696
116 S36 I3 low 0.139661
117 S37 I3 low -1.40934
118 S38 I3 low 1.05546
119 S39 I3 low -2.23782
120 S40 I3 low 1.15915
Code
unique!(select(design, :item, :frequency))
3×2 DataFrame
Row item frequency
String String
1 I1 high
2 I2 medium
3 I3 low
Code
m0 = let contrasts, form
    contrasts = Dict(:frequency => HelmertCoding(base="high"))
    form = @formula(dv ~ 1 + frequency +
                    (1 + frequency | subj))
    fit(MixedModel, form, design; contrasts, progress)
end
Est. SE z p σ_subj
(Intercept) -0.0830 0.0983 -0.84 0.3983 0.5440
frequency: low 0.0411 0.1258 0.33 0.7438 0.7051
frequency: medium -0.0060 0.0572 -0.11 0.9160 0.2928
Residual 0.5204
Code
corrmat = [ 1    0.1 -0.2
            0.1  1    0.1
           -0.2  0.1  1 ]
re_subj = create_re(1.2, 1.5, 1.5; corrmat)
3×3 LinearAlgebra.LowerTriangular{Float64, Matrix{Float64}}:
  1.2    ⋅         ⋅ 
  0.15  1.49248    ⋅ 
 -0.3   0.180907  1.45852
Code
θ = createθ(m0; subj=re_subj)
6-element Vector{Float64}:
  1.2
  0.15000000000000002
 -0.30000000000000004
  1.49248115565993
  0.18090680674665818
  1.4585173044131932
Code
σ = 1;
β = [1.0, -3, -2];
Code
fit!(simulate!(m0; θ, β, σ))
Est. SE z p σ_subj
(Intercept) 1.1689 0.2127 5.49 <1e-07 1.1765
frequency: low -3.1311 0.2637 -11.87 <1e-31 1.4635
frequency: medium -2.2201 0.2021 -10.99 <1e-27 1.1917
Residual 1.1307
Code
shrinkageplot(m0)

Code
caterpillar(m0; orderby=nothing, vline_at_zero=true)

Code
design[!, :dv] .= response(m0)
120-element Vector{Float64}:
  3.2990696531367165
  3.689536701783809
  4.375609837896851
  4.098590979470805
  5.830385961499552
  1.6089690566381476
  7.105717398444753
  3.776647543243844
  7.891976461025253
  9.629613121950978
  ⋮
  1.071165785741714
  0.7834490252984565
  3.9574089728387003
 -1.3254321390454225
 -2.3949726184742737
  0.1434001276178467
 -3.1778072724683133
 -5.684570140359314
  1.8733892681099578
Code
design_partial = filter(design) do row
    subj = parse(Int, row.subj[2:end])
    item = parse(Int, row.item[2:end])
    # for even-numbered subjects, we keep all conditions
    # for odd-numbered subjects, we keep only the two "odd" items,
    # i.e. the first and last conditions
    return iseven(subj) || isodd(item)
end
sort!(unique!(select(design_partial, :subj, :frequency)), :subj)
100×2 DataFrame
75 rows omitted
Row subj frequency
String String
1 S01 high
2 S01 low
3 S02 high
4 S02 medium
5 S02 low
6 S03 high
7 S03 low
8 S04 high
9 S04 medium
10 S04 low
11 S05 high
12 S05 low
13 S06 high
89 S36 medium
90 S36 low
91 S37 high
92 S37 low
93 S38 high
94 S38 medium
95 S38 low
96 S39 high
97 S39 low
98 S40 high
99 S40 medium
100 S40 low
Code
m1 = let contrasts, form
    contrasts = Dict(:frequency => HelmertCoding(base="high"))
    form = @formula(dv ~ 1 + frequency +
                    (1 + frequency | subj))
    fit(MixedModel, form, design_partial; contrasts, progress)
end
Est. SE z p σ_subj
(Intercept) 1.0299 0.2384 4.32 <1e-04 1.2080
frequency: low -3.1311 0.2637 -11.87 <1e-31 1.5114
frequency: medium -2.3591 0.2097 -11.25 <1e-28 1.0538
Residual 0.9968
Code
shrinkageplot(m1)

Code
caterpillar(m1; orderby=nothing, vline_at_zero=true)

Back to top