This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartial_within.qmd
124 lines (103 loc) · 2.39 KB
/
partial_within.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
title: "Partially-within subjects designs"
jupyter: julia-1.9
---
Begin by loading the packages to be used.
```{julia}
#| code-fold: true
#| output: false
using AlgebraOfGraphics
using CairoMakie
using DataFrames
using MixedModels
using MixedModelsMakie
using MixedModelsSim
using ProgressMeter
using Random
CairoMakie.activate!(; type="svg")
ProgressMeter.ijulia_behavior(:clear)
```
```{julia}
#| code-fold: true
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)
```
```{julia}
#| code-fold: true
unique!(select(design, :item, :frequency))
```
```{julia}
#| code-fold: true
m0 = let contrasts, form
contrasts = Dict(:frequency => HelmertCoding(base="high"))
form = @formula(dv ~ 1 + frequency +
(1 + frequency | subj))
fit(MixedModel, form, design; contrasts)
end
```
```{julia}
#| code-fold: true
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)
```
```{julia}
#| code-fold: true
θ = createθ(m0; subj=re_subj)
```
```{julia}
#| code-fold: true
σ = 1;
β = [1.0, -3, -2];
```
```{julia}
#| code-fold: true
fit!(simulate!(m0; θ, β, σ))
```
```{julia}
#| code-fold: true
shrinkageplot(m0)
```
```{julia}
#| code-fold: true
caterpillar(m0; orderby=nothing, vline_at_zero=true)
```
```{julia}
#| code-fold: true
design[!, :dv] .= response(m0)
```
```{julia}
#| code-fold: true
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)
```
```{julia}
#| code-fold: true
m1 = let contrasts, form
contrasts = Dict(:frequency => HelmertCoding(base="high"))
form = @formula(dv ~ 1 + frequency +
(1 + frequency | subj))
fit(MixedModel, form, design_partial; contrasts)
end
```
```{julia}
#| code-fold: true
shrinkageplot(m1)
```
```{julia}
#| code-fold: true
caterpillar(m1; orderby=nothing, vline_at_zero=true)
```