我在R中有一组模型,其中有一组因变量,但有相同的自变量。这个想法是建立一个模型列表(列表中的每个项目都有一个不同的因变量)。
一旦在列表中,我就无法访问模型摘要。
这个答案很接近,但不能完全满足我的需要:Appending models to list
如果我们设置这两个模型:
fit1 <- lm(Sepal.Length ~ Sepal.Width, data=iris)
fit2 <- lm(Sepal.Length ~ Petal.Width, data=iris)
,然后将它们作为列表加入:
x <- list(fit1, fit2)
我希望以下两行给出相同的输出:
summary(x[1])
summary(fit1)
这是我从summary(x [1])中得到的内容:
summary(x[1])
Length Class Mode
[1,] 12 lm list
同时,summary(fit1)恰好给了我我期望的结果:
...
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.5262 0.4789 13.63 <2e-16 ***
Sepal.Width -0.2234 0.1551 -1.44 0.152
...
在模型进入列表后,如何使结果看起来像summary(fit1)?
答案 0 :(得分:1)
问题是我们需要[[
才能提取list
元素。 [
将提取带有一个元素的list
identical(summary(x[[1]]), summary(fit1))
#[1] TRUE