对于混合模型,AIC计算在R和SAS中不匹配

时间:2018-01-08 14:51:10

标签: r sas lme4 mixed-models nlme

我尝试使用R重现一些SAS输出。我想要重现的方法是:

使用混合模型重复测量因子时间的方差的双向分析(协方差矩阵= CS,估计方法= REML)

一切看起来都不错AIC ...我想知道是否有人知道SAS使用的AIC公式......

主要的SAS输出是:

anova table

AIC and co

如果loglik相同,Anova表是相同的,但不是AIC(和BIC)事件。

这就是我用R做的:

library(nlme)
dataset_melt <- structure(list(Groupe = c("A", "A", "A", "A", "A", "B", "B", 
"B", "B", "B", "C", "C", "C", "C", "C", "A", "A", "A", "A", "A", 
"B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "A", "A", "A", 
"A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "A", 
"A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C", 
"C", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C", 
"C", "C", "C"), ID = c("01/001", "01/002", "01/003", "01/004", 
"01/005", "02/001", "02/002", "02/003", "02/004", "02/005", "03/001", 
"03/002", "03/003", "03/004", "03/005", "01/001", "01/002", "01/003", 
"01/004", "01/005", "02/001", "02/002", "02/003", "02/004", "02/005", 
"03/001", "03/002", "03/003", "03/004", "03/005", "01/001", "01/002", 
"01/003", "01/004", "01/005", "02/001", "02/002", "02/003", "02/004", 
"02/005", "03/001", "03/002", "03/003", "03/004", "03/005", "01/001", 
"01/002", "01/003", "01/004", "01/005", "02/001", "02/002", "02/003", 
"02/004", "02/005", "03/001", "03/002", "03/003", "03/004", "03/005", 
"01/001", "01/002", "01/003", "01/004", "01/005", "02/001", "02/002", 
"02/003", "02/004", "02/005", "03/001", "03/002", "03/003", "03/004", 
"03/005"), temps = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L), .Label = c("T0", "T1", "T2", "T3", "T4"), class = "factor"), 
    value = c(29.4, 21, 23.4, 26.2, 28.5, 27.8, 27.2, 20.6, 20.2, 
    25.3, 26.2, 29.2, 27.1, 23.1, 20.6, 22.9, 29.6, 20.9, 25.2, 
    25, 26, 26.7, 25.1, 21, 28.2, 23.4, 27.1, 29.8, 22.2, 26.6, 
    29.9, 29.1, 23.4, 22.6, 25.7, 24.5, 29.6, 21.5, 28.9, 20.1, 
    26.5, 23.4, 24.9, 25.3, 25, 27.4, 29.5, 24.6, 27.4, 24.6, 
    21.3, 23.6, 22.8, 23.6, 20.6, 26.5, 29.2, 20.6, 25.7, 29.1, 
    23.7, 24.3, 28.7, 21.9, 23.7, 29.8, 27.1, 28.7, 28.3, 20.4, 
    28.7, 20.3, 22.8, 23.4, 21.5)), row.names = c(NA, -75L), .Names = c("Groupe", 
"ID", "temps", "value"), class = "data.frame")

options(contrasts=c("contr.SAS","contr.poly"))
mon_lme <- lme(value ~ Groupe *temps, random = ~ +1 | ID,
        correlation=corCompSymm(form=~temps|ID), #na.action = na.exclude,
        data = dataset_melt,method='REML')
anova(mon_lme) # quite same as SAS

enter image description here

summary(mon_lme)$AIC
# 363.938
summary(mon_lme)$BIC
# 399.5419

k <- attr(logLik(mon_lme), "df")
aic <- 2 * k -2 * logLik(mon_lme) 
aic

-2 * logLik(mon_lme) # the same as SAS
#'log Lik.' 329.6698 (df=18)

什么是SAS AIC计算方法?

此致

2 个答案:

答案 0 :(得分:6)

您可以在帮助页面中找到根据SAS的AIC计算,例如:

http://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_mixed_sect008.htm#statug.mixed.mixedic

AIC在此计算为-2LL + 2d

LL是对数似然的最大值,d是模型的维数。在受限似然估计的情况下,d表示估计的协方差参数的有效数量。在这种情况下,输出中显示的是2个参数。

另一方面,R使用由Pinheiro和Bates计算的自由度。在SAS混合模型的背景下,他们对自由度的解释有着截然不同的解释。您可以使用函数int***

来查看
logLik

所以在R中,d的值是18.但是R也使用k = 2来计算AIC的标准。

答案 1 :(得分:0)

我试图通过反复试验找出答案,我认为SAS使用k = 2的AIC公式。这给出了2*2 - 2* (-164.8349) = 333.6698,它接近表中的值。但是,这不是k的价值所在,对我来说就像是一个错误。