Loop nested loops (in R or Stata)

你。 提交于 2020-02-25 06:58:05

问题


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
            }
        }
    }
}

The equivalent in R is:

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
            }
        }
    }
}

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.

Thank you for any suggestions.

Additional Information:

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.


回答1:


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

  • 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

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

NOTE: Be careful as the number of combinations for 60 variables across varying lengths is very high!



来源:https://stackoverflow.com/questions/51969529/loop-nested-loops-in-r-or-stata

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