Last lecture: Multilevel: part I.
Today: Multilevel: part II.
I solved something for this lecture, remember those overflowing code bits, meet 'xaringan'
setwd("~/Dropbox/Teaching_MRes_Northumbria/Lecture11")library(mlmRev) # contains datalibrary(lme4)Exam<-mlmRev::Examfixed_pred<-lmer(normexam ~ standLRT + (1 | school), data=Exam, REML=F)
library(ggplot2)library(dplyr)#subset 3 schools (just picked 3 from the dataframe)subset<-filter(Exam, school=='1' | school=='17' | school=='18')multilevelplot<-ggplot(subset,aes(standLRT, normexam)) + geom_jitter(alpha = 0.3) + facet_wrap(~school) + xlab("London Reading test") + ylab("Normed Exam Score") + geom_smooth(method="lm") + geom_hline(yintercept=0, linetype="dashed") + theme_bw()
plot(multilevelplot)
Positive is that it shows the actual data. But it doesn't really show what happens in a multilevel designs.
Can we have a graph with demonstrates everything again?
Illustration based on this.
# pooled pooled.model <- lm(normexam ~ standLRT, data=Exam)# Save the fitted valuesExam$PooledPredictions <- fitted(pooled.model)# Interceptvarying.intercept.model <- lm(normexam ~ standLRT + school, data=Exam)Exam$VaryingInterceptPredictions <- fitted(varying.intercept.model)# Slopevarying.slope.model <- lm(normexam ~ standLRT:school, data=Exam)Exam$VaryingSlopePredictions <- fitted(varying.slope.model)# Interaction (both slope)interaction.model <- lm(normexam ~ standLRT + school + standLRT:school, data=Exam)Exam$InteractionPredictions <- fitted(interaction.model)
We need a subset.
require(ggplot2)require(dplyr)subset<-filter(Exam, school=='1' | school=='18' | school=='21' | school=='40' | school=='55' | school=='59')gg <- ggplot(subset, aes(x = standLRT, y = normexam, group = school)) + geom_line(aes(y = PooledPredictions), color = "darkgrey") + geom_line(aes(y = VaryingInterceptPredictions), color = "blue") + #geom_line(aes(y = VaryingSlopePredictions), color = "red") + #geom_line(aes(y = InteractionPredictions), color = "black") + geom_point(alpha = 0.3, size = 3) + facet_wrap(~school) + xlab("London Reading test") + ylab("Normed Exam Score") + theme_bw()
print(gg)
require(ggplot2)require(dplyr)subset<-filter(Exam, school=='1' | school=='18' | school=='21' | school=='40' | school=='55' | school=='59')gg <- ggplot(subset, aes(x = standLRT, y = normexam, group = school)) + geom_line(aes(y = PooledPredictions), color = "darkgrey") + # geom_line(aes(y = VaryingInterceptPredictions), color = "blue") + geom_line(aes(y = VaryingSlopePredictions), color = "red") + #geom_line(aes(y = InteractionPredictions), color = "black") + geom_point(alpha = 0.3, size = 3) + facet_wrap(~school) + xlab("London Reading test") + ylab("Normed Exam Score") + theme_bw()
print(gg)
require(ggplot2)require(dplyr)subset<-filter(Exam, school=='1' | school=='18' | school=='21' | school=='40' | school=='55' | school=='59')gg <- ggplot(subset, aes(x = standLRT, y = normexam, group = school)) + geom_line(aes(y = PooledPredictions), color = "darkgrey") + # geom_line(aes(y = VaryingInterceptPredictions), color = "blue") + # geom_line(aes(y = VaryingSlopePredictions), color = "red") + geom_line(aes(y = InteractionPredictions), color = "black") + geom_point(alpha = 0.3, size = 3) + facet_wrap(~school) + xlab("London Reading test") + ylab("Normed Exam Score") + theme_bw()
print(gg)
Use the 'Scottish schools' dataset and make those 3 graphs. (If you cannot load MLMrev, it should be available) from blackboard.
You might have not cared so far as you only collect experimental data and multilevel models might not apply. Actually, they can be used for some of the designs you encounter.
Let us look at those.
You might have not cared so far as you only collect experimental data and multilevel models might not apply. Actually, they can be used for some of the designs you encounter.
Let us look at those.
A null model looks like this
# lme4lmer(y ~ 1 + (1 | subjects), data=data)
A null growth model looks like this. ("Unconditional growth model")
# lme4lmer(y ~ time + (time | subjects), data=data)
Here we examine if treatment influences the outcome over time.
lmer(y ~ time * tx + (time | subjects), data=data)# dropping a random slope.lmer(y ~ time * tx + (1 | subjects), data=data)# dropping a random intercept.lmer(y ~ time * tx + ( 0 + time | subjects), data=data)
Now imagine that we have therapists... .
lmer(y ~ time * tx + (time | therapist/subjects), data=df)
In the previous example, a therapist could only offer either treatment or control. Randomization at therapist level
But often you'll have random allocation at the subject level.
lmer(y ~ time * tx + (time | therapist:subjects) + (time * tx || therapist), data=df)
We might hypothesize that therapists that are allocated participants that report worse symptoms at treatment start have better outcomes (more room for improvement). --> we solve this via modelling the variance-covariance matrix
lmer(y ~ time * tx + (time | therapist:subjects) + (time | therapist) + (0 + tx + time:tx | therapist), data=data)
It is also possible that when a therapist is successful with treatment A, that he/she will also be with B. We could model all such possible scenarios. This basically amounts to an unstructured variance-covariance matrix. (Luckily this is also the default for most packages.).
lmer(y ~ time * tx + (time | therapist:subjects) + (time * tx | therapist), data=df)
What if you don't have a normal distribution. For example, you have a forced choice task --> Binomial.
What if you don't have a normal distribution. For example, you have a forced choice task --> Binomial.
Extensions to non-linear models. Logit.
What if you don't have a normal distribution. For example, you have a forced choice task --> Binomial.
Extensions to non-linear models. Logit.
Example
# Examplem <- glmer(remission ~ IL6 + CRP + CancerStage + LengthofStay + Experience + (1 | DID), data = hdp, family = binomial)
Help! My data are not normal... . Pointers in Zuur et al. (2009).
Count data --> Poisson, Negative Binomial, -- 'Excess of zeroes'.
Ordinal --> probit / censored regression.
'Weird' functions. Gamma distribution.
Machine learning. ('caret' package, Random forests) and text mining: check here.
Social network analysis. (Citation network analysis, for example)
Machine learning. ('caret' package, Random forests) and text mining: check here.
Social network analysis. (Citation network analysis, for example)
Bayesian statistics. Check out McElreath, R. (2015). Statistical Rethinking. Texts in Statistical Science. CRC Press.
Machine learning. ('caret' package, Random forests) and text mining: check here.
Social network analysis. (Citation network analysis, for example)
Bayesian statistics. Check out McElreath, R. (2015). Statistical Rethinking. Texts in Statistical Science. CRC Press.
Meta-analysis. Check out the amazing 'metafor' package.
Machine learning. ('caret' package, Random forests) and text mining: check here.
Social network analysis. (Citation network analysis, for example)
Bayesian statistics. Check out McElreath, R. (2015). Statistical Rethinking. Texts in Statistical Science. CRC Press.
Meta-analysis. Check out the amazing 'metafor' package.
Statistical simulation. If you are interested, have a read of an example here.
Machine learning. ('caret' package, Random forests) and text mining: check here.
Social network analysis. (Citation network analysis, for example)
Bayesian statistics. Check out McElreath, R. (2015). Statistical Rethinking. Texts in Statistical Science. CRC Press.
Meta-analysis. Check out the amazing 'metafor' package.
Statistical simulation. If you are interested, have a read of an example here.
Using R for writing. 'Shiny': App. building.
SPSS-light.
(Thomas opens 'Rcmdr').
Distinguish between exploratory and confirmatory analyses. Make analysis plan ahead.
Visual checks. Correlation matrices/plots. Any issues identified?
Distinguish between exploratory and confirmatory analyses. Make analysis plan ahead.
Visual checks. Correlation matrices/plots. Any issues identified?
Find the fitting analysis. (Most likely one we have seen?). Check assumptions! (Is it multilevel?)
Distinguish between exploratory and confirmatory analyses. Make analysis plan ahead.
Visual checks. Correlation matrices/plots. Any issues identified?
Find the fitting analysis. (Most likely one we have seen?). Check assumptions! (Is it multilevel?)
Run the analysis. Bootstrap if you can. Check if different methods lead to same conclusion.
Any statistical tests you commonly employ that we have not covered?
What will (likely) change with regards to next year... .
Any feedback, points you want to raise?
Still all to play for... .
Feedback via Turnitin.
You can post questions via blackboard. Book an appointment with me (check availability).
Questions on assignment via discussion board. (Unavailable Dec. 21st to Jan. 9th)
No set exercise, other than that I want you to explore an R package and see what it does. Alternatively, work through a tutorial. (see some examples here). or here
No inspiration then look through R-bloggers or datacamp.
Look at the vignette, example code, and try it on some data. Write it up in a small notebook.
Also check the reading list! (many more than listed here).
Gelman, A., & Hill, J. (2006). Data analysis using regression and multilevel/hierarchical models. New York, NY: Cambridge University Press.
Hox, J. J. (2010). Multilevel analysis: Techniques and applications (2nd ed.). London: Taylor & Francis.
Magnusson, K. (2015). Using R and lme/lmer to fit different two- and three-level longitudinal models http://rpsychologist.com/r-guide-longitudinal-lme-lmer
Nieuwenhuis, R. (2017). R-Sessions 16: Multilevel Model Specification (lme4) http://www.rensenieuwenhuis.nl/r-sessions-16-multilevel-model-specification-lme4/
Snijders, T. A. B., & Berkhof, J. (2008). Diagnostic Checks for Multilevel Models. In: Handbook of Multilevel Analysis (pp. 141–175). New York, NY: Springer New York. http://doi.org/10.1007/978-0-387-73186-5_3
Snijders, T. A. B., & Bosker, R. J. (1999). Multilevel analysis: An introduction to basic and advanced multilevel modeling. London: Sage Publications Limited.
Zuur, A., Ieno, E. N., Walker, N., Saveliev, A. A., & Smith, G. M. (2009). Mixed effects models and extensions in ecology with R. New York, NY: Springer.
Last lecture: Multilevel: part I.
Today: Multilevel: part II.
I solved something for this lecture, remember those overflowing code bits, meet 'xaringan'
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |