Max M Max M - 1 year ago 63
R Question

Extract coefficients from a list of estimation objects

I saw some similar questions but my questions seems to be simpler.

I am running a more complicated regression than my MWE and end up with a list of estimation objects. I was wondering if there is a more elegant way of extracting the coefficients of the list, than using another for loop over the number or names of the list.

for(i in 1:3) {
list.lm[[i]]<-lm(mpg~cyl+runif(32), data=mtcars)

I know that I can use apply to get the coeffcients, but I cannot transform this



t(sapply(list.lm, coefficients))

ok then I can do


which is the same as with my loop

results<-data.frame(iteration=numeric(),intercept=numeric(), cyl=numeric(), rand=numeric())
for(i in 1:3) {

Answer Source

One simple way would be to just use sapply and transpose the result, rather than lapply.

#      (Intercept)       cyl runif(32)
# [1,]    35.43360 -2.774654  4.163870
# [2,]    38.71960 -2.840392 -1.896252
# [3,]    38.97739 -2.784622 -3.955039

You might also want to look into Hadley's purrr package and/or David Robinson's broom package. See and