从跨越式regsubsets获取所有模型 [英] Get all models from leaps regsubsets

查看:340
本文介绍了从跨越式regsubsets获取所有模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用regsubsets搜索模型.是否可以从参数选择列表中自动创建所有lm?

I used regsubsets to search for models. Is it possible to automatically create all lm from the list of parameter selections?

library(leaps)
leaps<-regsubsets(y ~ x1 + x2 + x3, data, nbest=1, method="exhaustive")
summary(leaps)$which
  (Intercept)      x1        x2    x3                                                                                   
1        TRUE   FALSE     FALSE  TRUE                                                                                   
2        TRUE   FALSE      TRUE  TRUE                                                                                   
3        TRUE    TRUE      TRUE  TRUE                                                                                   

现在,我将手动执行model_1 <- lm(y ~ x3),依此类推.如何自动将它们包含在列表中?

Now i would manually do model_1 <- lm(y ~ x3) and so on. How can this be automated to have them in a list?

推荐答案

我不知道为什么要列出所有模型. summarycoef方法应该很好地为您服务.但是,我将首先从纯粹的编程方面回答您的问题,然后再回到这一点.

I don't know why you want a list of all models. summary and coef methods should serve you well. But I will first answer your question from a pure programming aspect, then come back to this point.

一种简单的方法是通过reformulate:

A simple way to do this is via reformulate:

reformulate(termlabels, response = NULL, intercept = TRUE)

方法如下:

## you are masking `leaps` and `data` function!!
leaps <- regsubsets(y ~ x1 + x2 + x3, data, nbest = 1, method = "exhaustive")
X <- summary(leaps)$which

xvars <- dimnames(X)[[2]][-1]  ## column names (all covariates except intercept)
responsevar <- "y"  ## name of response

lst <- vector("list", dim(X)[1])  ## set up an empty model list

## loop through all rows / model specifications
for (i in 1:dim(X)[1]) {
  id <- X[i, ]
  form <- reformulate(xvars[which(id[-1])], responsevar, id[1])
  lst[[i]] <- lm(form, data)
  }

不需要*apply解决方案. lm的成本很高,所以for循环根本没有开销.

There is no need for a *apply solution. lm is costly, so for loop has no overhead at all.

一种更快的方法是建立一个包含所有协变量的模型矩阵,并动态选择其列(使用模型矩阵的assign属性;当您具有因子变量时尤其如此).然后通过.lm.fit进行模型拟合.但是,除非您是线性模型专家,否则您将很难以.lm.fit的原始输出来生成模型摘要/预测,但是我认为summary(leaps)应该已经返回了各种有用的统计信息.

A faster way is to set up a model matrix containing all covariates, and select its columns dynamically (use assign attributes of the model matrix; especially true when you have factor variables). Model fitting is then via .lm.fit. However, you will have difficulty in producing model summary / prediction with raw output of .lm.fit unless you are a linear model guru, but I think summary(leaps) should return you various useful statistic already.

leaps:::coef.regsubsets函数与该.lm.fit路由等效.只需:

leaps:::coef.regsubsets function is an equivalence of this .lm.fit route. Simply do:

coef(leaps, 1:dim(X)[1], TRUE)

您将获得所有模型的系数和方差-协方差矩阵.

and you will get coefficients and variance-covariance matrix for all models.

这篇关于从跨越式regsubsets获取所有模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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