首页 文章

fixed()vs lmer()输出用于固定效果因子标签:数字与字符

提问于
浏览
0

我注意到,当使用包含因子类型预测变量的 lme4 包中的 lmer 函数指定模型时,指示预测变量级别的后缀是该因子级别的字符串,如下处理的情况:

library(afex)

data(obk.long)

m1 <- lmer(value ~ treatment + (1|id), obk.long)
summary(m1)

Fixed effects:
        Estimate Std. Error t value
(Intercept)    4.200      0.654    6.43
treatmentA     2.050      0.980    2.09
treatmentB     1.800      0.856    2.10

但是,在 afex 包中使用 mixed 函数时,后缀为数字:

m2 <- mixed(value ~ treatment + (1|id), obk.long)
summary(m2$full.model) # this should be the same as the lmer output... it's er, not

Fixed effects:
            Estimate Std. Error t value
(Intercept)    5.483      0.375   14.62
treatment1    -1.283      0.532   -2.41
treatment2     0.767      0.565    1.36

有谁知道导致预测标签级别后缀和/或固定效果差异的原因是什么?

1 回答

  • 3

    afex 默认情况下将分类预测变量的对比度编码设置为总和对比(当您使用 mixed 时在消息中提及),而 lmer 调用中指定的模型使用R的全局选项中的对比度设置 .

    options('contrasts')
    ##$contrasts
    ##        unordered           ordered 
    ##"contr.treatment"      "contr.poly" 
    
    obk2 <- obk.long
    contrasts(obk2$treatment) <- "contr.sum"
    
    # Or alternatively, set the global option with something like:
    # options(contrasts=c('contr.sum', 'contr.poly'))
    
    m_contr <- lmer(value ~ treatment + (1|id), obk2)
    
    summary(m_contr)$coefficients # fixed effects only for brevity
    ##              Estimate Std. Error   t value
    ##(Intercept)  5.4833333  0.3751349 14.616966
    ##treatment1  -1.2833333  0.5321163 -2.411753
    ##treatment2   0.7666667  0.5645823  1.357936
    
    all.equal(summary(m2)$coefficients, summary(m_contr)$coefficients)
    ##[1] TRUE
    

相关问题