我希望能够使用 vcdExtra 包中的 LRstats 函数来比较 50 个模型。但是,此功能似乎已设置为需要单独提供每个模型对象。如果我有 50 个模型的列表,我该如何做到这一点而无需手动指定每个模型,即 test[[1]]、test[[2]]、test[[3]]...等?
library(vcdExtra)
library(dplyr)
test <- data.frame(
y = rbinom(2500, 1, 0.5),
x1 = rnorm(2500, mean = 0, sd = 1),
x2 = rnorm(2500, mean = 1, sd = 3),
z = rep(seq(from = 400, length.out = 50, by = 400), times = 50))
test_list <- group_split(test, z)
names(test_list) <- unique(test$z)
test_models <- lapply(test_list, function(x) glm(y ~ x1 + x2, data = x, family = "binomial"))
test_models2 <- glmlist(test_models)
>Warning message:
In glmlist(test_models) :
Objects test_models removed because they are not glm objects
test_LR <- LRstats(test_models2)
>NULL
问题在于函数glmlist
的使用方式。glmlist
期望一个或多个对象继承自 class"glm"
或"loglm"
. 从文档中:
任何不继承适当类 glm 或 loglm 的对象都将被排除,并带有警告。
因此,使用 . 调用每个列表成员上的函数do.call
。
library(vcdExtra)
#> Loading required package: vcd
#> Loading required package: grid
#> Loading required package: gnm
test <- data.frame(
y = rbinom(2500, 1, 0.5),
x1 = rnorm(2500, mean = 0, sd = 1),
x2 = rnorm(2500, mean = 1, sd = 3),
z = rep(seq(from = 400, length.out = 50, by = 400), times = 50))
test_list <- dplyr::group_split(test, z)
names(test_list) <- unique(test$z)
test_models <- lapply(test_list, function(x) glm(y ~ x1 + x2, data = x, family = "binomial"))
test_models2 <- do.call(glmlist, test_models)
test_LR <- LRstats(test_models2)
head(test_LR)
#> Likelihood summary table:
#> AIC BIC LR Chisq Df Pr(>Chisq)
#> 400 72.136 77.872 66.136 47 0.03420 *
#> 800 68.608 74.344 62.608 47 0.06340 .
#> 1200 74.842 80.578 68.842 47 0.02056 *
#> 1600 73.545 79.281 67.545 47 0.02634 *
#> 2000 73.514 79.250 67.514 47 0.02649 *
#> 2400 70.524 76.260 64.524 47 0.04564 *
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
由reprex 包于 2022-03-31 创建(v2.0.1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句