2015-10-06 31 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