This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
Reaction
: RT in msDays
: day 0 is normal sleep baseline (interval da -> Numeric)Subject
: numbered (categorical, non ordinal -> Factor)> <span class="co"># example data providedy by lme4</span>
> <span class="kw">library</span>(lme4)
> <span class="kw">data</span>(sleepstudy)
> <span class="co"># for more info, try ?sleepstudy</span>
> <span class="kw">library</span>(lattice) <span class="co"># provides some nice plotting functions</span>
> <span class="kw">str</span>(sleepstudy)
## 'data.frame': 180 obs. of 3 variables:
## $ Reaction: num 250 259 251 321 357 ...
## $ Days : num 0 1 2 3 4 5 6 7 8 9 ...
## $ Subject : Factor w/ 18 levels "308","309","310",..: 1 1 1 1 1 1 1 1 1 1 ...
> <span class="co"># simple scatter plot</span>
> sleep.xy <- <span class="kw">xyplot</span>(Reaction~Days,<span class="dt">data=</span>sleepstudy,
+ <span class="dt">xlab =</span> <span class="st">"Days of sleep deprivation"</span>,
+ <span class="dt">ylab =</span> <span class="st">"Average reaction time (ms)"</span>)
> sleep.xy
R has this built in:
> sleep.lm <- <span class="kw">lm</span>(Reaction ~ Days, <span class="dt">data =</span> sleepstudy)
additional predictors with +
(no interaction) or *
(interaction)
> <span class="co"># update() effectively performs the previous call for us PLUS other</span>
> <span class="co"># options we add now</span>
> sleep.xy <- <span class="kw">update</span>(sleep.xy, <span class="dt">type =</span> <span class="kw">c</span>(<span class="st">"p"</span>, <span class="st">"r"</span>)) <span class="co"># p for points, r for regression</span>
> sleep.xy
> <span class="kw">summary</span>(sleep.lm)
##
## Call:
## lm(formula = Reaction ~ Days, data = sleepstudy)
##
## Residuals:
## Min 1Q Median 3Q Max
## -110.85 -27.48 1.55 26.14 139.95
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 251.41 6.61 38.03 < 2e-16 ***
## Days 10.47 1.24 8.45 9.9e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 47.7 on 178 degrees of freedom
## Multiple R-squared: 0.286, Adjusted R-squared: 0.282
## F-statistic: 71.5 on 1 and 178 DF, p-value: 9.89e-15
> <span class="co"># convenient lattice function for residuals</span>
> <span class="kw">rfs</span>(sleep.lm)
> sleep.lm.vp1 <- <span class="kw">lm</span>(Reaction ~ Days,
+ <span class="dt">data=</span>sleepstudy[sleepstudy$Subject==<span class="st">"308"</span>,])
> <span class="kw">rfs</span>(sleep.lm.vp1)
> sleep.xy.bysubj <- <span class="kw">xyplot</span>(Reaction ~ Days|Subject,
+ <span class="dt">data=</span>sleepstudy,
+ <span class="dt">xlab =</span> <span class="st">"Days of sleep deprivation"</span>,
+ <span class="dt">ylab =</span> <span class="st">"Average reaction time (ms)"</span>)
> sleep.xy.bysubj
> sleep.xy.bysubj <- <span class="kw">update</span>(sleep.xy.bysubj, <span class="dt">type =</span> <span class="kw">c</span>(<span class="st">"p"</span>, <span class="st">"r"</span>))
> sleep.xy.bysubj
dep ~ indep | group
additional (indep|group)
terms for random effects
> sleep.lmer <- <span class="kw">lmer</span>(Reaction ~ Days + (<span class="dv">1</span>|Subject),
+ <span class="dt">data=</span>sleepstudy)
> ?formula
> <span class="kw">summary</span>(sleep.lmer)
## Linear mixed model fit by REML
## Formula: Reaction ~ Days + (1 | Subject)
## Data: sleepstudy
## AIC BIC logLik deviance REMLdev
## 1794 1807 -893 1794 1786
## Random effects:
## Groups Name Variance Std.Dev.
## Subject (Intercept) 1378 37.1
## Residual 960 31.0
## Number of obs: 180, groups: Subject, 18
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) 251.405 9.746 25.8
## Days 10.467 0.804 13.0
##
## Correlation of Fixed Effects:
## (Intr)
## Days -0.371
ez
ezMixed()
as a convenience for exploring fixed effectsezPredict()
useful for plotting regression lineseffects
lmerTest
languageR
LMERConvenienceFunctions
lmtest
(1 | random.factor)
(0 + fixed.factor | random.factor)
(1 + fixed.factor | random.factor)
(1 | random.factor) + (0 + fixed.factor | random.factor)
> <span class="co"># REML is a "shortcut" that invalidates anova()</span>
> sleep.lmer <- <span class="kw">update</span>(sleep.lmer,<span class="dt">REML=</span><span class="ot">FALSE</span>)
> <span class="co"># same as (Days|Subject)</span>
> sleep.lmer.slopes <- <span class="kw">lmer</span>(Reaction ~ Days + (<span class="dv">1</span>+Days|Subject),
+ <span class="dt">data=</span>sleepstudy,<span class="dt">REML=</span><span class="ot">FALSE</span>)
> sleep.lmer.slopes.int <- <span class="kw">lmer</span>(Reaction ~ Days + (<span class="dv">1</span>|Subject) +
+ (<span class="dv">0</span>+Days|Subject),
+ <span class="dt">data=</span>sleepstudy,<span class="dt">REML=</span><span class="ot">FALSE</span>)
> <span class="co"># can only be used for nested models!</span>
> <span class="kw">anova</span>(sleep.lmer, sleep.lmer.slopes, sleep.lmer.slopes.int)
## Data: sleepstudy
## Models:
## sleep.lmer: Reaction ~ Days + (1 | Subject)
## sleep.lmer.slopes.int: Reaction ~ Days + (1 | Subject) + (0 + Days | Subject)
## sleep.lmer.slopes: Reaction ~ Days + (1 + Days | Subject)
## Df AIC BIC logLik Chisq Chi Df Pr(>Chisq)
## sleep.lmer 4 1802 1815 -897
## sleep.lmer.slopes.int 5 1762 1778 -876 42.08 1 8.8e-11 ***
## sleep.lmer.slopes 6 1764 1783 -876 0.06 1 0.8
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ezANOVA()
depends on aov()
which depends on lm()
anova()
can be used to compare existing lm()
sezANOVA()
> <span class="co"># hide startup output</span>
> <span class="kw">suppressPackageStartupMessages</span>(<span class="kw">library</span>(ez))
> sleep.ez <- <span class="kw">ezANOVA</span>(sleepstudy,
+ <span class="dt">dv=</span>.(Reaction),
+ <span class="dt">wid=</span>.(Subject),
+ <span class="dt">within=</span>.(Days),
+ <span class="dt">detailed=</span><span class="ot">TRUE</span>,
+ <span class="co"># we'll take a look at the aov() in a sec</span>
+ <span class="dt">return_aov=</span><span class="ot">TRUE</span>)
## Warning: "Days" will be treated as numeric.
## Warning: There is at least one numeric within variable, therefore aov()
## will be used for computation and no assumption checks will be obtained.
> sleep.ez$ANOVA
## Effect DFn DFd SSn SSd F p p<.05 ges
## 1 Days 1 17 162703 60322 45.85 3.264e-06 * 0.7295
aov()
from ezANOVA()
> sleep.ez$aov
##
## Call:
## aov(formula = formula(aov_formula), data = data)
##
## Grand Mean: 298.5
##
## Stratum 1: Subject
##
## Terms:
## Residuals
## Sum of Squares 250618
## Deg. of Freedom 17
##
## Residual standard error: 121.4
##
## Stratum 2: Subject:Days
##
## Terms:
## Days Residuals
## Sum of Squares 162703 60322
## Deg. of Freedom 1 17
##
## Residual standard error: 59.57
## Estimated effects are balanced
##
## Stratum 3: Within
##
## Terms:
## Residuals
## Sum of Squares 94312
## Deg. of Freedom 144
##
## Residual standard error: 25.59
aov()
from ezANOVA()
> aov.formula <- <span class="kw">formula</span>(<span class="kw">attr</span>(sleep.ez$aov, <span class="st">"terms"</span>))
> <span class="kw">print</span>(aov.formula, <span class="dt">showEnv =</span> <span class="ot">FALSE</span>)
## Reaction ~ Days + Error(Subject/(Days))
glm()
glmer()
binary ~ continuous
continuous ~ continuous
and continuous ~ categegorial
continuous ~ exp(continuous)
(exponential response)count ~ continuous
behavioral ~ eeg
knitr
glmer()
outputnlme
(older, more specialized MEM implementation), see also herelme4
coauthor: (Bolker et al. 2009 )