如何在 lmer() 模型上运行异方差性的 Breusch-Pagan 测试?

How to run a Breusch-Pagan Test for heteroskedasticity on lmer() model?

提问人:confusedindividual 提问时间:8/29/2022 最后编辑:jay.sfconfusedindividual 更新时间:9/13/2022 访问量:879

问:

为什么我不能在线性混合效应模型上运行 Breusch-Pagan 检验来检验异方差性?该函数适用于使用 和 构建的模型,但不能。我应该使用其他功能吗?bptest()lmer()bptestlmglmerlmer

错误信息

Error: $ operator not defined for this S4 class
data <- structure(list(Mn_new = c(3.90508190744665, 3.41518826685297, 
3.98107659173858, 4.06706444435455, 2.40431879320057, 3.8090250549363, 
3.72177711209025, 2.93248691964847, 4.10035133820019, 4.20508065155943, 
3.64103189844949, 4.24257964492719, 4.20182664641102, 3.41263061412322, 
4.04144915900294, 4.28185091235415, 3.09415352803393, 3.67021392570071, 
3.56418529613595, 3.21715355220772, 3.21429992539095, 3.54553486317315, 
4.03025205893711, 2.97382166830262, 3.80757707518732, 3.78523559035143, 
3.41487105608904, 2.75799799020337, 3.06834870580776, 3.30533869585591, 
2.8380338262522, 2.65147541433061, 3.53356800468757, 2.51733199167976, 
3.16115687664055, 3.64858366279116, 3.48272937241829, 2.91621249433787, 
3.26028181088023, 3.49589461456199, 2.82832109354896, 3.40328200399306, 
3.28568362736306, 2.87324453863543, 3.10651957200347, 2.81769064140214, 
2.57165695575711, 2.97592292304521, 3.18174081921005, 3.54312301316704, 
2.70447719350618, 3.48454089015539, 3.39666701335652, 3.03088932872189, 
3.1057376517166, 2.91083893666025, 3.18752169045788, 3.04054322208808, 
3.04284811683015, 3.53376439846743, 3.57155887085371, 2.67921235204479, 
3.24539585432457, 3.32270430796322, 3.75933211625452, 3.30303225771367, 
2.94140225772847, 3.22916966186489, 3.45512223500913, 2.89996056576201, 
3.19536565883228, 2.49108662931588, 2.55337036896523, 2.98316003461686, 
3.58241577241437, 3.40385600372579, 3.66136967423154, 3.71807222845311, 
3.73004186004765, 4.10988004656572, 3.90759927253415, 2.86608298949975, 
3.61450793458081, 3.85162032119424, 4.44992983828838, 3.19109366840847, 
3.09329595776341, 3.69955310870145, 4.47202033690943, 3.61326633240611, 
3.64532602062922, 3.33230174866167, 2.74653680127074, 3.61473897523957
), SEX = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"), S_M = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("AFTER", 
"BEFORE"), class = "factor"), ID = structure(c(43L, 40L, 25L, 
17L, 1L, 20L, 4L, 13L, 45L, 32L, 28L, 5L, 14L, 21L, 44L, 9L, 
16L, 42L, 18L, 35L, 22L, 10L, 8L, 36L, 37L, 15L, 19L, 43L, 40L, 
25L, 17L, 1L, 20L, 4L, 13L, 45L, 32L, 28L, 5L, 14L, 21L, 44L, 
9L, 16L, 42L, 18L, 35L, 22L, 10L, 8L, 36L, 37L, 15L, 19L, 47L, 
46L, 34L, 38L, 29L, 41L, 33L, 26L, 23L, 27L, 24L, 11L, 7L, 3L, 
6L, 12L, 30L, 39L, 2L, 31L, 47L, 46L, 34L, 38L, 29L, 41L, 33L, 
26L, 23L, 27L, 24L, 11L, 7L, 3L, 6L, 12L, 30L, 39L, 2L, 31L), .Label = c("BLA1", 
"BLA10", "BLA14", "BLA16", "BLA17", "BLA2", "BLA20", "BLA202", 
"BLA203", "BLA205", "BLA21", "BLA211", "BLA213", "BLA214", "BLA215", 
"BLA216", "BLA217", "BLA219", "BLA221", "BLA224", "BLA228", "BLA23", 
"BLA238", "BLA24", "BLA248", "BLA25", "BLA27", "BLA270", "BLA283", 
"BLA294", "BLA296", "BLA300", "BLA307", "BLA31", "BLA33", "BLA36", 
"BLA38", "BLA42", "BLA47", "BLA48", "BLA5", "BLA53", "BLA60", 
"BLA61", "BLA74", "BLA79", "BLA80"), class = "factor")), class = "data.frame", row.names = c(NA, 
-94L))

代码lmer

#Mg 
Mg_model <- lmer(Mg_new ~ SEX * S_M + (1|ID), data=data)
summary(Mg_model)
library(lmtest)
bptest(Mg_model)

错误

Error: $ operator not defined for this S4 class
R LME4

评论

1赞 jay.sf 8/29/2022
已经看过这个了吗?,他们使用.car::leveneTest
4赞 Rui Barradas 8/29/2022
你不应该测试残差吗?请不要认真对待以下内容,但有效的公式是 . 明确提到拟合模型,没有其他模型。bptest(resid(Mg_model) ~ SEX * S_M, varformula = ~ ID, data = data)lmtest::bptestlm

答:

1赞 Ben Bolker 9/13/2022 #1

Breusch-Pagan 检验“将线性回归模型拟合为线性回归模型的残差......默认情况下,采用与主回归模型相同的解释变量”。

基本 R 中的版本“适用于”和 模型,但我不相信它适用于模型——据我所知,测试不适用,只是它使用的泛型函数也适用于对象。(与您的问题相反,它抛出了一个适合的错误 - 也许您的意思是说?lmglmglmglmglmerglm

我不知道 B-P 测试是否已扩展到涵盖 LMM 案例。如果你有连续的预测变量,那会很棘手,但由于你只有因子,你可以使用Levene检验,如以下答案所示:

library(lme4)
library(broom.mixed)
library(ggplot2)
Mn_model <- lmer(Mn_new ~ SEX * S_M + (1|ID), data=data)
aa <- augment(Mn_model, .data = data)
ggplot(aa, aes(x = interaction(S_M,SEX), y = .resid)) + geom_boxplot()

enter image description here

car::leveneTest(.resid ~ S_M*SEX, data = aa)

## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value  Pr(>F)  
## group  3   2.271 0.08566 .
##       90