从lm结果列表中提取Adj R Square [英] Extract Adj R Square from a list of lm results

查看:91
本文介绍了从lm结果列表中提取Adj R Square的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了50个预测变量来为单个结果变量创建超过200万个回归模型的组合.其中大多数都是胡说八道-我想消除所有调整R平方(AR2)低于0.7,且成员的vif> 4(从汽车包装中)的模型.我首先创建了所有模型的列表(b),然后在第二步中,使用for/if循环消除了符合我的条件的所有模型,创建了第二个对象(bb).这是第二步:

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)] 

这有效,但是看起来很丑陋且效率低下.似乎应该有一种优雅的方法来使用一个apply命令(lapply,sapply),但是问题是双重的.首先,AR2实际上不是正常的lm结果的一部分这一事实-我必须使用摘要"来获得AR2.其次,存在子集问题,因为这是一个列表列表.我不能只提取摘要,并使用这些摘要来获取AR2 bb<-lapply(b,摘要) -我将不得不使用类似 bb<-lapply(b,摘要[[]]) 但当然R不喜欢那样.

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.

推荐答案

Filter()这样的声音在这里可能会派上用场

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.

这篇关于从lm结果列表中提取Adj R Square的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆