如何计算广义线性混合效应模型的中位数绝对偏差(MAD)

时间:2016-08-17 15:51:39

标签: r lme4 mixed-models

我知道我的问题与统计数据有关,但我正在寻找R的解决方案,因此我认为它适用于SO。

我使用glmer中的lme4包中的R函数构建了一个广义线性混合效应模型(GLMM),以基于显着的解释变量模拟水产养殖场周围的物种丰富度Zuur等。 (2009) Mixed Effects Models and Extensions in Ecology with R 。该模型是:

Mod1 <- glmer(Richness ~ Distance + Depth + Substrate + Beggiatoa + 
        Distance*Beggiatoa + (1|Site/transect), family = poisson, data = mydata)

现在我有一个在不同站点收集的完整数据集,我想评估此模型在新数据集上的表现。

在CV上question之后,有人建议在新数据集上查找中位数绝对偏差(mad)。我尝试了madstats包中的R函数,但收到以下错误消息:

Error in x[!is.na(x)] : object of type 'S4' is not subsettable
In addition: Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'S4'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'S4'

有人知道这里出了什么问题吗?是否无法为GLMM计算mad中的stats?如果是这样,是否有其他R包从GLMM计算疯狂?

修改

为了让您了解我的数据,这里是dput(head(mydata))的输出,同时请注意新数据集中没有“Substrate”类别,而“S”指的是“Richness”:

structure(list(S = c(0, 1, 2, 3, 3, 2), Site = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("BC", "BH", "GC", "IS", "Ref"
), class = "factor"), Transect = structure(c(4L, 4L, 4L, 4L, 
4L, 4L), .Label = c("10GC", "10IS", "10N", "10S", "11IS", "12IS", 
"13E", "1GC", "1N", "1W", "2E", "2GC", "2IS", "2N", "2W", "2WA", 
"3E", "3GC", "3IS", "3N", "3S", "4E", "4GC", "4IS", "4S", "4W", 
"5GC", "5IS", "5S", "6GC", "6IS", "6N", "6S", "6W", "7E", "7GC", 
"7IS", "8GC", "8IS", "8W", "9E", "9GC", "9IS", "9N", "RefBC1", 
"RefBC10", "RefBC11", "RefBC12", "RefBC2", "RefBC3", "RefBC4", 
"RefBC5", "RefBC6", "RefBC7", "RefBC8", "RefBC9", "X1", "X2"), class = "factor"), 
Distance = c(2, 20, 40, 80, 120, 160), Depth = c(40L, 40L, 
50L, 40L, 40L, 40L), Beggiatoa = c(2, 1, 1, 0, 0, 0)), .Names = c("S", 
"Site", "Transect", "Distance", "Depth", "Beggiatoa"), row.names = c(NA, 
6L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

对于 in-sample 错误,中位绝对偏差计算只是

mad(residuals(fitted_model))

...您可能需要residuals(fitted_model,type="response"),因为residuals默认会为您提供偏差残差(参见?residuals.merMod

如果您想查看样例外错误,可以执行以下操作:

pred <- predict(fitted_model,
                newdata = newdf,
                type = "response",
                re.form=~0)
mad(pred, center=newdf$S)

re.form=~0指定您要省略预测中的随机效果,这是您唯一的选择,除非您在已经获得培训数据的网站/横断面上预测