##TEMPLATE R CODE MULTILEVEL MODEL ##Example with dependent variable "relation_qua_repli" #load packages library(nlme) #for main multilevel analyses library(performance) #for icc library(sjPlot) #for export library(foreign) #for import datafile #set working directory setwd("put file path") #load data - all groups dataquest<-read.spss("put name spss file",use.value.labels=FALSE, to.data.frame=TRUE) #select dyads only (for triads, replace 2 by 3). Skip for Study 2 dataquest <- subset(dataquest,group_size=="2") #Grand-mean centering age and positive affect age_c <- dataquest$age - mean(dataquest$age) affect_pos_c <- dataquest$affect_pos_m - mean(dataquest$affect_pos_m) #1. Assess need for multilevel model (ICC) #1.a. Random intercept model (variation across groups, no IV) randomInterceptOnly <-lme(relation_qua_repli ~ 1, data = dataquest, random = ~1|group_number, method = "ML") summary(randomInterceptOnly) #1.b. Compute ICC (based on randamInterceptOnly model) icc(randomInterceptOnly) #2. Multilevel model: random intercept phone + covariates gender age positive affect (no random slope) randomInterceptcov <-lme(relation_qua_repli ~ condition +gender+age_c+affect_pos_c, data = dataquest, random = ~1|group_number, method = "ML") summary(randomInterceptcov) intervals(randomInterceptcov) #export results table <- tab_model(randomInterceptcov, show.se=TRUE, show.std=TRUE, show.stat=TRUE,show.re.var=TRUE,show.df=TRUE) table #see results in viewer panel on bottom right #3. Multilevel model: no covariates (random intercept phone) randomInterceptphone <-lme(relation_qua_repli ~ condition, data = dataquest, random = ~1|group_number, method = "ML") summary(randomInterceptphone)