6 - 2022 - Advanced Bio2 - MixedModels - Dragon
6 - 2022 - Advanced Bio2 - MixedModels - Dragon
example
Dragons
Interested in how smart dragons are, and the relationship with body length.
R: scale()
Ok (?)
Hmmm
Hmmmm...
More EDA (should have done this first..)
Stratify?
Treat group as discrete covariate?
Body length not significant.
Good (?)
Interaction model?
Mixed effects model
- Account for correlation (grouping) with fewer parameters
- Avoid problems with multiple comparisons
- Uses all the data available (no reduction in power due to stratification)
Data structure
Mountain range (we’ve seen) and Site (sites are within mountain ranges)
Fixed effects
We are interested in the smartness ~ length association, so we treat body length
as a fixed effect. Fixed because we want to estimate that association and make
inference on it.
Random effects (always categorical) are grouping factors which we are trying to
adjust for. We want to adjust for mountain range (dragons on same mountain
might be exposed to same ‘stuff’ and hence more alike than dragons on another
mountain) and also might want to account for site.
Random effect of mountain range
R:
R code
mixed.lmer <- lmer(testScore ~ bodyLength2 + (1|mountainRange), data = dragons)
plot(mixed.lmer)
qqnorm(resid(mixed.lmer))
qqline(resid(mixed.lmer))
Stata code
xtmixed testScore bodyLength2 || mountainRange
predict Y, fitted
predict R, rstudent
twoway connected R Y
qnorm R
Variance components
Mountain:
= 0.60
Nested means what is sounds like (and what we have in this case) factors nested
without other factors. Eg. teachers nested within schools
“Crossed” random effects in this example would be the case where dragons were
not so territorial, and so they could also be measured on different mountain
ranges.
summary(mixed.WRONG)
- New variable
- Implicit vs explicit
nesting
- Same code (!?!)
Cross table of mountain x site
‘a’ , ‘b’, and ‘c’ are meaningless on own - they only uniquely
identify a site within a mountain range
327.56 /
(23.09+327.56+208.58)
= 0.586
23.09 / (23.09+327.56+208.58)
= 0.041
Body length not significant (?).
Sort out the random effects structure (do you need nested/crossed/other random
effects - ie if a random effect is accounting for very little variance - why keep it)?
Sort out the fixed effects (are all the necessary confounders and effects accounted
for and in correct form) [keep random effects constant]
Finished.
Yes, look at diagnostics (for the residuals), plot your predictions, plot your random
effects. Is your model useful?
Tutorial seized from: https://gkhajduk.github.io/2017-03-09-mixed-models/