楼主: 丨YC丨
4871 4

[经验] 多层线性模型在HLM与R中的对比1 [推广有奖]

  • 1关注
  • 9粉丝

已卖:3306份资源

博士生

80%

还不是VIP/贵宾

-

威望
0
论坛币
53378 个
通用积分
24.1045
学术水平
10 点
热心指数
5 点
信用等级
5 点
经验
24491 点
帖子
78
精华
0
在线时间
472 小时
注册时间
2012-5-5
最后登录
2025-2-8

楼主
丨YC丨 学生认证  发表于 2019-8-14 14:44:11 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
1 两层模型
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操作步骤及结果
1.png
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语言代码及结果
model2 <- lme(fixed = geread~gevocab, random = ~1|school, data = data0)
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操作过程及结果
2.png
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语言代码及结果
model3 <- lme(fixed = geread~gevocab, random = ~gevocab|school, data = data0)
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操作过程及结果
3.png
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语言代码及结果
model4 <- lme(fixed = geread~gevocab+senroll, random = ~1|school, data = data0)
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操作过程及结果
4.png
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
-----------------------------------------------------------------------------

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝


8.png (14.27 KB)

8.png

7.png (63.12 KB)

7.png

6.png (15.24 KB)

6.png

5.png (14.88 KB)

5.png

好好学习,天天向上

沙发
追求自我的水瓶座 发表于 2019-10-22 09:36:25
希望继续更新

藤椅
较拉峭 在职认证  发表于 2019-11-20 10:38:50
好,博主不错。

板凳
tj2020cyy 发表于 2020-5-5 21:55:16 来自手机
丨YC丨 发表于 2019-8-14 14:44
1 两层模型
1.1 数据准备
1.1.1 变量
无偿分享,楼主好人

报纸
林大的不归路 发表于 2020-9-3 17:15:58
请问R的结果怎么看卡方检验的结果?请问R中怎么确定变量是否需要采用随机斜率?

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-2-11 01:20