循环嵌套循环(在R或Stata中) [英] Loop nested loops (in R or Stata)

查看:942
本文介绍了循环嵌套循环(在R或Stata中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个60个尺寸的嵌套循环,也就是说,我相互嵌套了60个循环.在Stata中,MWE如下所示:

I have a nested loop with 60 dimensions, i.e. I nest 60 loops into each other. In Stata the MWE would look like the following:

forvalues i = 1/60 {
    forvalues j = 1/60 {
        forvalues k = 1/60 {
            forvalues l = 1/60 {
                ... imagine the 56 remaining loops here
            }
        }
    }
}

R中的等效项是:

for(i in 1:60) {
    for(j in 1:60) {
        for(k in 1:60) {
            for(l in 1:60) {
                ... imagine the 56 remaining loops here
            }
        }
    }
}

这里的目标是避免在我的代码中键入所有60个级别,而是为循环结构本身创建一个循环.这个问题显得微不足道.但是出于某种原因,我正在努力提出解决方案.

The objective here is to avoid typing all 60 levels into my code and create a loop for the loop structure itself instead. This question appears so trivial. But for some reason I am struggling to come up with a solution.

谢谢您的任何建议.

其他信息:

我有一个包含60个解释变量的数据集,并希望对这些变量的每种可能组合进行回归分析.更具体地说,我对所有60个解释变量分别进行因变量的单变量回归并计算某些条件.然后,将第二个回归变量添加到估计方程式中,然后再次计算标准. IE. reg DependentVar ExplVar1 ExplVar2reg DependentVar ExplVar1 ExplVar3,...,reg DependentVar ExplVar60 ExplVar59.取决于计算的标准,该回归树的分支可以提前,也可以终止.例如.第一个分支reg DependentVar ExplVar1 ExplVar2要么继续增长为reg DependentVar ExplVar1 ExplVar2 ExplVar3reg DependentVar ExplVar1 ExplVar2 ExplVar4等,要么终止为reg DependentVar ExplVar1 ExplVar2.包含多个解释性因素的分支也会被剪切-例如reg DependentVar ExplVar1 ExplVar1reg DependentVar ExplVar1 ExplVar2 ExplVar1.因此,总的来说,我设计了一种模型选择方法.我知道已经存在的模型选择命令,但是需要根据给定数据集的特定属性定制的命令.

I have a dataset with 60 explanatory variables in it and would like to run regressions with every possible combination of these variables. More specifically I run univariate regressions of the dependent variable on all 60 explanatory variables separately and calculate certain criteria. Then I add a second regressor to the estimation equation and calculate criteria again. I.e. reg DependentVar ExplVar1 ExplVar2, reg DependentVar ExplVar1 ExplVar3, ..., reg DependentVar ExplVar60 ExplVar59. Dependent on the calculated criteria a branch of that regression tree is either advanced or terminated. E.g. the first branch reg DependentVar ExplVar1 ExplVar2 either continues to grow as reg DependentVar ExplVar1 ExplVar2 ExplVar3, reg DependentVar ExplVar1 ExplVar2 ExplVar4 etc. or terminated as reg DependentVar ExplVar1 ExplVar2. Branches that contain an explanatory factor more than once are also cut - such as reg DependentVar ExplVar1 ExplVar1 or reg DependentVar ExplVar1 ExplVar2 ExplVar1. Overall, I hence design a model selection approach. I am aware of already existant model selection commands, but need one that is customized to specific properties of the given dataset.

推荐答案

rapplycombn一起考虑.下面演示了5个解释变量.对于实际用例:

Consider rapply with combn. Below demonstrates for 5 explanatory variables. For actual use case:

  • 用60个变量的名称替换paste0("ExplVar", 1:5)(可能使用names(df))
  • 将序列1:5替换为1:60,其中包括简单的一个变量回归
  • 用实际因变量替换 DepVar
  • replace paste0("ExplVar", 1:5) with names of your 60 variables (possibly using names(df))
  • replace sequence 1:5 to 1:60 which includes simple one variable regression
  • replace DepVar with actual dependent variable

作为 apply 系列的递归成员,rapply(我从没想过会被淘汰以获取SO答案!)将从嵌套中构建线性公式的字符向量然后可以使用lm进行迭代的列表:

Being the the recursive member of the apply family, rapply (which I never dreamed would be dusted off the shelf for an SO answer!) will build a character vector of linear formulas from nested list which can then be iterated with lm:

expvar_list <- lapply(1:5, function(x) combn(paste0("ExplVar", 1:5), x, simplify=FALSE))

formulas_list <- rapply(expvar_list, function(x) paste("DepVar ~", paste(x, collapse="+")))
formulas_list
#  [1] "DepVar ~ ExplVar1"                                    
#  [2] "DepVar ~ ExplVar2"                                    
#  [3] "DepVar ~ ExplVar3"                                    
#  [4] "DepVar ~ ExplVar4"                                    
#  [5] "DepVar ~ ExplVar5"                                    
#  [6] "DepVar ~ ExplVar1+ExplVar2"                           
#  [7] "DepVar ~ ExplVar1+ExplVar3"                           
#  [8] "DepVar ~ ExplVar1+ExplVar4"                           
#  [9] "DepVar ~ ExplVar1+ExplVar5"                           
# [10] "DepVar ~ ExplVar2+ExplVar3"                           
# [11] "DepVar ~ ExplVar2+ExplVar4"                           
# [12] "DepVar ~ ExplVar2+ExplVar5"                           
# [13] "DepVar ~ ExplVar3+ExplVar4"                           
# [14] "DepVar ~ ExplVar3+ExplVar5"                           
# [15] "DepVar ~ ExplVar4+ExplVar5"                           
# [16] "DepVar ~ ExplVar1+ExplVar2+ExplVar3"                  
# [17] "DepVar ~ ExplVar1+ExplVar2+ExplVar4"                  
# [18] "DepVar ~ ExplVar1+ExplVar2+ExplVar5"                  
# [19] "DepVar ~ ExplVar1+ExplVar3+ExplVar4"                  
# [20] "DepVar ~ ExplVar1+ExplVar3+ExplVar5"                  
# [21] "DepVar ~ ExplVar1+ExplVar4+ExplVar5"                  
# [22] "DepVar ~ ExplVar2+ExplVar3+ExplVar4"                  
# [23] "DepVar ~ ExplVar2+ExplVar3+ExplVar5"                  
# [24] "DepVar ~ ExplVar2+ExplVar4+ExplVar5"                  
# [25] "DepVar ~ ExplVar3+ExplVar4+ExplVar5"                  
# [26] "DepVar ~ ExplVar1+ExplVar2+ExplVar3+ExplVar4"         
# [27] "DepVar ~ ExplVar1+ExplVar2+ExplVar3+ExplVar5"         
# [28] "DepVar ~ ExplVar1+ExplVar2+ExplVar4+ExplVar5"         
# [29] "DepVar ~ ExplVar1+ExplVar3+ExplVar4+ExplVar5"         
# [30] "DepVar ~ ExplVar2+ExplVar3+ExplVar4+ExplVar5"         
# [31] "DepVar ~ ExplVar1+ExplVar2+ExplVar3+ExplVar4+ExplVar5"

models_list <- lapply(formulas_list, function(x) summary(lm(as.formula(x), mydata)))

注意:请注意,不同长度的60个变量的组合数量非常多!

这篇关于循环嵌套循环(在R或Stata中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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