A researcher wishes to assess the effects of principal leadership on mathematic perfomance in the sixth grade. Data for the study were collected from 10 students from 10 separate schools (a total of 100 students). The leadership variable for each school principal was a composite score from teacher’s ratings on six indicators of school leadership. The student variables were gender, an indicator of students coming from families of low socio-economic background, and the score on a mathematic performance test.
# 呼叫檔案
dta <- read.table("/Users/sudewei/Desktop/leadership_math.txt", header=T)# 資料結構
str(dta)'data.frame': 100 obs. of 6 variables:
$ Student: int 1 2 3 4 5 6 7 8 9 10 ...
$ School : int 100 100 100 100 100 100 100 100 100 100 ...
$ Lead : num 0.74 0.74 0.74 0.74 0.74 0.74 0.74 0.74 0.74 0.74 ...
$ Math : int 623 635 700 663 656 695 802 648 594 679 ...
$ Gender : chr "M" "F" "F" "M" ...
$ SES : chr "O" "O" "O" "O" ...
# 把vector改成factor
dta$student <- as.factor(dta$Student)
dta$school <- as.factor(dta$School)# dta報表
head(dta) Student School Lead Math Gender SES student school
1 1 100 0.74 623 M O 1 100
2 2 100 0.74 635 F O 2 100
3 3 100 0.74 700 F O 3 100
4 4 100 0.74 663 M O 4 100
5 5 100 0.74 656 F O 5 100
6 6 100 0.74 695 F O 6 100
#繪圖
ggplot(dta, aes(Lead, Math))+
stat_smooth(method='lm', formula=y~x, se=T)+
geom_point()+
facet_grid(Gender~SES)+
labs(x="Leardership score",
y="Math score")+
theme_minimal()# 下載package&model比較
library(lme4)
m0_full <- lme4::lmer(Math ~ SES + Gender + Gender:SES + (1 | School), data=dta, REML=FALSE)
m0_rd <- lme4::lmer(Math ~ 1 + (1 | School), data=dta, REML=FALSE)
anova(m0_full, m0_rd)Data: dta
Models:
m0_rd: Math ~ 1 + (1 | School)
m0_full: Math ~ SES + Gender + Gender:SES + (1 | School)
npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
m0_rd 3 1025 1033 -509.5 1019
m0_full 6 1025 1041 -506.5 1013 6.047 3 0.109
Test for gender by SES interaction effect only.
confint(m0_full,method='boot') 2.5 % 97.5 %
.sig01 0.00000 21.3492
.sigma 31.00682 41.7854
(Intercept) 633.47132 670.4164
SESO -1.29783 41.0032
GenderM -28.24312 27.0321
SESO:GenderM -28.96503 37.7816
Implement your final model here
#m0_rd <- lme4::lmer(Math ~ 1 + (1 | School), data=dta)Heck, R.H., & Thomas, S.L. (2000). An Introduction to Multilevel Modeling Techniques. (pp. 75-82), Mahwah, NJ:LEA.
# try
m3L3 <- lmer(Math ~ SES*Gender + (1 | School) ,data=dta)
dta$yhat <- fitted(m3L3)
ggplot(dta,
aes(Lead,
Math,
)) +
geom_point(alpha=.3)+
stat_smooth(method="lm",
formula=y~x, se=FALSE,
size=rel(.3), col='red')+
stat_smooth(aes(y=yhat),
method="lm",
formula=y~x, se=FALSE,
size=rel(.2),
alpha=.5)+
labs(x="Leadership score",
y="Math score")+
theme_minimal()