如何将列表的所有元素提供给需要单独对象的函数?

坎德森156

我希望能够使用 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将mongodb的连接对象提供给JasperFillManager.fillReport()-Spring MongoDB Jasper Integration

Theano:如何将训练数据提供给神经网络

角度2-指令如何将数据提供给主机?

将python WeakSet提供给列表构造函数是否安全?

如何将迭代器变量作为参数提供给ActionListener

如何将API密钥提供给Django模型

如何将Bert嵌入提供给LSTM

如何将bash命令列表提供给xargs以并行运行?

在安装过程中如何将Libnet标头提供给Nemesis?

如何将列表元素中的参数提供给类?

将红宝石提供给所有用户

在Hadoop中,“如何将整个文件作为输入提供给映射器?”

如何将集合提供给游标参数?

MySQL,如何将UNION的输出提供给WHERE子句

如何将GridPattern提供给自定义控件

如何将“数据”提供给jQuery延迟对象的done方法?

如何将数组(未知长度)的所有元素作为单独的参数传递给函数?

我如何有效地将2d python列表提供给C ++扩展模块

将子类的构造函数提供给父类的对象有什么意义?

如何将 IPv6 提供给 WSL2?

如何将提供给 bash 脚本的所有命令行参数(包括字符串参数)按原样传递给子进程?

如何将 user_ids 数组提供给 flickr.people.getInfo()?

如何将命令替换提供给“是”命令?

如何将 SHGetKnownFolderPath 输出提供给 C++ 中的 CopyFile?

如何将数据从 Firefox 配置文件文件夹提供给扩展程序

如何将对象作为关键字参数提供给关键字参数

使用 Bicep 将 API 管理私有 IP 地址提供给私有 DNS 条目时,不需要模板函数“引用”

如何将 fmx 的组件提供给 firemonkey C++ 中的函数?

如何将整数列表提供给 SQL 并返回项目的数据框