-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcombinations_of_regressors_nu.ado
64 lines (51 loc) · 2.61 KB
/
combinations_of_regressors_nu.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
program define combinations_of_regressors_nu, rclass
syntax anything(id="all regressors" name=all_regressors) using/ [, MIN_number_of_vars(integer 1) MAX_number_of_vars(integer 20)]
version 10.1
marksample touse
tempname total_number_of_vars combination_loop_upper_limit vars_to_include include_var_or_not_pos regressor_combination include_var_or_not count actual_combs
// Figure out how many combinations of variables are possible: this is the number of loops for the outter for loop.
local `total_number_of_vars' : word count `all_regressors'
local `combination_loop_upper_limit' = 2^``total_number_of_vars''-1
di `"total_number_of_vars=``total_number_of_vars''"'
// di `"combination_loop_upper_limit=``combination_loop_upper_limit''"'
di `"min number of vars=`min_number_of_vars'"'
di `"max number of vars=`max_number_of_vars'"'
local `actual_combs' = 0
forv r = `min_number_of_vars'/`max_number_of_vars' {
local `actual_combs' = ``actual_combs'' + (exp(lnfactorial(``total_number_of_vars''))/(exp(lnfactorial(``total_number_of_vars''-`r'))*exp(lnfactorial(`r'))))
}
di `"number of combinations to be calculated=``actual_combs''"'
// Loop over all possible combinations
local `count' = 0
forv combination_number = 0/``combination_loop_upper_limit'' {
// di `"combination_number=`combination_number'"'
dec2bin_3, b(2) d(`combination_number') n(``total_number_of_vars'')
// di `"r(number_of_ones)=`r(number_of_ones)'"'
local `regressor_combination'
// Include regressor combination only if the combination has the specified minimum number of regressors
if (`r(number_of_ones)' >= `min_number_of_vars' & `r(number_of_ones)' <= `max_number_of_vars') {
local `count' = ``count'' + 1
local `vars_to_include' = r(contcatenated_startright)
// di `"vars_to_include=``vars_to_include''"'
local `include_var_or_not_pos' = 1
local `regressor_combination'
foreach regressor of local all_regressors {
local `include_var_or_not' = substr(`"``vars_to_include''"',``include_var_or_not_pos'',1)
if (``include_var_or_not''==1) {
local `regressor_combination' ``regressor_combination'' `regressor'
}
local `include_var_or_not_pos' = ``include_var_or_not_pos'' + 1
}
/*
di `"---------------------"'
di `"combination_number=`combination_number'"'
di `"count=``count''"'
di `"regressor_combination=``regressor_combination''"'
*/
return local regressor_combination_`combination_number' `"``regressor_combination''"'
return local regressor_combination_seq_``count'' `"``regressor_combination''"'
}
}
return scalar number_of_returned_combinations = ``count''
}
end