Longitudinal Multilevel Modeling in R Studio (PART 3)
In this three part video series, I will show you how to analyze longitudinal data using multilevel modeling in R studio. In part three of this series, which is the last part of this series, I will show you how to build and interpret the conditional growth model. The conditional growth model, which is full model, is built upon the unconditional growth model, which establishes a measure of time across a slope (either being a fixed or a random slope). The conditional growth model includes all predictors (independent variables) of the dependent variable or outcome. If you have specific hypotheses or research questions about your longitudinal data, this would be the stage at which you could begin to include your predictors within the model. By running the conditional growth model, we can test our research questions and examine interactions between predictors on the dependent variable. We can also compare the conditional growth model (full model) to the unconditional growth model (with slope) for "model fit" and examine the predictive power of our independent variables.
NOTE: All three videos use the "nlme" package for multilevel modeling in R studio.
Below is all the R code I used in this video. Please note that angle brackets are not allowed in youtube video descriptions, so I left notes below where the angle brackets need to be inserted within the code.
# STEP 1: Upload dataset
Data1 (insert angled bracket here)- read.csv(file.choose())
# Make sure your file is a csv file and not an excel worksheet
# STEP 2: Open "nlme" package
library(nlme)
# STEP 3: Run conditional growth model (full model)
mod4 (insert angled bracket here)-lme(Job_Performance~Days_of_Training + Job_Site + Satisfaction_with_Trainer + Job_Site*Satisfaction_with_Trainer + Job_Satisfaction,random=~Days_of_Training|ID,data=Data1,method="ML")
summary(mod4)
intervals(mod4)
# STEP 4: Examine interaction plots
# Job Site and Job Performance
interaction.plot(Data1$Days_of_Training,Data1$Job_Site,Data1$Job_Performance)
# Job Site and Satisfaction with Trainer
interaction.plot(Data1$Days_of_Training,Data1$Job_Site,Data1$Satisfaction_with_Trainer)
# Or other plotting techniques to use
library(lattice)
xyplot(Job_Performance ~ Days_of_Training | ID, data=Data1, groups = Job_Site, type = c("p", "r"))
# Job Site and Job Performance
xyplot(Job_Performance ~ Days_of_Training | Job_Site, data=Data1,
prepanel = function(x, y) prepanel.loess(x, y, family ="gaussian"),
xlab = "Training Day", ylab = "Training Performance",
panel = function(x, y) {panel.xyplot(x, y)
panel.loess(x,y, family="gaussian") }, as.table=TRUE)
# Job Site and Satisfaction with Trainer
xyplot(Job_Performance ~ Satisfaction_with_Trainer | Job_Site, data=Data1,
prepanel = function(x, y) prepanel.loess(x, y, family ="gaussian"),
xlab = "Training Day", ylab = "Training Performance",
panel = function(x, y) {panel.xyplot(x, y)
panel.loess(x,y, family="gaussian") }, as.table=TRUE)
# STEP 5: Run deviance statistics.
# First, rerun the unconditional growth model (mod3) with random slope from before
mod3 (insert angled bracket here)-lme(Job_Performance~Days_of_Training,random=~Days_of_Training|ID,data=Data1,method="ML")
summary(mod3)
intervals(mod3)
# Compare unconditional growth model (mod3) with random slope to the full conditional growth model (mod4)
(results (insert angled bracket here)- anova(mod3,mod4))
results$`p-value`
# STEP 6: Calculate intra-class correlation coefficient (ICC) for full model
# ICC for unconditional growth model with random slope (mod3)
(1.2106302^2) / ((1.2106302^2) + (0.6154547^2))
# ICC for full conditional growth model (mod4)
(0.9410299^2) / ((0.9410299^2) + (0.7199369 ^2))
# STEP 7: Calculate the proportion reduction between the unconditional growth model with a random slope (mod3) and the full conditional growth model with all predictors (mod4)
# Level 1 is comparing Days of Training (repeated measures) to Job Performance
# This is at the repeated measures model
VarCorr(mod3)[1,1]
VarCorr(mod4)[1,1]
# This provides the unexplained variance captured in the full model
(as.numeric(VarCorr(mod3)[1,1])-as.numeric(VarCorr(mod4)[1,1]))/as.numeric(VarCorr(mod3)[1,1])
# Level 2 is comparing Days of Training plus all your predictors to Job Performance
# This is at the individual level
VarCorr(mod3)[2,1]
VarCorr(mod4)[2,1]
# This provides the unexplained variance captured in the full model
(as.numeric(VarCorr(mod3)[2,1])-as.numeric(VarCorr(mod4)[2,1]))/as.numeric(VarCorr(mod3)[2,1])