1.1 数据准备
1.1.1 变量
层次:学生-学校
样本数:10320-160
ID:school
因变量:geread(学生的阅读水平)
自变量:gevocab(学生的词汇量),senroll(学校的招生规模)
1.1.2 数据文件处理
本文使用的数据为《Multilevel Modeling Using R》书中的数据Achieve.csv
(1)R语言
在R语言中需要将所有样本数据放到一个文件中(最好是csv文件)
(2)HLM软件
HLM6.08软件则需要对不同层次样本数据建立SPSS文件(sav文件),两层数据有一个关联ID(比如school)
1.2 模型构建
1.2.1 零模型
R语言中利用nlme包的lme函数来计算多层线性模型,然后通过summary函数来察看计算结果。结果中判断模型中加入的变量是否有效的关键参数是AIC指数,该指数下降,表明添加的变量对因变量有解释作用。随机效应可考察变量标准差,固定效应可考察变量回归方程相关参数及统计检验值。
HLM6.08中模型构建则全部是可视化操作,通过File菜单下的View Output来查看计算结果。注意,HLM6.08中通过View Output查看的是当前模型计算结果,如果模型修改,计算结果(txt文件)随之覆盖更新,若操作步骤较多,注意将计算结果另存。
(1) R语言代码及结果
library(nlme)
data0 <- read.csv("E:/02 研究参考/02 多层线性模型/02 R语言/Achieve.csv")
model1 <- lme(fixed = geread~1, random = ~1|school, data = data0)
summary(model1)
Linear mixed-effects model fit by REML
Data: data0
AIC BIC logLik
46274.31 46296.03 -23134.15
Random effects:
Formula: ~1 | school
(Intercept) Residual
StdDev: 0.6257119 2.24611
Fixed effects: geread ~ 1
Value Std.Error DF t-value p-value
(Intercept) 4.306753 0.05497501 10160 78.3402 0
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-2.3229469 -0.6377948 -0.2137753 0.2849664 3.8811630
Number of Observations: 10320
Number of Groups: 160
(2)HLM6.08操作步骤及结果
Final estimation of fixed effects
(with robust standard errors)
----------------------------------------------------------------------------
Standard Approx.
Fixed Effect Coefficient Error T-ratio d.f. P-value
----------------------------------------------------------------------------
For INTRCPT1, B0
INTRCPT2, G00 4.306758 0.054802 78.588 159 0.000
----------------------------------------------------------------------------
Final estimation of variance components:
-----------------------------------------------------------------------------
Random Effect Standard Variance df Chi-square P-value
Deviation Component
-----------------------------------------------------------------------------
INTRCPT1, U0 0.62562 0.39140 159 964.43194 0.000
level-1, R 2.24611 5.04502
-----------------------------------------------------------------------------
1.2.2 第一层添加自变量(非中心化)的两层模型
(1)添加一个自变量且不考虑自变量系数的随机效应
- R语言代码及结果
summary(model2)
Linear mixed-effects model fit by REML
Data: data0
AIC BIC logLik
43145.2 43174.17 -21568.6
Random effects:
Formula: ~1 | school
(Intercept) Residual
StdDev: 0.3158785 1.94074
Fixed effects: geread ~ gevocab
Value Std.Error DF t-value p-value
(Intercept) 2.0233559 0.04930868 10159 41.03447 0
gevocab 0.5128977 0.00837268 10159 61.25850 0
Correlation:
(Intr)
gevocab -0.758
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.0822506 -0.5734728 -0.2103488 0.3206692 4.4334337
Number of Observations: 10320
Number of Groups: 160
- HLM操作过程及结果
Final estimation of fixed effects
(with robust standard errors)
----------------------------------------------------------------------------
Standard Approx.
Fixed Effect Coefficient Error T-ratio d.f. P-value
----------------------------------------------------------------------------
For INTRCPT1, B0
INTRCPT2, G00 2.023358 0.065877 30.714 159 0.000
For GEVOCAB slope, B1
INTRCPT2, G10 0.512897 0.015608 32.861 10318 0.000
----------------------------------------------------------------------------
Final estimation of variance components:
-----------------------------------------------------------------------------
Random Effect Standard Variance df Chi-square P-value
Deviation Component
-----------------------------------------------------------------------------
INTRCPT1, U0 0.31589 0.09978 159 434.42829 0.000
level-1, R 1.94074 3.76647
-----------------------------------------------------------------------------
(2)添加一个自变量且考虑自变量系数的随机效应
- R语言代码及结果
summary(model3)
Linear mixed-effects model fit by REML
Data: data0
AIC BIC logLik
43004.85 43048.3 -21496.43
Random effects:
Formula: ~gevocab | school
Structure: General positive-definite, Log-Cholesky parametrization
StdDev Corr
(Intercept) 0.5316531 (Intr)
gevocab 0.1389443 -0.859
Residual 1.9146626
Fixed effects: geread ~ gevocab
Value Std.Error DF t-value p-value
(Intercept) 2.0057064 0.06108786 10159 32.83314 0
gevocab 0.5203554 0.01441548 10159 36.09699 0
Correlation:
(Intr)
gevocab -0.866
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.7102091 -0.5674433 -0.2074358 0.3176380 4.6774569
Number of Observations: 10320
Number of Groups: 160
- HLM操作过程及结果
Final estimation of fixed effects
(with robust standard errors)
----------------------------------------------------------------------------
Standard Approx.
Fixed Effect Coefficient Error T-ratio d.f. P-value
----------------------------------------------------------------------------
For INTRCPT1, B0
INTRCPT2, G00 2.005717 0.060883 32.944 159 0.000
For GEVOCAB slope, B1
INTRCPT2, G10 0.520350 0.014367 36.219 159 0.000
----------------------------------------------------------------------------
Final estimation of variance components:
-----------------------------------------------------------------------------
Random Effect Standard Variance df Chi-square P-value
Deviation Component
-----------------------------------------------------------------------------
INTRCPT1, U0 0.53154 0.28254 159 313.08314 0.000
GEVOCAB slope, U1 0.13894 0.01931 159 449.56769 0.000
level-1, R 1.91466 3.66593
-----------------------------------------------------------------------------
1.2.3 第二层添加自变量(非中心化)的两层模型
(1)不考虑第一层自变量系数的随机效应
- R语言代码及结果
summary(model4)
Linear mixed-effects model fit by REML
Data: data0
AIC BIC logLik
43162.1 43198.31 -21576.05
Random effects:
Formula: ~1 | school
(Intercept) Residual
StdDev: 0.3167654 1.94076
Fixed effects: geread ~ gevocab + senroll
Value Std.Error DF t-value p-value
(Intercept) 2.0748819 0.11400758 10159 18.19951 0.0000
gevocab 0.5128708 0.00837340 10159 61.25000 0.0000
senroll -0.0001026 0.00020511 158 -0.50012 0.6177
Correlation:
(Intr) gevocb
gevocab -0.327
senroll -0.901 -0.002
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.0834462 -0.5728938 -0.2103480 0.3212091 4.4335881
Number of Observations: 10320
Number of Groups: 160
- HLM操作过程及结果
Final estimation of fixed effects
(with robust standard errors)
----------------------------------------------------------------------------
Standard Approx.
Fixed Effect Coefficient Error T-ratio d.f. P-value
----------------------------------------------------------------------------
For INTRCPT1, B0
INTRCPT2, G00 2.074882 0.117873 17.603 158 0.000
SENROLL, G01 -0.000103 0.000184 -0.558 158 0.577
For GEVOCAB slope, B1
INTRCPT2, G10 0.512870 0.015592 32.893 10317 0.000
----------------------------------------------------------------------------
Final estimation of variance components:
-----------------------------------------------------------------------------
Random Effect Standard Variance df Chi-square P-value
Deviation Component
-----------------------------------------------------------------------------
INTRCPT1, U0 0.31677 0.10034 158 433.44703 0.000
level-1, R 1.94076 3.76655
-----------------------------------------------------------------------------


雷达卡









京公网安备 11010802022788号







