Multilevel Modeling dengan paket “lme4” dan “multilevel”

#Load paket lme4 dan multilevel

library(lme4)

library(multilevel)

# Model dengan random intercept#

Null.Model<-lme(migrant2005~1,random=~1|villageid,data=dsmigration, control=list(opt=”optim”))

VarCorr(Null.Model)

tmod<-aov(migrant2005~as.factor(villageid),data=dsmigration)

# Menghitung “Intra Class Correlation” / persentase variasi yang bersumber dari perbedaan antar grup#

ICC1(tmod)

GREL.DAT<-GmeanRel(Null.Model)

names(GREL.DAT)

GREL.DAT$ICC

GREL.DAT$MeanRel

mean(GREL.DAT$MeanRel) #Average group-mean reliability

ICC2(tmod)

mod.ml<-lme(migrant2005~1,random=~1|villageid,data=dsmigration,method=”ML”,

control=list(opt=”optim”))

VarCorr(mod.ml)

Null.Model.2<-gls(migrant2005~1,data=dsmigration,

control=list(opt=”optim”))

anova(Null.Model,Null.Model.2)

# Model dengan Fixed Effect dan Random intercept#

Model.1<-lme(migrant2005~ses,random=~1|villageid,data=dsmigration,

control=list(opt=”optim”))

summary(Model.1)

VarCorr(Model.1)

library(lattice)

xyplot(migrant2005~SES|as.factor(villageid),data=dsmigration[1:1582,],

type=c(“p”,”g”,”r”),col=”dark blue”,col.line=”black”,

xlab=”Socio Economic Index”,

ylab=”migration”)

#Model dengan Random Effect(slope) dan Random Intercept#

Model.2<-lme(migrant2005~ses,random=~ses|villageid, data=dsmigration,

control=list(opt=”optim”))

summary(Model.2)

Model.2a<-update(Model.2,random=~1|villageid)

# Significance Test untuk Random Slope#

anova(Model.2,Model.2a)

Final.Model<-lme(migrant2005~HRS+LEAD+G.HRS+LEAD:G.HRS,

random=~LEAD|villageid,data=dsmigration,control=list(opt=”optim”))

round(summary(Final.Model)$tTable,dig=3)