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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example of an incomplete within design
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 MixedModlesMakie | ||
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=3) | ||
``` | ||
|
||
|
||
```{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=3) | ||
``` |