“摘要"多个LM和GLM对象 [英] `Summary` multiple LM and GLM objects
问题描述
我在R中有50个lm
和glm
对象.我想获取所有这些对象的摘要,而不必键入50次.有快速的方法吗?数据集的名称以相同的方式开头: lin.mod.t#或 lin.mod.s#,其中{#= 1,2,...,25 }.我可以使用
I have 50 lm
and glm
objects in R. I want to get the summary of all of them without having to type summary(lm...)
50 times. Is there a quick way to do it? The names of the dataset begin the same way: lin.mod.t# or lin.mod.s# where {# = 1,2,...,25}. I can list all the models using
ls(pattern = "lin.mod")
objects(pattern = "lin.mod")
但是对于使用ls
或objects
的结果,我无法运行summary
.我使用了summary(eval(parse(text = ls(pattern = "lin.mod"))))
,但这仅运行第一个.有什么建议?也许使用lapply
?
But I cannot run summary
for the outcomes of using ls
or objects
. I used summary(eval(parse(text = ls(pattern = "lin.mod"))))
, but this only runs the first one. Any suggestions? Maybe use lapply
?
推荐答案
如何?
# 2 sample models
lin.mod.1 <- lm(mpg ~ wt, data = mtcars);
lin.mod.2 <- lm(mpg ~ wt, data = mtcars);
# Get models and store in list
lst <- lapply(ls(pattern = "lin.mod"), get);
# Summary of models
res <- lapply(lst, summary);
说明:get
与当前环境中的模式"lin.mod"
匹配并存储在列表lst
中的所有模型.使用lapply
将所有模型的summary
存储在列表res
中.
Explanation: get
all models that match pattern "lin.mod"
from the current environment and store in list lst
. Use lapply
to store summary
of all models in list res
.
这篇关于“摘要"多个LM和GLM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!