使用lapply返回线性模型的摘要 [英] returning summaries of linear models using lapply

查看:44
本文介绍了使用lapply返回线性模型的摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么下面的lapply函数不起作用以及不涉及for循环的可能替代方法.

Can anyone explain why the following lapply function does not work, and a possible alternative that does not involve a for-loop.

DV1 <- rnorm(20, 10, 3)
DV2 <- rnorm(20, 8, 3)
DV3 <- rnorm(20, 9, 3)
group <- rep(c("A", "B"), each = 2, length.out = 20)
df <- data.frame(group, DV1, DV2, DV3)

我想对多个结果变量进行分析.在此示例中,我创建了一个结果变量列表,以传递给lm函数.现在,我的理解是lapply将一个函数应用于列表并返回一个列表.那么,为什么不能给我列出三个 summary(lm())对象的列表,每个对象都是一个列表?有什么方法可以执行我要使用的一组应用功能吗?

I would like to perform analyses on multiple outcome variables. In this example I created a list of outcome variables to pass into a lm function. Now my understanding is that lapply applies a function to a list and return a list. So why can't it give me a list of three summary(lm()) objects, each of which is a list? Is there any way to do what I am trying to do with one of the apply family of functions?

cols <- list("DV1", "DV2", "DV3")

lapply(cols, function (x) summary(lm(x ~ group, data = df)))

推荐答案

尝试一下:

forms <- paste(cols, ' ~ group')
lapply(forms, lm, data = df)

或者如果您只想打印摘要而不是保存输出:

Or if you want to just print summaries rather than save output:

lapply(forms, function(x) summary(lm(x, data = df)))

这篇关于使用lapply返回线性模型的摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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