#Set seed so your answers are all the same
set.seed(9)
# Sample Per class room people
n1 <- 50; n2 <- 50; n3 <- 50; n4 <- 50
N<-n1+n2+n3+n4 # Total N
# Uniform distrobution of ActiveTime per classroom
X1 <- runif(n1, 0, 1); X2 <- runif(n2, 0, 1)
X3 <- runif(n3, 0, 1); X4 <- runif(n4, 0, 1)
# Uniform distrobution of support per classroom
Z1 <- runif(n1, 0, 1); Z2 <- runif(n2, 0, 1)
Z3 <- runif(n3, 0, 1); Z4 <- runif(n4, 0, 1)
# Intercepts per classroom
B0.1 <- 80; B0.2 <- 75
B0.3 <- 65; B0.4 <- 68
# Same slope for ActiveTime per classroom + Noise
B1 <- rnorm(n1, 10, sd=2.5); B2 <- rnorm(n2, 10, sd=2.5)
B3 <- rnorm(n3, 10, sd=2.5); B4 <- rnorm(n4, 10, sd=2.5)
# different slope for support per classroom + Noise
g1 <- rnorm(n1, 10, sd=2.5); g2 <- rnorm(n2, 5, sd=2.5)
g3 <- rnorm(n3, -5, sd=2.5); g4 <- rnorm(n4, 2, sd=2.5)
# Same interaction between ActiveTime*support support per classroom + Noise
f1<- rnorm(n3, 15, sd=2.5); f2<- rnorm(n3, 15, sd=2.5)
f3<- rnorm(n3, 15, sd=2.5); f4<- rnorm(n3, 15, sd=2.5)
# noise per student within each classroom
e1 <- rnorm(n1, 0, sd=2.5); e2 <- rnorm(n2, 0, sd=2.5)
e3 <- rnorm(n3, 0, sd=2.5); e4 <- rnorm(n4, 0, sd=2.5)
# Our equation to create Y for each classroom
Y1 = B1*scale(X1,scale=F)+g1*Z1+f1*scale(X1,scale=F)*scale(Z1,scale=F) + B0.1 + e1
Y2 = B2*scale(X2,scale=F)+g2*Z2+f2*scale(X2,scale=F)*scale(Z2,scale=F) + B0.2 + e2
Y3 = B3*scale(X3,scale=F)+g3*Z3+f3*scale(X3,scale=F)*scale(Z3,scale=F) + B0.3 + e3
Y4 = B4*scale(X4,scale=F)+g4*Z4+f4*scale(X4,scale=F)*scale(Z4,scale=F) + B0.4 + e4
# Merge classrooms into 1 data.frame
MLM.Data<-data.frame(Math=c(Y1,Y2,Y3,Y4),ActiveTime=c(X1,X2,X3,X4),Support=c(Z1,Z2,Z3,Z4),
Classroom=c(rep("C1",n1),rep("C2",n2),rep("C3",n3),rep("C4",n4)),
Told=c(rep("Works",n1),rep("Works",n2),
rep("Experimental",n3),rep("Experimental",n4)),
StudentID=as.factor(1:N))
library(ggplot2)
theme_set(theme_bw(base_size = 12, base_family = ""))
ClassRoom.Active <-ggplot(data = MLM.Data, aes(x = ActiveTime, y=Math,group=Classroom))+
facet_grid(Told~.)+
geom_point(aes(colour = Classroom,shape=Classroom))+
geom_smooth(method = "lm", se = TRUE, aes(colour = Classroom, linetype=Classroom))+
xlab("Active Learning Time")+ylab("Math Score")+
theme(legend.position = "top")
ClassRoom.Active
ClassRoom.Support <-ggplot(data = MLM.Data, aes(x = Support, y=Math,group=Classroom))+
facet_grid(Told~.)+
geom_point(aes(colour = Classroom, shape=Classroom))+
geom_smooth(method = "lm", se = TRUE, aes(colour = Classroom,linetype=Classroom))+
xlab("Support")+ylab("Math Score")+
theme(legend.position = "top")
ClassRoom.Support
Level 1 remains the same as last week
Level 2 Intercept
\[B_{0j} = \gamma_{01}W_j+\gamma_{00}+u_{0j} \]
Level 2 Slope
\[B_{1j} = \gamma_{11}W_j+\gamma_{10}+u_{1j} \]
library(lme4) #mixed model package by Douglas Bates et al
Model.Null<-lmer(Math ~1+(1|Classroom),
data=MLM.Data, REML=FALSE)
summary(Model.Null)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ 1 + (1 | Classroom)
## Data: MLM.Data
##
## AIC BIC logLik deviance df.resid
## 1222.3 1232.2 -608.2 1216.3 197
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.66017 -0.69735 0.00735 0.68158 2.99470
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 78.44 8.857
## Residual 23.13 4.809
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.551 4.441 4.000 16.56 7.79e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ICC.Model<-function(Model.Name) {
tau.Null<-as.numeric(lapply(summary(Model.Name)$varcor, diag))
sigma.Null <- as.numeric(attr(summary(Model.Name)$varcor, "sc")^2)
ICC.Null <- tau.Null/(tau.Null+sigma.Null)
return(ICC.Null)
}
MLM.Data$ActiveTime.GM<-scale(MLM.Data$ActiveTime,scale=F)
Model.1<-lmer(Math ~ActiveTime.GM+(1|Classroom),
data=MLM.Data, REML=FALSE)
summary(Model.1)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM + (1 | Classroom)
## Data: MLM.Data
##
## AIC BIC logLik deviance df.resid
## 1091.1 1104.3 -541.6 1083.1 196
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.5503 -0.6569 -0.0730 0.6226 3.6403
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 78.82 8.878
## Residual 11.72 3.423
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5514 4.4455 4.0000 16.55 7.82e-05 ***
## ActiveTime.GM 11.2642 0.8154 196.0050 13.81 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## ActiveTm.GM 0.000
anova(Model.Null,Model.1)
## Data: MLM.Data
## Models:
## Model.Null: Math ~ 1 + (1 | Classroom)
## Model.1: Math ~ ActiveTime.GM + (1 | Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## Model.Null 3 1222.3 1232.2 -608.17 1216.3
## Model.1 4 1091.1 1104.3 -541.55 1083.1 133.24 1 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(plyr)
# Cluster mean
MLM.Data<-ddply(MLM.Data,.(Classroom), mutate, ClassSupport = mean(Support))
MLM.Data$Support.CC<-MLM.Data$Support-MLM.Data$ClassSupport
Model.2<-lmer(Math ~ ActiveTime.GM+Support.CC+(1|Classroom),
data=MLM.Data, REML=FALSE)
summary(Model.2)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM + Support.CC + (1 | Classroom)
## Data: MLM.Data
##
## AIC BIC logLik deviance df.resid
## 1078.6 1095.1 -534.3 1068.6 195
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.9376 -0.6003 0.0133 0.5959 3.4624
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 78.84 8.879
## Residual 10.88 3.299
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5514 4.4456 4.0000 16.55 7.82e-05 ***
## ActiveTime.GM 11.4325 0.7870 196.0046 14.53 < 2e-16 ***
## Support.CC 3.1650 0.8157 196.0000 3.88 0.000143 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM
## ActiveTm.GM 0.000
## Support.CC 0.000 0.055
anova(Model.1,Model.2)
## Data: MLM.Data
## Models:
## Model.1: Math ~ ActiveTime.GM + (1 | Classroom)
## Model.2: Math ~ ActiveTime.GM + Support.CC + (1 | Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## Model.1 4 1091.1 1104.3 -541.55 1083.1
## Model.2 5 1078.6 1095.1 -534.30 1068.6 14.504 1 0.0001399 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Model.3<-lmer(Math ~ ActiveTime.GM+Support.CC+(1+Support.CC|Classroom),
data=MLM.Data, REML=FALSE)
summary(Model.3)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM + Support.CC + (1 + Support.CC | Classroom)
## Data: MLM.Data
##
## AIC BIC logLik deviance df.resid
## 1064.3 1087.4 -525.2 1050.3 193
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.0342 -0.7269 0.0562 0.5989 3.1819
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## Classroom (Intercept) 78.85 8.880
## Support.CC 13.27 3.643 1.00
## Residual 9.91 3.148
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5514 4.4454 4.0002 16.546 7.81e-05 ***
## ActiveTime.GM 10.9855 0.7514 196.0033 14.619 < 2e-16 ***
## Support.CC 2.9583 1.9811 3.9466 1.493 0.211
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM
## ActiveTm.GM 0.000
## Support.CC 0.918 0.021
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
anova(Model.2,Model.3)
## Data: MLM.Data
## Models:
## Model.2: Math ~ ActiveTime.GM + Support.CC + (1 | Classroom)
## Model.3: Math ~ ActiveTime.GM + Support.CC + (1 + Support.CC | Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## Model.2 5 1078.6 1095.1 -534.30 1068.6
## Model.3 7 1064.3 1087.4 -525.15 1050.3 18.301 2 0.0001062 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Model.3a<-lmer(Math ~ ActiveTime.GM+Support.CC+(1+Support.CC||Classroom),
data=MLM.Data, REML=FALSE)
summary(Model.3a)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM + Support.CC + (1 + Support.CC || Classroom)
## Data: MLM.Data
##
## AIC BIC logLik deviance df.resid
## 1071.8 1091.6 -529.9 1059.8 194
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.0272 -0.7003 0.0172 0.5958 3.3308
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 78.85 8.880
## Classroom.1 Support.CC 10.69 3.269
## Residual 10.06 3.171
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5514 4.4454 4.0001 16.545 7.81e-05 ***
## ActiveTime.GM 11.1567 0.7662 194.0181 14.560 < 2e-16 ***
## Support.CC 2.8910 1.8165 3.8582 1.591 0.189
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM
## ActiveTm.GM 0.000
## Support.CC 0.000 0.020
anova(Model.3,Model.3a)
## Data: MLM.Data
## Models:
## Model.3a: Math ~ ActiveTime.GM + Support.CC + (1 + Support.CC || Classroom)
## Model.3: Math ~ ActiveTime.GM + Support.CC + (1 + Support.CC | Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## Model.3a 6 1071.8 1091.6 -529.90 1059.8
## Model.3 7 1064.3 1087.4 -525.15 1050.3 9.491 1 0.002065 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Model.4<-lmer(Math ~ ActiveTime.GM*Support.CC+(1+Support.CC||Classroom),
data=MLM.Data, REML=FALSE)
summary(Model.4)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM * Support.CC + (1 + Support.CC || Classroom)
## Data: MLM.Data
##
## AIC BIC logLik deviance df.resid
## 1055.0 1078.1 -520.5 1041.0 193
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.9810 -0.6582 -0.0185 0.5562 3.3230
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 76.629 8.754
## Classroom.1 Support.CC 13.796 3.714
## Residual 9.087 3.014
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.6073 4.3821 3.9997 16.797 7.37e-05 ***
## ActiveTime.GM 11.0667 0.7291 193.5746 15.178 < 2e-16 ***
## Support.CC 3.0609 2.0048 3.9023 1.527 0.203
## ActiveTime.GM:Support.CC 11.9355 2.6768 192.8449 4.459 1.40e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM Spp.CC
## ActiveTm.GM 0.000
## Support.CC 0.000 0.017
## AcT.GM:S.CC 0.003 -0.022 0.021
anova(Model.3a,Model.4)
## Data: MLM.Data
## Models:
## Model.3a: Math ~ ActiveTime.GM + Support.CC + (1 + Support.CC || Classroom)
## Model.4: Math ~ ActiveTime.GM * Support.CC + (1 + Support.CC || Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## Model.3a 6 1071.8 1091.6 -529.9 1059.8
## Model.4 7 1055.0 1078.1 -520.5 1041.0 18.792 1 1.458e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(effects)
# Set levels of support
SLevels<-c(mean(MLM.Data$Support.CC)-sd(MLM.Data$Support.CC),
mean(MLM.Data$Support.CC),
mean(MLM.Data$Support.CC)+sd(MLM.Data$Support.CC))
#extract fixed effects
Results.Model.4<-Effect(c("ActiveTime.GM","Support.CC"),Model.4,
xlevels=list(ActiveTime.GM=seq(-.5,.5,.2),
Support.CC=SLevels))
#Convert to data frame for ggplot
Results.Model.4<-as.data.frame(Results.Model.4)
#Label Support for graphing
Results.Model.4$Support.F<-factor(Results.Model.4$Support.CC,
levels=SLevels,
labels=c("Low Support", "Mean Support", "High Support"))
#Plot fixed effect
Final.Fixed.Plot.1 <-ggplot(data = Results.Model.4,
aes(x = ActiveTime.GM, y =fit, group=Support.F))+
geom_line(size=2, aes(color=Support.F,linetype=Support.F))+
coord_cartesian(xlim = c(-.5, .5),ylim = c(50, 90))+
geom_ribbon(aes(ymin=fit-se, ymax=fit+se, group=Support.F, fill=Support.F),alpha=.2)+
xlab("Proportion of Time Engaged in Active Learning \nCentered")+
ylab("Math Score")+
theme(legend.position = "top",
legend.title=element_blank())
Final.Fixed.Plot.1
library(texreg)
texreg(list(Model.1,Model.2,Model.3a,Model.4),
table = FALSE, use.packages = FALSE,
single.row = FALSE, stars = c(0.001, 0.01, 0.05,0.1),digits=3)
htmlreg(list(Model.1,Model.2,Model.3a,Model.4),file = "ModelResults.doc",
single.row = FALSE, stars = c(0.001, 0.01, 0.05,0.1),digits=3,
inline.css = FALSE, doctype = TRUE, html.tag = TRUE,
head.tag = TRUE, body.tag = TRUE)
Model.5<-lmer(Math ~ ActiveTime.GM*Support.CC+(1+ActiveTime.GM+Support.CC||Classroom),
data=MLM.Data, REML=FALSE)
summary(Model.5)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula:
## Math ~ ActiveTime.GM * Support.CC + (1 + ActiveTime.GM + Support.CC ||
## Classroom)
## Data: MLM.Data
##
## AIC BIC logLik deviance df.resid
## 1056.7 1083.1 -520.3 1040.7 192
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.8928 -0.6345 0.0072 0.5731 3.3479
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 76.7011 8.7579
## Classroom.1 ActiveTime.GM 0.9878 0.9939
## Classroom.2 Support.CC 14.5540 3.8150
## Residual 8.9915 2.9986
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.6159 4.3841 3.9997 16.792 7.38e-05 ***
## ActiveTime.GM 11.0832 0.8797 3.8113 12.599 0.000302 ***
## Support.CC 3.1048 2.0516 3.8739 1.513 0.207008
## ActiveTime.GM:Support.CC 12.0019 2.6642 188.7441 4.505 1.16e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM Spp.CC
## ActiveTm.GM 0.000
## Support.CC 0.000 0.015
## AcT.GM:S.CC 0.003 -0.017 0.021
anova(Model.4,Model.5)
## Data: MLM.Data
## Models:
## Model.4: Math ~ ActiveTime.GM * Support.CC + (1 + Support.CC || Classroom)
## Model.5: Math ~ ActiveTime.GM * Support.CC + (1 + ActiveTime.GM + Support.CC || Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## Model.4 7 1055.0 1078.1 -520.50 1041.0
## Model.5 8 1056.7 1083.1 -520.35 1040.7 0.3082 1 0.5788
#Set seed so your answers are all the same
set.seed(9)
# Sample Per class room people
n1 <- 50; n2 <- 50; n3 <- 50; n4 <- 50
N<-n1+n2+n3+n4 # Total N
# Uniform distrobution of ActiveTime per classroom
X1 <- runif(n1, 0, 1); X2 <- runif(n2, 0, 1)
X3 <- runif(n3, 0, 1); X4 <- runif(n4, 0, 1)
# Uniform distrobution of support per classroom
Z1 <- runif(n1, 0, 1); Z2 <- runif(n2, 0, 1)
Z3 <- runif(n3, 0, 1); Z4 <- runif(n4, 0, 1)
# Intercepts per classroom
B0.1 <- 80; B0.2 <- 75
B0.3 <- 65; B0.4 <- 68
# Same slope for ActiveTime per classroom + Noise
B1 <- rnorm(n1, 17, sd=2.5); B2 <- rnorm(n2, 12, sd=2.5)
B3 <- rnorm(n3, 8, sd=2.5); B4 <- rnorm(n4, 4, sd=2.5)
# different slope for support per classroom + Noise
g1 <- rnorm(n1, 10, sd=2.5); g2 <- rnorm(n2, 5, sd=2.5)
g3 <- rnorm(n3, -5, sd=2.5); g4 <- rnorm(n4, 2, sd=2.5)
# Same interaction between ActiveTime*support support per classroom + Noise
f1<- rnorm(n3, 15, sd=2.5); f2<- rnorm(n3, 15, sd=2.5)
f3<- rnorm(n3, 15, sd=2.5); f4<- rnorm(n3, 15, sd=2.5)
# noise per student within each classroom
e1 <- rnorm(n1, 0, sd=2.5); e2 <- rnorm(n2, 0, sd=2.5)
e3 <- rnorm(n3, 0, sd=2.5); e4 <- rnorm(n4, 0, sd=2.5)
# Our equation to create Y for each classroom
Y1 = B1*scale(X1,scale=F)+g1*Z1+f1*scale(X1,scale=F)*scale(Z1,scale=F) + B0.1 + e1
Y2 = B2*scale(X2,scale=F)+g2*Z2+f2*scale(X2,scale=F)*scale(Z2,scale=F) + B0.2 + e2
Y3 = B3*scale(X3,scale=F)+g3*Z3+f3*scale(X3,scale=F)*scale(Z3,scale=F) + B0.3 + e3
Y4 = B4*scale(X4,scale=F)+g4*Z4+f4*scale(X4,scale=F)*scale(Z4,scale=F) + B0.4 + e4
# Merge classrooms into 1 data.frame
MLM.Data.2<-data.frame(Math=c(Y1,Y2,Y3,Y4),ActiveTime=c(X1,X2,X3,X4),Support=c(Z1,Z2,Z3,Z4),
Classroom=c(rep("C1",n1),rep("C2",n2),rep("C3",n3),rep("C4",n4)),
Told=c(rep("Works",n1),rep("Works",n2),
rep("Experimental",n3),rep("Experimental",n4)),
StudentID=as.factor(1:N))
library(ggplot2)
theme_set(theme_bw(base_size = 12, base_family = ""))
ClassRoom.Active.2 <-ggplot(data = MLM.Data.2, aes(x = ActiveTime, y=Math,group=Classroom))+
facet_grid(Told~.)+
geom_point(aes(colour = Classroom))+
geom_smooth(method = "lm", se = TRUE, aes(colour = Classroom))+
xlab("Active Learning Time")+ylab("Math Score")+
theme(legend.position = "none")
ClassRoom.Active.2
ClassRoom.Support.2 <-ggplot(data = MLM.Data.2, aes(x = Support, y=Math,group=Classroom))+
facet_grid(Told~.)+
geom_point(aes(colour = Classroom))+
geom_smooth(method = "lm", se = TRUE, aes(colour = Classroom))+
xlab("Support")+ylab("Math Score")+
theme(legend.position = "none")
ClassRoom.Support.2
MLM.Data.2$ActiveTime.GM<-scale(MLM.Data.2$ActiveTime,scale=F)
MLM.Data.2<-ddply(MLM.Data.2,.(Classroom), mutate, ClassSupport = mean(Support))
MLM.Data.2$Support.CC<-MLM.Data.2$Support-MLM.Data.2$ClassSupport
MLM.Model.1<-lmer(Math ~ ActiveTime.GM*Support.CC+(1|Classroom),
data=MLM.Data.2, REML=FALSE)
MLM.Model.2<-lmer(Math ~ ActiveTime.GM*Support.CC+(1+Support.CC|Classroom),
data=MLM.Data.2, REML=FALSE)
MLM.Model.3<-lmer(Math ~ ActiveTime.GM*Support.CC+(1+ActiveTime.GM+Support.CC|Classroom),
data=MLM.Data.2, REML=FALSE)
library(texreg)
texreg(list(MLM.Model.1,MLM.Model.2,MLM.Model.3),
table = FALSE, use.packages = FALSE,
single.row = FALSE, stars = c(0.001, 0.01, 0.05,0.1),digits=3)
anova(MLM.Model.1,MLM.Model.2,MLM.Model.3)
## Data: MLM.Data.2
## Models:
## MLM.Model.1: Math ~ ActiveTime.GM * Support.CC + (1 | Classroom)
## MLM.Model.2: Math ~ ActiveTime.GM * Support.CC + (1 + Support.CC | Classroom)
## MLM.Model.3: Math ~ ActiveTime.GM * Support.CC + (1 + ActiveTime.GM + Support.CC | Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## MLM.Model.1 6 1105.8 1125.6 -546.91 1093.8
## MLM.Model.2 8 1088.0 1114.4 -535.98 1072.0 21.848 2 1.802e-05 ***
## MLM.Model.3 11 1056.8 1093.0 -517.38 1034.8 37.205 3 4.165e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Set levels of support
SLevels.2<-c(mean(MLM.Data.2$Support.CC)-sd(MLM.Data.2$Support.CC),
mean(MLM.Data.2$Support.CC),
mean(MLM.Data.2$Support.CC)+sd(MLM.Data.2$Support.CC))
Results.MLM.Model.3<-Effect(c("ActiveTime.GM","Support.CC"),MLM.Model.3,
xlevels=list(ActiveTime.GM=seq(-.5,.5,.2),
Support.CC=SLevels.2))
Results.MLM.Model.3<-as.data.frame(Results.MLM.Model.3)
Results.MLM.Model.3$Support.F<-factor(Results.MLM.Model.3$Support.CC,
levels=SLevels.2,
labels=c("Low Support", "Mean Support", "High Support"))
Final.Fixed.Plot.2 <-ggplot(data = Results.MLM.Model.3,
aes(x = ActiveTime.GM, y =fit, group=Support.F))+
geom_line(size=2, aes(color=Support.F,linetype=Support.F))+
coord_cartesian(xlim = c(-.5, .5),ylim = c(50, 90))+
geom_ribbon(aes(ymin=fit-se, ymax=fit+se, group=Support.F, fill=Support.F),alpha=.2)+
xlab("Proportion of Time Engaged in Active Learning \nCentered")+
ylab("Math Score")+
theme(legend.position = "top",
legend.title=element_blank())
Final.Fixed.Plot.2
MLM.Data.2$Told.N<-as.numeric(as.factor(MLM.Data.2$Told))
MLM.Data.2$Told.C<-scale(MLM.Data.2$Told.N,scale=F)[,]
library(lme4)
L2test.1<-lmer(Math ~ ActiveTime.GM+Told.C
+(1|Classroom),
data=MLM.Data.2, REML=FALSE)
summary(L2test.1)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM + Told.C + (1 | Classroom)
## Data: MLM.Data.2
##
## AIC BIC logLik deviance df.resid
## 1127.7 1144.2 -558.9 1117.7 195
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.3473 -0.7294 -0.0060 0.6084 3.5710
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 12.91 3.593
## Residual 14.50 3.808
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5514 1.8164 3.9994 40.493 2.23e-06 ***
## ActiveTime.GM 11.8418 0.9071 196.0286 13.054 < 2e-16 ***
## Told.C 16.2312 3.6328 3.9996 4.468 0.0111 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM
## ActiveTm.GM 0.000
## Told.C 0.000 0.004
anova(MLM.Model.1,L2test.1)
## Data: MLM.Data.2
## Models:
## L2test.1: Math ~ ActiveTime.GM + Told.C + (1 | Classroom)
## MLM.Model.1: Math ~ ActiveTime.GM * Support.CC + (1 | Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## L2test.1 5 1127.7 1144.2 -558.86 1117.7
## MLM.Model.1 6 1105.8 1125.6 -546.91 1093.8 23.907 1 1.011e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
L2test.2<-lmer(Math ~ ActiveTime.GM*Told.C+(1|Classroom),
data=MLM.Data.2, REML=FALSE)
summary(L2test.2)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM * Told.C + (1 | Classroom)
## Data: MLM.Data.2
##
## AIC BIC logLik deviance df.resid
## 1091.4 1111.2 -539.7 1079.4 194
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.8458 -0.6170 -0.1151 0.6326 3.6811
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 12.88 3.589
## Residual 11.93 3.454
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5986 1.8108 3.9996 40.643 2.19e-06 ***
## ActiveTime.GM 11.6254 0.8233 196.0235 14.120 < 2e-16 ***
## Told.C 16.2274 3.6217 3.9996 4.481 0.011 *
## ActiveTime.GM:Told.C 10.7132 1.6467 196.0235 6.506 6.29e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM Told.C
## ActiveTm.GM 0.000
## Told.C 0.000 0.004
## ActT.GM:T.C 0.004 -0.040 0.000
anova(L2test.1,L2test.2)
## Data: MLM.Data.2
## Models:
## L2test.1: Math ~ ActiveTime.GM + Told.C + (1 | Classroom)
## L2test.2: Math ~ ActiveTime.GM * Told.C + (1 | Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## L2test.1 5 1127.7 1144.2 -558.86 1117.7
## L2test.2 6 1091.4 1111.2 -539.70 1079.4 38.328 1 5.98e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
L2test.3<-lmer(Math ~ ActiveTime.GM*Told.C+Support.CC+(1+Support.CC|Classroom),
data=MLM.Data.2, REML=FALSE)
summary(L2test.3)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM * Told.C + Support.CC + (1 + Support.CC |
## Classroom)
## Data: MLM.Data.2
##
## AIC BIC logLik deviance df.resid
## 1065.8 1095.5 -523.9 1047.8 191
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.9037 -0.6303 -0.0269 0.5935 3.2055
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## Classroom (Intercept) 49.802 7.057
## Support.CC 15.402 3.924 1.00
## Residual 9.875 3.142
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5960 3.5355 2.5652 20.816 0.000625 ***
## ActiveTime.GM 11.3120 0.7543 194.0727 14.997 < 2e-16 ***
## Told.C 4.0842 2.6563 5.7194 1.538 0.177460
## Support.CC 3.1934 2.1128 3.9054 1.511 0.206885
## ActiveTime.GM:Told.C 10.1190 1.5091 193.8334 6.705 2.14e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM Told.C Spp.CC
## ActiveTm.GM 0.000
## Told.C 0.000 0.094
## Support.CC 0.927 0.024 0.031
## ActT.GM:T.C 0.002 -0.048 -0.061 -0.036
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
L2test.3a<-lmer(Math ~ ActiveTime.GM*Told.C+Support.CC+(1+Support.CC||Classroom),
data=MLM.Data.2, REML=FALSE)
summary(L2test.3a)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM * Told.C + Support.CC + (1 + Support.CC ||
## Classroom)
## Data: MLM.Data.2
##
## AIC BIC logLik deviance df.resid
## 1067.1 1093.5 -525.5 1051.1 192
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.9306 -0.5811 -0.0084 0.5860 3.3844
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 12.936 3.597
## Classroom.1 Support.CC 13.558 3.682
## Residual 9.937 3.152
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5975 1.8121 3.9997 40.615 2.20e-06 ***
## ActiveTime.GM 11.5009 0.7625 193.6600 15.084 < 2e-16 ***
## Told.C 16.2252 3.6242 3.9997 4.477 0.011 *
## Support.CC 3.0440 2.0045 3.8964 1.519 0.205
## ActiveTime.GM:Told.C 10.4661 1.5261 193.2390 6.858 9.17e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM Told.C Spp.CC
## ActiveTm.GM 0.000
## Told.C 0.000 0.004
## Support.CC 0.000 0.019 0.000
## ActT.GM:T.C 0.004 -0.031 0.000 -0.043
anova(L2test.2,L2test.3a)
## Data: MLM.Data.2
## Models:
## L2test.2: Math ~ ActiveTime.GM * Told.C + (1 | Classroom)
## L2test.3a: Math ~ ActiveTime.GM * Told.C + Support.CC + (1 + Support.CC || Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## L2test.2 6 1091.4 1111.2 -539.70 1079.4
## L2test.3a 8 1067.1 1093.5 -525.55 1051.1 28.306 2 7.137e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
L2test.4<-lmer(Math ~ ActiveTime.GM*Told.C+Support.CC*Told.C+(1+Support.CC||Classroom),
data=MLM.Data.2, REML=FALSE)
summary(L2test.4)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula:
## Math ~ ActiveTime.GM * Told.C + Support.CC * Told.C + (1 + Support.CC ||
## Classroom)
## Data: MLM.Data.2
##
## AIC BIC logLik deviance df.resid
## 1060.9 1090.6 -521.5 1042.9 191
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.0715 -0.5576 0.0091 0.6209 3.3709
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 12.937 3.597
## Classroom.1 Support.CC 0.000 0.000
## Residual 9.904 3.147
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.5981 1.8121 3.9997 40.614 2.20e-06 ***
## ActiveTime.GM 11.4833 0.7549 196.0197 15.211 < 2e-16 ***
## Told.C 16.2249 3.6243 3.9997 4.477 0.011020 *
## Support.CC 3.0568 0.7846 195.9998 3.896 0.000134 ***
## ActiveTime.GM:Told.C 10.5940 1.5099 196.0197 7.016 3.62e-11 ***
## Told.C:Support.CC 7.2397 1.5692 195.9998 4.614 7.13e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM Told.C Spp.CC AT.GM:
## ActiveTm.GM 0.000
## Told.C 0.000 0.004
## Support.CC 0.000 0.067 0.000
## ActT.GM:T.C 0.004 -0.051 0.000 -0.095
## Tld.C:Sp.CC 0.000 -0.095 0.000 -0.091 0.067
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
L2test.4a<-lmer(Math ~ ActiveTime.GM*Told.C*Support.CC+(1|Classroom),
data=MLM.Data.2, REML=FALSE)
summary(L2test.4a)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: Math ~ ActiveTime.GM * Told.C * Support.CC + (1 | Classroom)
## Data: MLM.Data.2
##
## AIC BIC logLik deviance df.resid
## 1042.4 1075.4 -511.2 1022.4 190
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.7532 -0.5890 -0.0383 0.5852 3.3567
##
## Random effects:
## Groups Name Variance Std.Dev.
## Classroom (Intercept) 12.20 3.493
## Residual 8.93 2.988
## Number of obs: 200, groups: Classroom, 4
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 73.6591 1.7594 3.9983 41.867 1.95e-06
## ActiveTime.GM 11.3861 0.7177 196.0116 15.865 < 2e-16
## Told.C 16.0345 3.5187 3.9983 4.557 0.0104
## Support.CC 3.2730 0.7506 196.0002 4.361 2.09e-05
## ActiveTime.GM:Told.C 10.3461 1.4354 196.0116 7.208 1.20e-11
## ActiveTime.GM:Support.CC 12.3497 2.6575 196.1172 4.647 6.17e-06
## Told.C:Support.CC 7.9631 1.5012 196.0002 5.305 3.04e-07
## ActiveTime.GM:Told.C:Support.CC -1.1512 5.3149 196.1172 -0.217 0.8288
##
## (Intercept) ***
## ActiveTime.GM ***
## Told.C *
## Support.CC ***
## ActiveTime.GM:Told.C ***
## ActiveTime.GM:Support.CC ***
## Told.C:Support.CC ***
## ActiveTime.GM:Told.C:Support.CC
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) AcT.GM Told.C Spp.CC AcT.GM:T.C AT.GM:S T.C:S.
## ActiveTm.GM 0.000
## Told.C 0.000 0.004
## Support.CC -0.001 0.061 0.000
## ActT.GM:T.C 0.004 -0.049 0.000 -0.100
## AcT.GM:S.CC 0.008 -0.028 -0.012 0.059 -0.036
## Tld.C:Sp.CC 0.000 -0.100 -0.001 -0.077 0.061 0.102
## AT.GM:T.C:S -0.012 -0.036 0.008 0.102 -0.028 -0.076 0.059
anova(L2test.3a,L2test.4a)
## Data: MLM.Data.2
## Models:
## L2test.3a: Math ~ ActiveTime.GM * Told.C + Support.CC + (1 + Support.CC || Classroom)
## L2test.4a: Math ~ ActiveTime.GM * Told.C * Support.CC + (1 | Classroom)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## L2test.3a 8 1067.1 1093.5 -525.55 1051.1
## L2test.4a 10 1042.4 1075.4 -511.21 1022.4 28.676 2 5.931e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Set levels of support
SLevels.2<-c(mean(MLM.Data.2$Support.CC)-sd(MLM.Data.2$Support.CC),
mean(MLM.Data.2$Support.CC),
mean(MLM.Data.2$Support.CC)+sd(MLM.Data.2$Support.CC))
Results.L2test.4a<-Effect(c("ActiveTime.GM","Support.CC","Told.C"),L2test.4a,
xlevels=list(ActiveTime.GM=seq(-.5,.5,.2),
Support.CC=SLevels.2,
Told.C=c(min(MLM.Data.2$Told.C),max(MLM.Data.2$Told.C))))
Results.L2test.4a<-as.data.frame(Results.L2test.4a)
Results.L2test.4a$Support.F<-factor(Results.L2test.4a$Support.CC,
levels=SLevels.2,
labels=c("Low Support", "Mean Support", "High Support"))
Results.L2test.4a$Told.F<-factor(Results.L2test.4a$Told.C,
levels=c(min(MLM.Data.2$Told.C),max(MLM.Data.2$Told.C)),
labels=c("Experimental","Works"))
Final.Fixed.Plot.3 <-ggplot(data = Results.L2test.4a,
aes(x = ActiveTime.GM, y =fit, group=Support.F))+
facet_grid(.~Told.F)+
geom_line(size=2, aes(color=Support.F,linetype=Support.F))+
coord_cartesian(xlim = c(-.5, .5),ylim = c(50, 100))+
geom_ribbon(aes(ymin=fit-se, ymax=fit+se, group=Support.F, fill=Support.F),alpha=.2)+
xlab("Proportion of Time Engaged in Active Learning \nCentered")+
ylab("Math Score")+
theme(legend.position = "top",
legend.title=element_blank())
Final.Fixed.Plot.3