0

I have a set of models in R where I have a set of dependent variables but the same independent variables. The idea is to build out a list of models (each item in the list has a different dependent variable).

I am having trouble accessing the model summary once it's in the list.

This answer is close, but doesn't get to exactly what I need: Appending models to list

If we set up these two models:

fit1 <- lm(Sepal.Length ~ Sepal.Width, data=iris)
fit2 <- lm(Sepal.Length ~ Petal.Width, data=iris)

and then join them as a list:

x <- list(fit1, fit2)

I would expect the following two lines to give the same output:

summary(x[1])
summary(fit1)

This is what I get from summary(x[1]):

summary(x[1])
     Length Class Mode
[1,] 12     lm    list

Meanwhile, summary(fit1) is giving me exactly what I expect:

...
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 
...

How can I get the results to look like summary(fit1) after a model is in a list?

Andy
  • 53
  • 1
  • 5
  • It should be `summary(x[[1]])` as `[[` extracts the `list` element whle `[` is still a list with one element – akrun Aug 19 '19 at 21:23

1 Answers1

1

The issue is that we need [[ for extracting the list element. The [ will extract a list with one element

identical(summary(x[[1]]), summary(fit1))
#[1] TRUE
akrun
  • 874,273
  • 37
  • 540
  • 662