2015-10-06 32 views
0

我注意到,当使用包含因子类型预测变量的lme4包中的lmer函数指定模型时,后缀指示预测器是因子水平的字符串,如这里用于治疗的情况下:混合()与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 

有谁知道是什么导致了预测变量标签级别后缀的差异和/或固定效应的差异?

回答

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 
+0

谢谢,这回答了我的两个问题之一! – luser

+0

其实,你回答了**两个**问题,接受:Henrik的更详细的答案可以看到[这里](https://github.com/singmann/afex/issues/4) – luser

+0

请避免交叉发帖未来,因为它会导致重复工作。根据你后来的帖子,我认为一个比较一般的指南可能是有用的,比如http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm – alexforrence