估算方差归因于固定效应

时间:2016-12-20 10:27:22

标签: r variance mixed-models

无论它有多“重要”,我有兴趣尝试估计有多少方差归因于单个固定效应(它是主效应或交互项)。

快速思考我想象为混合模型的预测值构建线性模型(没有随机效应),并且评估ANOVA表将提供估计(是的,剩余方差将为零,但是我们从混合模型中知道(?)这个。然而,从玩弄显然不是。

我推理的缺陷在哪里?或者我在路上做错了什么?有替代方法吗?

免责声明:我知道有些人建议在删除/添加固定效果时查看剩余方差的变化,但由于这并未考虑固定效应和随机效应之间的相关性,我不感兴趣。

data(Orthodont,package="nlme")
Orthodont = na.omit(Orthodont)
#Fitting a linear mixed model
library(lme4)
mod = lmer(distance ~ age*Sex + (1|Subject) , data=Orthodont)
# Predicting across all observed values, 
pred.frame = expand.grid(age = seq(min(Orthodont$age, na.rm = T),max(Orthodont$age, na.rm=T)), 
            Sex = unique(Orthodont$Sex))
# But not including random effects
pred.frame$fit = predict(mod, newdata = pred.frame, re.form=NA)
anova(lm(fit~age*Sex, data = pred.frame))
library(data.table)
Orthodont = data.table(Orthodont)
# to test the validity of the approach
# by estimating a linear model using a random observation 
# per individual and look at the means
tmp = sapply(1:500, function(x){
     print(x)
     as.matrix(anova(lm(distance~age*Sex, data =Orthodont[,.SD[sample(1:.N,1)],"Subject"])))[,2]
}
)
# These are clearly not similar
    prop.table(as.table(rowMeans(tmp)[-4]))
       age        Sex    age:Sex 
0.60895615 0.31874622 0.07229763 
> prop.table(as.table(anova(lm(fit~age*Sex, data = pred.frame))[1:3,2]))
         A          B          C 
0.52597575 0.44342996 0.03059429 

0 个答案:

没有答案