问题
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