Extract Adj R Square from a list of lm results [duplicate]

∥☆過路亽.° 提交于 2019-12-24 16:56:24

问题


I have used 50 predictor variables to create combinations of more than 2 million regression models for a single outcome variable. Most of these are nonsense--I want to eliminate all models with an Adjusted R-Square (AR2) below 0.7, and whose members have a vif>4 (from the car package). I have been first creating a list of all of the models (b), and in a second step, eliminating all models that meet my criteria using a for/if loop, creating a second object (bb). This is the second step:

### Create the filtered list
mlen <- length(b)
for (i in 1:mlen) {  
  if(summary(b[[i]])$adj.r.squared > .7 & all(vif(b[[i]]) <4)) {
      bb[i]<-b[i]
    }     
  }
### Get rid of all of the null results
bb <- bb[!sapply(bb, is.null)] 

This works, but it seems so ugly and inefficient. It seems like there should be an elegant way to use one of the apply commands (lapply, sapply), but the problem is twofold. First, the fact that AR2 isn't actually part of the normal lm results--I have to use "summary" to get AR2. Second, there is the subsetting problem since this is a list of lists. I can't just extract the summaries, and get the AR2 from those, using bb <- lapply(b, summary) -- I would have to use something like bb <- lapply(b, summary[[]]) but of course R doesn't like that.


回答1:


Sounds like Filter() could some in handy here

bb <- Filter(function(x) {
    summary(x)$adj.r.squared > .7 & all(vif(x) <4)
}, b)

You just pass it a function to tell it which objects you want to keep and the list if items you want to filter.



来源:https://stackoverflow.com/questions/36168579/extract-adj-r-square-from-a-list-of-lm-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!