Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Regression for all possible combinations of variables

    Hello all, I am a new STATA user and trying to run a foreach regression loop.

    I have 32 independent variables of which B C D are log transformed, E and F are categorical, and the rest are dummy variables.

    I have a log transformed independent variable (ln_A) that I want to regress against various combinations of the independent variables.

    The total number of observations in my dataset is 800. I am interested in obtaining the regression coefficients for each combination of variables regressed against ln_y and export the coefficients as well r2, adj_r2, and rmse into an excel file.

    the code I have tried so far is as follows:

    local predictors ln_B ln_C ln_D E F G H I J…etc
    local regressors

    foreach p of local predictors {
    local regressors `regressors’ `p’
    regress ln_A `regressors’
    outreg2 using file, excel e(r2_a rmse)
    }

    This code works but for each iteration it adds another variable to the regression. Whereas, I want to randomize the combination of variables used for each iteration so it would be something like:

    reg ln_A ln_B E F I J
    reg lnA E K L N F
    etc.

    All help would be greatly appreciated!

  • #2
    I do not know what you gain from such randomizations, but you can use tuples from SSC to create all combinations of 5 from a list exceeding 5. With 8 variables, you have 56 such combinations as

    $$56= \frac{8\text{!}}{5\text{!}(8-5)\text{!}}. $$

    In the auto dataset

    Code:
    sysuse auto, clear
    *ssc install tuples, replace
    tuples price mpg rep78 headroom trunk weight length turn, min(5) max(5)
    macro list
    Res.:

    Code:
    . macro list
    
    _ntuples:       56
    _tuple56:       price mpg rep78 headroom trunk
    _tuple55:       price mpg rep78 headroom weight
    _tuple54:       price mpg rep78 headroom length
    _tuple53:       price mpg rep78 headroom turn
    _tuple52:       price mpg rep78 trunk weight
    _tuple51:       price mpg rep78 trunk length
    _tuple50:       price mpg rep78 trunk turn
    _tuple49:       price mpg rep78 weight length
    _tuple48:       price mpg rep78 weight turn
    _tuple47:       price mpg rep78 length turn
    _tuple46:       price mpg headroom trunk weight
    _tuple45:       price mpg headroom trunk length
    _tuple44:       price mpg headroom trunk turn
    _tuple43:       price mpg headroom weight length
    _tuple42:       price mpg headroom weight turn
    _tuple41:       price mpg headroom length turn
    _tuple40:       price mpg trunk weight length
    _tuple39:       price mpg trunk weight turn
    _tuple38:       price mpg trunk length turn
    _tuple37:       price mpg weight length turn
    _tuple36:       price rep78 headroom trunk weight
    _tuple35:       price rep78 headroom trunk length
    _tuple34:       price rep78 headroom trunk turn
    _tuple33:       price rep78 headroom weight length
    _tuple32:       price rep78 headroom weight turn
    _tuple31:       price rep78 headroom length turn
    _tuple30:       price rep78 trunk weight length
    _tuple29:       price rep78 trunk weight turn
    _tuple28:       price rep78 trunk length turn
    _tuple27:       price rep78 weight length turn
    _tuple26:       price headroom trunk weight length
    _tuple25:       price headroom trunk weight turn
    _tuple24:       price headroom trunk length turn
    _tuple23:       price headroom weight length turn
    _tuple22:       price trunk weight length turn
    _tuple21:       mpg rep78 headroom trunk weight
    _tuple20:       mpg rep78 headroom trunk length
    _tuple19:       mpg rep78 headroom trunk turn
    _tuple18:       mpg rep78 headroom weight length
    _tuple17:       mpg rep78 headroom weight turn
    _tuple16:       mpg rep78 headroom length turn
    _tuple15:       mpg rep78 trunk weight length
    _tuple14:       mpg rep78 trunk weight turn
    _tuple13:       mpg rep78 trunk length turn
    _tuple12:       mpg rep78 weight length turn
    _tuple11:       mpg headroom trunk weight length
    _tuple10:       mpg headroom trunk weight turn
    _tuple9:        mpg headroom trunk length turn
    _tuple8:        mpg headroom weight length turn
    _tuple7:        mpg trunk weight length turn
    _tuple6:        rep78 headroom trunk weight length
    _tuple5:        rep78 headroom trunk weight turn
    _tuple4:        rep78 headroom trunk length turn
    _tuple3:        rep78 headroom weight length turn
    _tuple2:        rep78 trunk weight length turn
    _tuple1:        headroom trunk weight length turn
    
    S_FNDATE:       13 Apr 2018 17:45
    So you can pick the tuples and run regressions, e.g., for the first 5 tuples:

    Code:
    sysuse auto, clear
    *ssc install tuples, replace
    tuples price mpg rep78 headroom trunk weight length turn, min(5) max(5)
    foreach depvar in disp gear{
        forval i=1/5{
            display "command: reg `depvar' `tuple`i'', robust"
            regress `depvar' `tuple`i'', robust
        }
    }
    Res.:

    Code:
    . foreach depvar in disp gear{
      2. 
    .     forval i=1/5{
      3. 
    .         display "command: reg `depvar' `tuple`i'', robust"
      4. 
    .         regress `depvar' `tuple`i'', robust
      5. 
    .     }
      6. 
    . }
    command: reg disp headroom trunk weight length turn, robust
    
    Linear regression                               Number of obs     =         74
                                                    F(5, 68)          =     100.03
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.8064
                                                    Root MSE          =     41.866
    
    ------------------------------------------------------------------------------
                 |               Robust
    displacement |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
        headroom |   7.463084   5.705514     1.31   0.195    -3.922091    18.84826
           trunk |  -.0570543   1.327227    -0.04   0.966    -2.705495    2.591387
          weight |   .1151405   .0210735     5.46   0.000      .073089    .1571921
          length |  -.7435477   .7192435    -1.03   0.305    -2.178776    .6916805
            turn |    1.45064   2.149174     0.67   0.502     -2.83797    5.739251
           _cons |  -89.69802   80.26664    -1.12   0.268    -249.8676    70.47157
    ------------------------------------------------------------------------------
    command: reg disp rep78 trunk weight length turn, robust
    
    Linear regression                               Number of obs     =         69
                                                    F(5, 63)          =     134.23
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.8765
                                                    Root MSE          =     34.005
    
    ------------------------------------------------------------------------------
                 |               Robust
    displacement |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |  -3.465495   4.384528    -0.79   0.432    -12.22727    5.296284
           trunk |   1.424145   .8280448     1.72   0.090    -.2305707     3.07886
          weight |   .1286264   .0223755     5.75   0.000     .0839124    .1733404
          length |  -1.202641   .7024589    -1.71   0.092    -2.606393    .2011114
            turn |   1.363678   1.761373     0.77   0.442    -2.156144    4.883501
           _cons |  -27.85643   98.29772    -0.28   0.778    -224.2887    168.5759
    ------------------------------------------------------------------------------
    command: reg disp rep78 headroom weight length turn, robust
    
    Linear regression                               Number of obs     =         69
                                                    F(5, 63)          =     107.89
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.8774
                                                    Root MSE          =     33.881
    
    ------------------------------------------------------------------------------
                 |               Robust
    displacement |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |  -2.990048    4.36038    -0.69   0.495    -11.70357    5.723476
        headroom |   6.891369   3.982449     1.73   0.088     -1.06692    14.84966
          weight |   .1282691    .021362     6.00   0.000     .0855804    .1709577
          length |  -1.120746   .6276612    -1.79   0.079    -2.375027    .1335351
            turn |   1.370387   1.734392     0.79   0.432     -2.09552    4.836294
           _cons |  -44.91849   95.13304    -0.47   0.638    -235.0267    145.1897
    ------------------------------------------------------------------------------
    command: reg disp rep78 headroom trunk length turn, robust
    
    Linear regression                               Number of obs     =         69
                                                    F(5, 63)          =      48.61
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.7664
                                                    Root MSE          =      46.77
    
    ------------------------------------------------------------------------------
                 |               Robust
    displacement |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |  -6.743106   5.516461    -1.22   0.226    -17.76687    4.280662
        headroom |   4.752432   6.539184     0.73   0.470    -8.315083    17.81995
           trunk |  -.0155672   1.331233    -0.01   0.991    -2.675823    2.644689
          length |   2.559564   .4314869     5.93   0.000     1.697306    3.421822
            turn |   4.590105   2.362289     1.94   0.056    -.1305529    9.310762
           _cons |  -457.6876   68.58535    -6.67   0.000    -594.7445   -320.6307
    ------------------------------------------------------------------------------
    command: reg disp rep78 headroom trunk weight turn, robust
    
    Linear regression                               Number of obs     =         69
                                                    F(5, 63)          =      96.36
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.8711
                                                    Root MSE          =     34.747
    
    ------------------------------------------------------------------------------
                 |               Robust
    displacement |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |  -4.306183   4.471858    -0.96   0.339    -13.24248    4.630112
        headroom |   4.900558   4.630183     1.06   0.294    -4.352124    14.15324
           trunk |  -.1990481   .9874427    -0.20   0.841    -2.172295    1.774199
          weight |    .104504   .0110301     9.47   0.000     .0824621     .126546
            turn |   .2064708   1.885977     0.11   0.913    -3.562354    3.975295
           _cons |  -124.3397   65.50142    -1.90   0.062    -255.2338    6.554487
    ------------------------------------------------------------------------------
    command: reg gear headroom trunk weight length turn, robust
    
    Linear regression                               Number of obs     =         74
                                                    F(5, 68)          =      19.46
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.5878
                                                    Root MSE          =     .30353
    
    ------------------------------------------------------------------------------
                 |               Robust
      gear_ratio |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
        headroom |  -.0132194   .0503739    -0.26   0.794    -.1137389       .0873
           trunk |  -.0030621   .0149072    -0.21   0.838    -.0328091    .0266848
          weight |  -.0005295   .0001265    -4.18   0.000     -.000782    -.000277
          length |    .006687   .0049584     1.35   0.182    -.0032073    .0165812
            turn |  -.0163174   .0179992    -0.91   0.368    -.0522342    .0195994
           _cons |   4.085656   .5627702     7.26   0.000     2.962666    5.208647
    ------------------------------------------------------------------------------
    command: reg gear rep78 trunk weight length turn, robust
    
    Linear regression                               Number of obs     =         69
                                                    F(5, 63)          =      21.49
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.6430
                                                    Root MSE          =     .28722
    
    ------------------------------------------------------------------------------
                 |               Robust
      gear_ratio |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |   .0451922   .0440179     1.03   0.308    -.0427706    .1331551
           trunk |  -.0040699   .0135043    -0.30   0.764     -.031056    .0229163
          weight |   -.000559    .000122    -4.58   0.000    -.0008028   -.0003152
          length |   .0063107   .0052304     1.21   0.232    -.0041414    .0167628
            turn |  -.0075665   .0178414    -0.42   0.673    -.0432197    .0280867
           _cons |   3.709777   .6243065     5.94   0.000       2.4622    4.957354
    ------------------------------------------------------------------------------
    command: reg gear rep78 headroom weight length turn, robust
    
    Linear regression                               Number of obs     =         69
                                                    F(5, 63)          =      21.98
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.6430
                                                    Root MSE          =     .28722
    
    ------------------------------------------------------------------------------
                 |               Robust
      gear_ratio |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |   .0437331   .0450183     0.97   0.335    -.0462289     .133695
        headroom |  -.0161693   .0473584    -0.34   0.734    -.1108075    .0784689
          weight |  -.0005574   .0001181    -4.72   0.000    -.0007934   -.0003215
          length |   .0059858   .0044248     1.35   0.181    -.0028565    .0148281
            turn |  -.0075714    .017849    -0.42   0.673    -.0432398    .0280969
           _cons |   3.763233   .5919443     6.36   0.000     2.580326    4.946139
    ------------------------------------------------------------------------------
    command: reg gear rep78 headroom trunk length turn, robust
    
    Linear regression                               Number of obs     =         69
                                                    F(5, 63)          =      19.28
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.5580
                                                    Root MSE          =     .31957
    
    ------------------------------------------------------------------------------
                 |               Robust
      gear_ratio |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |   .0597682   .0449408     1.33   0.188     -.030039    .1495753
        headroom |   -.008073   .0568557    -0.14   0.888    -.1216901     .105544
           trunk |    .000695   .0150864     0.05   0.963    -.0294528    .0308427
          length |  -.0100791   .0040969    -2.46   0.017     -.018266   -.0018922
            turn |  -.0215481   .0205791    -1.05   0.299    -.0626721     .019576
           _cons |   5.565596   .4777145    11.65   0.000      4.61096    6.520232
    ------------------------------------------------------------------------------
    command: reg gear rep78 headroom trunk weight turn, robust
    
    Linear regression                               Number of obs     =         69
                                                    F(5, 63)          =      22.05
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.6358
                                                    Root MSE          =      .2901
    
    ------------------------------------------------------------------------------
                 |               Robust
      gear_ratio |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |   .0500213   .0428636     1.17   0.248    -.0356347    .1356773
        headroom |  -.0082942   .0512855    -0.16   0.872    -.1107801    .0941918
           trunk |   .0023232   .0132319     0.18   0.861    -.0241185     .028765
          weight |  -.0004333   .0000918    -4.72   0.000    -.0006168   -.0002497
            turn |  -.0015245    .015639    -0.10   0.923    -.0327765    .0297275
           _cons |   4.195737   .5330257     7.87   0.000      3.13057    5.260903
    ------------------------------------------------------------------------------

    Comment

    Working...
    X