Codes (and explanations) for Linear Mixed-Effects Regression Analysis
Published by Haerim Hwang
linear mixed-effects regression anova statistics data science r
6 min READ
This post explains basics of linear mixed-effects regression vs. ANOVA.
How does a formula work?
contrast.model <- lmer(DependentMeasure ~ Factor1 * Factor2 + (1 + Factor1 * Factor2 | Participant) + (1 + Factor1 * Factor2 | item), data)
Take an example from my pilot study to understand how the results of mixed-effects analyses are interpreted. My pilot study investigated how Korean L2ers of English (n = 22) judged acceptability of English sentences on a 4-point scale with an additional “I don’t know” option. The critical sentences (k = 24) were distributed in a 2 × 2 Latin square design with the factors “construction” (VP-ellipsis; Gapping) and “clause” (Conjunct; Adjunct), as shown in (1)-(4).
(1) VP-ellipsis in a Conjunct Clause (VPE‑C): Sara made pizza, and Kelly did too.
(2) VP-ellipsis in an Adjunct Clause (VPE‑A): Sara made pizza because Kelly did.
(3) Gapping in a Conjunct Clause (Gapping‑C): Sara made pizza, and Kelly pasta.
(4) Gapping in an Adjunct Clause (Gapping‑A): * Sara made pizza because Kelly pasta.
In the following mixed-effects model (run with a contrast coding), the two independent variables “construction” and “clause” are added as fixed effects.
Haerim.model <- lmer(z-score ~ construction * clause + (1 + construction * clause | participant) + (1 + construction * clause | item), data)
By adding these random effects, now I have different intercept and slopes for “construction,” “clause,” and “construction:clause” (indicating interaction between “construction” and “clause”) per participant and item as below.
$participant
(Intercept) construction clause construction:clause
L2A_01 −0.13035717 −1.8822782 −0.21515671 −0.14715510
L2A_02 0.06596786 −1.7498164 −0.22280625 −0.22145612
L2A_03 0.28485458 −1.3562880 −0.63579403 −1.24079459
… …
$item
(Intercept) construction clause construction:clause
1 0.11852205 −1.292183 −0.6962850 −0.8422584
2 0.19970942 −1.387608 −0.5917419 −0.8100677
3 0.24531526 −1.374109 −0.5889505 −0.9270524
… …
When we run the model with a contrast coding, we get the results for random effects as below.
Random effects:
Groups Name Variance Std.Dev. Corr
participant (Intercept) 0.040101 0.20025
construction 0.150601 0.38807 0.80
clause 0.160550 0.40069 −0.61 −0.87
construction:clause 0.477556 0.69105 −0.85 −0.80
item (Intercept) 0.009452 0.09722
construction 0.007289 0.08537 −0.45
clause 0.007876 0.08875 0.70 −0.95
construction:clause 0.036609 0.19134 −0.60 −0.45
residual 0.168750 0.41079 0.16 0.76
Now, let’s take a look at the results of the model that we built (again, with a contrast coding).
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 0.20417 0.05044 27.42000 4.047 0.000381***
construction −1.32417 0.09212 22.78900 −14.375 6.47e-13***
clause −0.64325 0.09471 22.83400 −6.792 6.55e-07***
construction:clause −0.94653 0.16904 23.47700 −5.599 9.94e-06***