HLM软件输出到R:每个随机效果的独立固定效果

时间:2015-12-29 18:20:57

标签: r mixed-models

我尝试匹配R中另一个软件HLM中指定的模型的输出:http://justpaste.it/q10n enter image description here

enter image description here

到目前为止,我尝试的模型(具有相关截距的随机斜率)与固定效果不匹配:

m1 <- lmer(formula = mathach ~ 1 + freered + (1 + hrs | school), data = dat)

这里是用于比较的R输出:

summary(m1)
Linear mixed model fit by REML ['lmerMod']
Formula: 
mathach ~ 1 + freered + (1 + hrs | school)
   Data: dat

REML criterion at convergence: 180.2

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.1706 -0.4274 -0.0611  0.3395  4.3201 

Random effects:
 Groups   Name        Variance  Std.Dev. Corr 
 school   (Intercept) 102.96318 10.1471       
          hrs           6.03046  2.4557  -1.00
 Residual               0.09409  0.3067       
Number of obs: 100, groups:  school, 10

Fixed effects:
            Estimate Std. Error t value
(Intercept) 68.99782    0.59137  116.67
freered      0.48765    0.03025   16.12

Correlation of Fixed Effects:
        (Intr)
freered -0.895

和数据集:

dat <- structure(list(school = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), .Label = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "factor"),
student = 1:100, hrs = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L), mathach = c(100, 96, 92, 88, 84, 80, 76, 72, 68,
65, 95, 92, 89, 86, 83, 80, 77, 74, 71, 69, 90, 88, 86, 84,
82, 80, 78, 76, 74, 71, 85, 84, 83, 82, 81, 80, 79, 78, 77,
77, 80, 79.5, 79, 78.5, 78, 77.5, 77, 76.5, 76, 76, 75, 75.5,
76, 76.5, 77, 77.5, 78, 78.5, 79, 80, 70, 71, 72, 73, 74,
75, 76, 77, 78, 80, 65, 67, 69, 71, 73, 75, 77, 79, 81, 82,
60, 63, 66, 69, 72, 75, 78, 81, 84, 86, 55, 59, 63, 67, 71,
75, 79, 83, 87, 93), freered = c(30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("school",
"student", "hrs", "mathach", "freered"), row.names = c(NA, -100L
), class = "data.frame")

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这是一个开始:我们靠近(1)在固定效果中进行(1+hrs)freered之间的互动; (2)使hrsfreered变量居中(如HLM日志中所述):

library(lme4)
library(broom) ## for tidy() (cosmetic: may (???) need Github devel version?)

dat2 <- transform(dat,hrs=scale(hrs,scale=FALSE),
                  freered=scale(freered,scale=FALSE))
m1 <- lmer(formula = mathach ~ (1 + hrs)*freered + (1 + hrs | school),
           data = dat2)
## equivalent, explicit response formula:
##  ~ 1 + freered + hrs + freered : hrs + (1 + hrs | school)
print(tidy(m1),digits=3)
                        term estimate std.error statistic    group
1                (Intercept)  77.5400    0.2677   289.619    fixed
2                        hrs   0.0218    0.1245     0.175    fixed
3                    freered   0.3811    0.0307    12.399    fixed
4                hrs:freered  -0.2791    0.0143   -19.520    fixed
5      sd_(Intercept).school   0.8411        NA        NA   school
6              sd_hrs.school   0.3923        NA        NA   school
7 cor_(Intercept).hrs.school   0.1793        NA        NA   school
8    sd_Observation.Residual   0.3067        NA        NA Residual