From 2f8a650cf6b006257e393505b2ad5cebc4df409b Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Tue, 12 Sep 2023 03:34:52 -0500 Subject: [PATCH 1/5] example of an incomplete within design --- partial_within.qmd | 124 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 partial_within.qmd diff --git a/partial_within.qmd b/partial_within.qmd new file mode 100644 index 0000000..9251650 --- /dev/null +++ b/partial_within.qmd @@ -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) +``` From 713b2edf426925d3b447da3f393e336cc6d580d4 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Wed, 26 Jun 2024 23:51:21 -0500 Subject: [PATCH 2/5] add to quarto --- _quarto.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/_quarto.yml b/_quarto.yml index 8a5901c..c172257 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -19,6 +19,7 @@ project: - kwdyz11.qmd - largescaledesigned.qmd - mrk17.qmd + - partial_within.qmd - pkg.qmd - profiling.qmd - selection.qmd @@ -89,6 +90,7 @@ website: - kkl15.qmd - largescaledesigned.qmd - mrk17.qmd + - partial_within.qmd - section: "Contrast coding" contents: - contrasts_fggk21.qmd From 99a63c331431932acd299b711890a2201a05ebe5 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Tue, 12 Sep 2023 03:44:06 -0500 Subject: [PATCH 3/5] more tweaks --- partial_within.qmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/partial_within.qmd b/partial_within.qmd index 9251650..47d03f5 100644 --- a/partial_within.qmd +++ b/partial_within.qmd @@ -80,7 +80,7 @@ shrinkageplot(m0) ```{julia} #| code-fold: true -caterpillar(m0; orderby=3) +caterpillar(m0; orderby=nothing, vline_at_zero=true) ``` @@ -120,5 +120,5 @@ shrinkageplot(m1) ```{julia} #| code-fold: true -caterpillar(m1; orderby=3) +caterpillar(m1; orderby=nothing, vline_at_zero=true) ``` From b8661160da95eda8b1a20d7e38e40add498d2982 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Wed, 26 Jun 2024 23:52:25 -0500 Subject: [PATCH 4/5] fix a thinko --- fits/elm01.json | 2 +- mrk17.qmd | 94 ++++++++++++++++++++++++------------------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/fits/elm01.json b/fits/elm01.json index 763c862..c7f11f6 100644 --- a/fits/elm01.json +++ b/fits/elm01.json @@ -1 +1 @@ -{"initial":[1.0,1.0],"finitial":2.6445865867863283e6,"ftol_rel":1.0e-12,"ftol_abs":1.0e-8,"xtol_rel":0.0,"xtol_abs":[1.0e-10,1.0e-10],"initial_step":[0.75,0.75],"maxfeval":-1,"maxtime":-1.0,"feval":53,"final":[0.31334125021488385,0.6744721169558918],"fmin":2.5474565247301087e6,"optimizer":"LN_BOBYQA","returnvalue":"FTOL_REACHED","nAGQ":1,"REML":false,"sigma":null,"fitlog":[[[1.0,1.0],2.6445865867863283e6]]} +{"initial":[1.0,1.0],"finitial":2.6445865867861053e6,"ftol_rel":1.0e-12,"ftol_abs":1.0e-8,"xtol_rel":0.0,"xtol_abs":[1.0e-10,1.0e-10],"initial_step":[0.75,0.75],"maxfeval":-1,"maxtime":-1.0,"feval":53,"final":[0.31334125099025767,0.674472125666452],"fmin":2.5474565247303774e6,"optimizer":"LN_BOBYQA","returnvalue":"FTOL_REACHED","nAGQ":1,"REML":false,"sigma":null,"fitlog":[[[1.0,1.0],2.6445865867861053e6]]} \ No newline at end of file diff --git a/mrk17.qmd b/mrk17.qmd index a67f372..26b3ad9 100644 --- a/mrk17.qmd +++ b/mrk17.qmd @@ -26,9 +26,9 @@ This section covers the general terminology and advice for model specification. ## Response, covariates, and factors -Linear mixed models (LMMs), like many other types of statistical models, describe a relationship between a *response* variable and *covariates* that have been measured or observed along with the response. The statistical model assumes that the residuals of the fitted response (i.e., not the responses) are normally -- also identically and independently -- distributed. This is the *first assumption* of normality in the LMM. It is standard practice that model residuals are inspected and, if serious skew is indicated, that the response is Box-Cox transformed (unless not justified for theoretical reasons) to fulfill this model assumption. +Linear mixed models (LMMs), like many other types of statistical models, describe a relationship between a *response* variable and *covariates* that have been measured or observed along with the response. The statistical model assumes that the residuals of the fitted response (i.e., not the responses) are normally -- also identically and independently -- distributed. This is the *first assumption* of normality in the LMM. It is standard practice that model residuals are inspected and, if serious skew is indicated, that the response is Box-Cox transformed (unless not justified for theoretical reasons) to fulfill this model assumption. -In the following we distinguish between *categorical covariates* and *numerical covariates*. Categorical covariates are *factors*. The important characteristic of a factor is that, for each observed value of the response, the factor takes on the value of one of a set of discrete levels. The levels can be unordered (nominal) or ordered (ordinal). We use the term *covariate* when we refer to *numerical covariates*, that is to continuous measures with some distribution. In principle, statistical models are not constrained by the distribution of observations across levels of factors and covariates, but the distribution may lead to problems of model identification and it does implications for statistical power. +In the following we distinguish between *categorical covariates* and *numerical covariates*. Categorical covariates are *factors*. The important characteristic of a factor is that, for each observed value of the response, the factor takes on the value of one of a set of discrete levels. The levels can be unordered (nominal) or ordered (ordinal). We use the term *covariate* when we refer to *numerical covariates*, that is to continuous measures with some distribution. In principle, statistical models are not constrained by the distribution of observations across levels of factors and covariates, but the distribution may lead to problems of model identification and it does implications for statistical power. Statistical power, especially for the detection of interactions, is best when observations are uniformly distributed across levels of factors or uniform across the values of covariates. In experimental designs, uniform distributions may be achieved by balanced assignment of subjects (or other carriers of responses) to the levels of factors or combinations of factor levels. In observational contexts, we achieve uniform distributions by stratification (e..g., on age, gender, or IQ scores). Statistical power is worse for skewed than normal distributions (I think ...). Therefore, although it is *not* required to meet an assumption of the statistical model, it may be useful to consider Box-Cox transformations of covariates. @@ -38,25 +38,25 @@ In LMMs the levels of at least one of the factors represents *units* in the data Random sampling is the basis of generalization from the sample to the population. The core statistics we will estimate in this context are variances and correlations of grand means and (quasi-)experimental effects. These terms will be explained below. What we want to stress here is that the estimation of (co-)variances / correlations requires a larger number of units (levels) than the estimation of means. Therefore, from a practical perspective, it is important that random factors are represented with many units. -When there is more than one random factor, we must be clear about their relation. The two prototypical cases are that the factors are *nested* or *crossed*. In multilevel models, a special case of mixed models, the levels of the random factors are strictly nested. For example, at a given time, every student attends a specific class in a specific school. Students, classes, and schools could be three random factors. As soon as we look at this scenario across several school years, the nesting quickly falls apart because students may move between classes and between schools. +When there is more than one random factor, we must be clear about their relation. The two prototypical cases are that the factors are *nested* or *crossed*. In multilevel models, a special case of mixed models, the levels of the random factors are strictly nested. For example, at a given time, every student attends a specific class in a specific school. Students, classes, and schools could be three random factors. As soon as we look at this scenario across several school years, the nesting quickly falls apart because students may move between classes and between schools. In psychology and linguistics, random factors are often crossed, for example, when every subject reads every word in every sentence in a word-by-word self-paced reading experiment (or alternatively: when every word in every sentence elicits a response from every subject). However, in an eye-movement experiment (for example), the perfect crossing on a measure like fixation duration is not attainable because of blinks or skipping of words. -In summary, the typical situation in experimental and observational studies with more than one random factor is _partial crossing_ or _partial nesting_ of levels of the random factors. Linear mixed models handle these situations very well. +In summary, the typical situation in experimental and observational studies with more than one random factor is _partial crossing_ or _partial nesting_ of levels of the random factors. Linear mixed models handle these situations very well. ## Experimental and quasi-experimental fixed factors / covariates -*Fixed experimental factor or covariate*. In experiments the units (or levels) of the random factor(s) are assigned to manipulations implemented in their design. The researcher controls the assignment of units of the random factor(s) (e.g., subjects, items) to experimental manipulations. These manipulations are represented as factors with a fixed and discrete set of levels (e.g., training vs. control group) or as covariates associated with continuous numeric values (e.g., presentation times). +*Fixed experimental factor or covariate*. In experiments the units (or levels) of the random factor(s) are assigned to manipulations implemented in their design. The researcher controls the assignment of units of the random factor(s) (e.g., subjects, items) to experimental manipulations. These manipulations are represented as factors with a fixed and discrete set of levels (e.g., training vs. control group) or as covariates associated with continuous numeric values (e.g., presentation times). -*Fixed quasi-experimental factor or covariate*. In observational studies (which can also be experiments) the units (or levels) of random factors may "bring along" characteristics that represent the levels of quasi-experimental factors or covariates beyond the control of the researcher. Whether a a subject is female, male, or diverse or whether a word is a noun, a verb, or an adjective are examples of quasi-experimental factors of gender or word type, respectively. Subject-related covariates are body height, body mass, and IQ scores; word-related covariates are their lengths, frequency, and cloze predictability. +*Fixed quasi-experimental factor or covariate*. In observational studies (which can also be experiments) the units (or levels) of random factors may "bring along" characteristics that represent the levels of quasi-experimental factors or covariates beyond the control of the researcher. Whether a a subject is female, male, or diverse or whether a word is a noun, a verb, or an adjective are examples of quasi-experimental factors of gender or word type, respectively. Subject-related covariates are body height, body mass, and IQ scores; word-related covariates are their lengths, frequency, and cloze predictability. ## Between-unit and within-unit factors / covariates -The distinction between between-unit and within-unit factors is always relative to a random (grouping) factor of an experimental design. A between-unit factor / covariate is a factor for which every unit of the random factor is assigned to or characterized by only one level of the factor. A within-unit factor is a factor for which units of the random factor appear at every level of the factor. +The distinction between between-unit and within-unit factors is always relative to a random (grouping) factor of an experimental design. A between-unit factor / covariate is a factor for which every unit of the random factor is assigned to or characterized by only one level of the factor. A within-unit factor is a factor for which units of the random factor appear at every level of the factor. -For the typical random factor, say *Subject*, there is little ambiguity because we are used to the between-within distinction from ANOVAs, more specifically the F1-ANOVA. In psycholinguistics, there is the tradition to test effects also for the second random factor *Item* in an F2-ANOVA. Importantly, for a given fixed factor all four combinations are possible. For example, *Gender* is a fixed quasi-experimental between-subject / within-item factor; word frequency is a fixed quasi-experimental within-subject / between-item covariate; *Prime-target relation* is a fixed experimental within-subject / within-item factor (assuming that targets are presented both in a primed and in an unprimed situation); and when a training manipulation is defined by the items used in the training, then in a training-control group design, the fixed factor *Group* is a fixed experimental between-subject / between-item factor. +For the typical random factor, say *Subject*, there is little ambiguity because we are used to the between-within distinction from ANOVAs, more specifically the F1-ANOVA. In psycholinguistics, there is the tradition to test effects also for the second random factor *Item* in an F2-ANOVA. Importantly, for a given fixed factor all four combinations are possible. For example, *Gender* is a fixed quasi-experimental between-subject / within-item factor; word frequency is a fixed quasi-experimental within-subject / between-item covariate; *Prime-target relation* is a fixed experimental within-subject / within-item factor (assuming that targets are presented both in a primed and in an unprimed situation); and when a training manipulation is defined by the items used in the training, then in a training-control group design, the fixed factor *Group* is a fixed experimental between-subject / between-item factor. -These distinctions are critical for setting up LMMs because variance components for (quasi-)experimental effects can only be specified for within-unit effects. Note also that loss of data (within limits), counterbalancing or blocking of items are irrelevant for these definitions. +These distinctions are critical for setting up LMMs because variance components for (quasi-)experimental effects can only be specified for within-unit effects. Note also that loss of data (within limits), counterbalancing or blocking of items are irrelevant for these definitions. ## Factor-based contrasts and covariate-based trends @@ -64,23 +64,23 @@ The simplest fixed factor has two levels and the model estimates the difference The simplest specification of a covariate is to include its linear trend, that is its slope. The slope (like a contrast) represents a difference score, that is the change in response to a one-unit change on the covariate. For covariates we must decide on the order of the trend we want to model. -## Contrast- and trend-based fixed-effect model parameters +## Contrast- and trend-based fixed-effect model parameters -Fixed factors and covariates are expected to have effects on the response. Fixed-effect model parameters estimate the hypothesized main and interaction effects of the study. The estimates of factors are based on contrasts; the estimates of covariates are based on trends. Conceptually, they correspond to unstandardized regression coefficients in multiple regression. +Fixed factors and covariates are expected to have effects on the response. Fixed-effect model parameters estimate the hypothesized main and interaction effects of the study. The estimates of factors are based on contrasts; the estimates of covariates are based on trends. Conceptually, they correspond to unstandardized regression coefficients in multiple regression. -The intercept is a special regression coefficient; it estimates the value of the dependent variable when all fixed effects associated with factors and trends associated with covariates are zero. In experimental designs with higher-order interactions there is an advantage of specifying the LMM in such a way that the intercept estimates the grand mean (GM; mean of the means of design cells). This happens if (a) contrasts for factors are chosen such that the intercept estimates the GM (positive: EffectsCoding, SeqDifferenceCoding, or HelmertCoding contrasts; negative: DummyCoding), (b) orthogonal polynomial trends are used (Helmert, anova-based), and (c) covariates are centered on their mean before inclusion in the model. As always, there may be good theoretical reasons to depart from the default recommendation. +The intercept is a special regression coefficient; it estimates the value of the dependent variable when all fixed effects associated with factors and trends associated with covariates are zero. In experimental designs with higher-order interactions there is an advantage of specifying the LMM in such a way that the intercept estimates the grand mean (GM; mean of the means of design cells). This happens if (a) contrasts for factors are chosen such that the intercept estimates the GM (positive: EffectsCoding, SeqDifferenceCoding, or HelmertCoding contrasts; negative: DummyCoding), (b) orthogonal polynomial trends are used (Helmert, anova-based), and (c) covariates are centered on their mean before inclusion in the model. As always, there may be good theoretical reasons to depart from the default recommendation. The specification of contrasts / trends does not depend on the status of the fixed factor / covariate. It does not matter whether a factor varies between or within the units of a random factor or whether it is an experimental or quasi-experimental factor. Contrasts are *not* specified for random (grouping) factors. ## Variance components (VCs) and correlation parameters (CPs) -Variance components (VCs) and correlation parameters (CPs) are within-group model parameters; they correspond to (some of the) *within-unit* (quasi-)experimental fixed-effect model parameters. Thus, we may be able to estimate a subject-related VC for word frequency. If we included a linear trend for word frequency, the VC estimates the subject-related variance in these slopes. We cannot estimate an item-related VC for the word-frequency slopes because there is only one frequency associated with words. Analogously, we may able to estimate an item-related VC for the effect of `Group (training vs. control)`, but we cannot estimate a subject-related VC for this effect. +Variance components (VCs) and correlation parameters (CPs) are within-group model parameters; they correspond to (some of the) *within-unit* (quasi-)experimental fixed-effect model parameters. Thus, we may be able to estimate a subject-related VC for word frequency. If we included a linear trend for word frequency, the VC estimates the subject-related variance in these slopes. We cannot estimate an item-related VC for the word-frequency slopes because there is only one frequency associated with words. Analogously, we may able to estimate an item-related VC for the effect of `Group (training vs. control)`, but we cannot estimate a subject-related VC for this effect. -The within-between characteristics of fixed factors and covariates relative to the random factor(s) are features of the design of the experiment or observational study. They fundamentally constrain the specification of the LMM. That's why it is of upmost importance to be absolutely clear about their status. +The within-between characteristics of fixed factors and covariates relative to the random factor(s) are features of the design of the experiment or observational study. They fundamentally constrain the specification of the LMM. That's why it is of upmost importance to be absolutely clear about their status. ## Conditional modes of random effects -In this outline of the dimensions underlying the specification of an LMM, we have said nothing so far about the conditional modes of random effects (i.e., the results shown in caterpillar and shrinkage plots). They are not needed for model specification or model selection. +In this outline of the dimensions underlying the specification of an LMM, we have said nothing so far about the conditional modes of random effects (i.e., the results shown in caterpillar and shrinkage plots). They are not needed for model specification or model selection. The VC is the prior variance of the random effects, whereas `var(ranef(model))` is the variance of the posterior means/modes of the random effects. See Kliegl et al. (2010, VisualCognition); [Rizopoulos (2019, stackexchange](https://stats.stackexchange.com/questions/392283/interpreting-blups-or-varcorr-estimates-in-mixed-models/392307#392307). @@ -96,7 +96,7 @@ The data (variables and observations) used by Masson et al. (2017) are available |Variable | Description| |---------|----------- | |Subj | Subject identifier | -|Item | Target (non-)word | +|Item | Target (non-)word | |trial | Trial number | |F | Target frequency is _high_ or _low_ | |P | Prime is _related_ or _unrelated_ to target | @@ -105,7 +105,7 @@ The data (variables and observations) used by Masson et al. (2017) are available |lT | Last-trail target requires _word_ or _nonword_ response | |rt | Reaction time [ms] | -`lagQlty` and `lagTrgt` refer to experimental conditions in the last trial. +`lagQlty` and `lagTrgt` refer to experimental conditions in the last trial. Corresponding indicator variables (-1/+1): @@ -147,7 +147,7 @@ The following LMM is *not* the maximal factorial LMM because we do not include i ## Model fit ```{julia} -contrasts = +contrasts = Dict( :F => EffectsCoding(; levels=["LF", "HF"]) , :P => EffectsCoding(; levels=["unr", "rel"]), :Q => EffectsCoding(; levels=["deg", "clr"]), @@ -155,7 +155,7 @@ contrasts = :lT =>EffectsCoding(; levels=["NW", "WD"]) ); -m_cpx = let +m_cpx = let form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + (1+F+P+Q+lQ+lT | subj) + (1 +P+Q+lQ+lT | item); @@ -199,7 +199,7 @@ VP is zero for fourth diagonal entry; not supported by data. We take out correlation parameters. ```{julia} -m_zcp = let +m_zcp = let form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + zerocorr(1+F+P+Q+lQ+lT | subj) + zerocorr(1 +P+Q+lQ+lT | item); @@ -218,7 +218,7 @@ MixedModels.likelihoodratiotest(m_zcp, m_cpx) Looks ok. It might be a good idea to prune the LMM by removing small VCs. ```{julia} -m_zcp2 = let +m_zcp2 = let form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + zerocorr(1 +P+Q+lQ+lT | subj) + zerocorr(1 +P+Q +lT | item); @@ -234,7 +234,7 @@ MixedModels.likelihoodratiotest(m_zcp2, m_zcp, m_cpx) We can perhaps remove some more. ```{julia} -m_zcp3 = let +m_zcp3 = let form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + zerocorr(1 +Q +lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) @@ -249,7 +249,7 @@ MixedModels.likelihoodratiotest(m_zcp3, m_zcp2, m_zcp, m_cpx) And another iteration. ```{julia} -m_zcp4 = let +m_zcp4 = let form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + zerocorr(1 +lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) @@ -264,8 +264,8 @@ MixedModels.likelihoodratiotest(m_zcp4, m_zcp3, m_zcp2, m_zcp, m_cpx) Too much removed. Stay with `m_zcp3`, but extend with CPs. ```{julia} -m_prm = let - form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + +m_prm = let + form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + (1+ Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) end @@ -273,8 +273,8 @@ VarCorr(m_prm) ``` ### post-hoc LMM ```{julia} -m_prm = let - form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + +m_prm = let + form = @formula (1000/rt) ~ 1+F*P*Q*lQ*lT + (1+ Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) end @@ -287,7 +287,7 @@ VarCorr(m_prm) MixedModels.likelihoodratiotest(m_zcp3, m_prm, m_cpx) ``` -The LRT favors the complex LMM, but not that χ² < 2*(χ²-dof); AIC and BIC suggest against selection. +The LRT favors the complex LMM, but not that χ² < 2*(χ²-dof); AIC and BIC suggest against selection. ```{julia} gof_summary = let @@ -295,7 +295,7 @@ gof_summary = let mods = eval.(nms) lrt = MixedModels.likelihoodratiotest(m_zcp3, m_prm, m_cpx) DataFrame(; - name = nms, + name = nms, dof=dof.(mods), deviance=round.(deviance.(mods), digits=0), AIC=round.(aic.(mods),digits=0), @@ -308,14 +308,14 @@ gof_summary = let end ``` -# Parsimonious LMM - replication of MRK17 LMM +# Parsimonious LMM - replication of MRK17 LMM -The LMM is not nested in the previous sequence. +The LMM is not nested in the previous sequence. ## Crossed fixed effects ```{julia} -m_mrk17_crossed =let +m_mrk17_crossed =let form = @formula (1000/rt) ~ 1 + F*P*Q*lQ*lT + (1+Q | subj) + zerocorr(0+lT | subj) + zerocorr(1 + P | item) ; fit(MixedModel, form, dat; contrasts) @@ -333,7 +333,7 @@ Finally, a look at the fixed effects. The four-factor interaction reported in Ma ## Nested fixed effects ```{julia} -m_mrk17_nested =let +m_mrk17_nested =let form = @formula (1000/rt) ~ 1 + Q/(F/P) + (1+Q | subj) + zerocorr(0+lT | subj) + zerocorr(1 + P | item) ; fit(MixedModel, form, dat; contrasts) @@ -347,7 +347,7 @@ end Include parenthesis ```{julia} -m_mrk17_nested =let +m_mrk17_nested =let form = @formula (1000/rt) ~ 1 + Q/(F/P) + (1+Q | subj) + zerocorr(0+lT | subj) + zerocorr(1 + P | item) ; fit(MixedModel, form, dat; contrasts) @@ -359,27 +359,27 @@ end ```{julia} using RegressionFormulae # m_prm_5 is equivalent to m_prm -m_prm_5 = let +m_prm_5 = let form = @formula (1000/rt) ~ 1+(F+P+Q+lQ+lT)^5 + (1+Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) end; -m_prm_4 = let +m_prm_4 = let form = @formula (1000/rt) ~ 1+(F+P+Q+lQ+lT)^4 + (1+Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) end; -m_prm_3 = let +m_prm_3 = let form = @formula (1000/rt) ~ 1+(F+P+Q+lQ+lT)^3 + (1+Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) end; -m_prm_2 = let +m_prm_2 = let form = @formula (1000/rt) ~ 1+(F+P+Q+lQ+lT)^2 + (1+Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) end; -m_prm_1 = let +m_prm_1 = let form = @formula (1000/rt) ~ 1+ F+P+Q+lQ+lT + (1+Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) end; @@ -390,7 +390,7 @@ gof_summary = let mods = eval.(nms) lrt = MixedModels.likelihoodratiotest(m_prm_1, m_prm_2, m_prm_3, m_prm_4, m_prm_5) DataFrame(; - name = nms, + name = nms, dof=dof.(mods), deviance=deviance.(mods), AIC=aic.(mods), @@ -403,24 +403,24 @@ gof_summary = let end ``` -Depending on the level of precision of your hypothesis. You could stay with main effect (BIC), include 2-factor interactions (AIC; also called _simple_ interactions) or include 3-factor interactions [χ² < 2*(χ²-dof); also called _2-way_ interactions]. +Depending on the level of precision of your hypothesis. You could stay with main effect (BIC), include 2-factor interactions (AIC; also called _simple_ interactions) or include 3-factor interactions [χ² < 2*(χ²-dof); also called _2-way_ interactions]. ## Posthoc LMM -We are using only three factors for the illustruation. +We are using only three factors for the illustruation. ```{julia} -m_prm3 = let +m_prm3 = let form = @formula (1000/rt) ~ 1 + lT*lQ*Q + (1+ Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) end ``` -The `lT & lQ & Q` interactions is significant. Let's follow it up with a post-hoc LMM, that is looking at the `lQ & Q` interaction in the two levels of whether the last word was a target or not. +The `lT & lQ & Q` interactions is significant. Let's follow it up with a post-hoc LMM, that is looking at the `lQ & Q` interaction in the two levels of whether the last word was a target or not. ```{julia} -m_prm3_posthoc1 = let +m_prm3_posthoc = let form = @formula (1000/rt) ~ 1 + lT/(lQ*Q) + (1+ Q+lT | subj) + (1 | item); fit(MixedModel, form, dat; contrasts) @@ -433,8 +433,8 @@ The original and post-hoc LMM have the same goodness of fit. ```{julia} [objective(m_prm3), objective(m_prm3_posthoc)] - ``` - +``` + ## Info ```{julia} From ffc2578b1867950f4c6f7c4140ea5f7d3fcd1395 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Wed, 26 Jun 2024 23:53:50 -0500 Subject: [PATCH 5/5] typo --- partial_within.qmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/partial_within.qmd b/partial_within.qmd index 47d03f5..9c7c8f7 100644 --- a/partial_within.qmd +++ b/partial_within.qmd @@ -12,7 +12,7 @@ using AlgebraOfGraphics using CairoMakie using DataFrames using MixedModels -using MixedModlesMakie +using MixedModelsMakie using MixedModelsSim using ProgressMeter using Random