从列表创建和调用线性模型 [英] Create and Call Linear Models from List

查看:60
本文介绍了从列表创建和调用线性模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试比较不同的线性模型,以确定一个模型是否优于另一个模型.但是,我有几个模型,因此我想创建一个模型列表,然后调用它们.有可能吗?

So I'm trying to compare different linear models in order to determine if one is better than another. However I have several models, so I want to create an list of models and then call on them. Is that possible?

 Models <- list(lm(y~a),lm(y~b),lm(y~c)
 Models2 <- list(lm(y~a+b),lm(y~a+c),lm(y~b+c))

 anova(Models2[1],Models[1])

谢谢您的帮助!

推荐答案

如果您有两个模型列表,并且想要比较每对模型,则需要Map:

If you have two lists of models, and you want to compare each pair of models, then you want Map:

models1 <- list(lm(y ~ a), lm(y ~ b), lm(y ~ c)
models2 <- list(lm(y ~ a + b), lm(y ~ a + c), lm(y ~ b + c))

Map(anova, models1, models2)

这基本上等效于以下for循环:

This is basically equivalent to the following for loop:

out <- vector("list", length(models1))
for (i in seq_along(out) {
  out[[i]] <- anova(models1[[i]], models2[[i]])
}

地图是功能的一个示例,您可以在 https://github.com/上找到有关它们的更多信息. hadley/devtools/wiki/Functionals

Map is an example of a functional, and you can find out more about them at https://github.com/hadley/devtools/wiki/Functionals

这篇关于从列表创建和调用线性模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆