Announcement

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

  • #16
    Thanks. I have done that now:

    Code:
    . sort  quarter_date myWEEK // this sort is important 
    
    . 
    . 
    . 
    . gen SUBsample=.
    (1,846 missing values generated)
    
    . replace SUBsample=1 if quarter_date<tq(1999q4)  // a sample to fit the model
    (850 real changes made)
    
    . replace SUBsample=2  if SUBsample==.   // a sample to evaluate the model (out of sample)
    (996 real changes made)
    
    . 
    . 
    . gen prediction=.
    (1,846 missing values generated)
    
    . 
    . set seed 12345 // this is done for the results to be reproducible 
    
    . 
    . levelsof myWEEK, local(levels)
    1 2 3 4 5 6 7 8 9 10 11 12
    
    . 
    . foreach x of local levels {
      2.   
    . lasso linear quarterly_macro var1-var30 if SUBsample == 1 & myWEEK==`x'
      3. estimates store LASSOrev 
      4. predict temp if SUBsample==2 & myWEEK==`x', postselection
      5. replace prediction=temp  if myWEEK==`x'
      6. drop temp
      7. }
    note: var11 dropped because it is constant
    note: var22 dropped because it is constant
    note: var21 dropped because of collinearity with another variable
    note: var25 dropped because it is constant in C.V. subsamples
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .1299048   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .5129244
    Grid value 2:     lambda = .1183644   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .5233544
    Grid value 3:     lambda = .1078492   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .5364788
    Grid value 4:     lambda = .0982682   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .5501552
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         70
                                                No. of covariates =         26
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .1299048         0       0.0899     .5129244
           2 |    lambda after    .1183644         1      -0.1121     .5233544
           4 |     last lambda    .0982682         1      -0.1690     .5501552
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var25 dropped because it is constant
    note: var28 dropped because it is constant
    note: var3 dropped because it is constant in C.V. subsamples
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .1273055   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4883653
    Grid value 2:     lambda =  .115996   no. of nonzero coef. =       2
    Folds: 1...5....10   CVF = .491965
    Grid value 3:     lambda = .1056912   no. of nonzero coef. =       2
    Folds: 1...5....10   CVF = .4956619
    Grid value 4:     lambda = .0963019   no. of nonzero coef. =       2
    Folds: 1...5....10   CVF = .5002015
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         23
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .1273055         0       0.0488     .4883653
           2 |    lambda after     .115996         2      -0.0566      .491965
           4 |     last lambda    .0963019         2      -0.0743     .5002015
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var28 dropped because it is constant
    note: var25 dropped because it is constant in C.V. subsamples
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .1469631   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4840107
    Grid value 2:     lambda = .1339073   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4870012
    Grid value 3:     lambda = .1220114   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4900909
    Grid value 4:     lambda = .1111722   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .492393
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         23
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .1469631         0       0.0395     .4840107
           2 |    lambda after    .1339073         1      -0.0459     .4870012
           4 |     last lambda    .1111722         1      -0.0575      .492393
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda =  .079373   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .5158857
    Grid value 2:     lambda = .0723217   no. of nonzero coef. =       4
    Folds: 1...5....10   CVF = .52118
    Grid value 3:     lambda = .0658968   no. of nonzero coef. =       5
    Folds: 1...5....10   CVF = .5218032
    Grid value 4:     lambda = .0600427   no. of nonzero coef. =       5
    Folds: 1...5....10   CVF = .5206043
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         24
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda     .079373         0       0.1079     .5158857
           2 |    lambda after    .0723217         4      -0.1193       .52118
           4 |     last lambda    .0600427         5      -0.1181     .5206043
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .1115648   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4831735
    Grid value 2:     lambda = .1016537   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4852746
    Grid value 3:     lambda = .0926231   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4889328
    Grid value 4:     lambda = .0843947   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4938684
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         24
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .1115648         0       0.0377     .4831735
           2 |    lambda after    .1016537         1      -0.0422     .4852746
           4 |     last lambda    .0843947         1      -0.0607     .4938684
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda =  .116832   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4950638
    Grid value 2:     lambda =  .106453   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4987938
    Grid value 3:     lambda =  .096996   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .5031363
    Grid value 4:     lambda = .0883792   no. of nonzero coef. =       2
    Folds: 1...5....10   CVF = .5082863
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         24
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda     .116832         0       0.0632     .4950638
           2 |    lambda after     .106453         1      -0.0712     .4987938
           4 |     last lambda    .0883792         2      -0.0916     .5082863
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .0922869   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .5015781
    Grid value 2:     lambda = .0840883   no. of nonzero coef. =       3
    Folds: 1...5....10   CVF = .5078456
    Grid value 3:     lambda = .0766182   no. of nonzero coef. =       4
    Folds: 1...5....10   CVF = .5120959
    Grid value 4:     lambda = .0698116   no. of nonzero coef. =       4
    Folds: 1...5....10   CVF = .514441
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         24
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .0922869         0       0.0772     .5015781
           2 |    lambda after    .0840883         3      -0.0907     .5078456
           4 |     last lambda    .0698116         4      -0.1048      .514441
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .0978168   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4869643
    Grid value 2:     lambda = .0891271   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4915056
    Grid value 3:     lambda = .0812093   no. of nonzero coef. =       3
    Folds: 1...5....10   CVF = .4971674
    Grid value 4:     lambda = .0739949   no. of nonzero coef. =       3
    Folds: 1...5....10   CVF = .5038661
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         24
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .0978168         0       0.0458     .4869643
           2 |    lambda after    .0891271         1      -0.0556     .4915056
           4 |     last lambda    .0739949         3      -0.0821     .5038661
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .1263635   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4842843
    Grid value 2:     lambda = .1151377   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4873655
    Grid value 3:     lambda = .1049092   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4943139
    Grid value 4:     lambda = .0955894   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .5027705
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         70
                                                No. of covariates =         24
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .1263635         0       0.0260     .4842843
           2 |    lambda after    .1151377         1      -0.0325     .4873655
           4 |     last lambda    .0955894         1      -0.0651     .5027705
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .1331209   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4934726
    Grid value 2:     lambda = .1212948   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4949057
    Grid value 3:     lambda = .1105193   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .496512
    Grid value 4:     lambda = .1007011   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4985962
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         24
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .1331209         0       0.0598     .4934726
           2 |    lambda after    .1212948         1      -0.0629     .4949057
           4 |     last lambda    .1007011         1      -0.0708     .4985962
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var25 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .1335231   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4854478
    Grid value 2:     lambda = .1216613   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4852986
    Grid value 3:     lambda = .1108532   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4870591
    Grid value 4:     lambda = .1010053   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4895512
    Grid value 5:     lambda = .0920323   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4929817
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         23
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
           1 |    first lambda    .1335231         0       0.0426     .4854478
         * 2 | selected lambda    .1216613         1      -0.0422     .4852986
           3 |    lambda after    .1108532         1      -0.0460     .4870591
           5 |     last lambda    .0920323         1      -0.0587     .4929817
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    note: var3 dropped because it is constant
    note: var9 dropped because it is constant
    note: var11 dropped because it is constant
    note: var21 dropped because it is constant
    note: var22 dropped because it is constant
    note: var25 dropped because it is constant
    note: var28 dropped because it is constant
    10-fold cross-validation with 100 lambdas ...
    Grid value 1:     lambda = .1335061   no. of nonzero coef. =       0
    Folds: 1...5....10   CVF = .4863952
    Grid value 2:     lambda = .1216458   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .4886327
    Grid value 3:     lambda = .1108391   no. of nonzero coef. =       1
    Folds: 1...5....10   CVF = .49106
    Grid value 4:     lambda = .1009925   no. of nonzero coef. =       2
    Folds: 1...5....10   CVF = .4948401
    ... cross-validation complete ... minimum found
    
    Lasso linear model                          No. of obs        =         71
                                                No. of covariates =         23
    Selection: Cross-validation                 No. of CV folds   =         10
    
    --------------------------------------------------------------------------
             |                                No. of      Out-of-      CV mean
             |                               nonzero       sample   prediction
          ID |     Description      lambda     coef.    R-squared        error
    ---------+----------------------------------------------------------------
         * 1 | selected lambda    .1335061         0       0.0446     .4863952
           2 |    lambda after    .1216458         1      -0.0494     .4886327
           4 |     last lambda    .1009925         2      -0.0627     .4948401
    --------------------------------------------------------------------------
    * lambda selected by cross-validation.
    (option xb assumed; linear prediction with postselection coefficients)
    (83 real changes made)
    
    .
    end of do-file

    ​​​​​​. sort quarter_date myWEEK

    . br quarter_date myWEEK prediction

    . dataex quarter_date myWEEK prediction if prediction!=.

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(quarter_date myWEEK prediction)
    159  1  .1105143
    159  2 .10566197
    159  3 .10566197
    159  4 .10566197
    159  5 .10566197
    159  6 .10566197
    159  7 .10566197
    159  8 .10566197
    159  9 .10755715
    159 10 .10566197
    159 11 .05600106
    159 12 .10566197
    160  1  .1105143
    160  2 .10566197
    160  3 .10566197
    160  4 .10566197
    160  5 .10566197
    160  6 .10566197
    160  7 .10566197
    160  8 .10566197
    160  9 .10755715
    160 10 .10566197
    160 11 .05600106
    160 12 .10566197
    161  1  .1105143
    161  2 .10566197
    161  3 .10566197
    161  4 .10566197
    161  5 .10566197
    161  6 .10566197
    161  7 .10566197
    161  8 .10566197
    161  9 .10755715
    161 10 .10566197
    161 11 .05600106
    161 12 .10566197
    162  1  .1105143
    162  2 .10566197
    162  3 .10566197
    162  4 .10566197
    162  5 .10566197
    162  6 .10566197
    162  7 .10566197
    162  8 .10566197
    162  9 .10755715
    162 10 .10566197
    162 11 .05600106
    162 12 .10566197
    163  1  .1105143
    163  2 .10566197
    163  3 .10566197
    163  4 .10566197
    163  5 .10566197
    163  6 .10566197
    163  7 .10566197
    163  8 .10566197
    163  9 .10755715
    163 10 .10566197
    163 11 .05600106
    163 12 .10566197
    164  1  .1105143
    164  2 .10566197
    164  3 .10566197
    164  4 .10566197
    164  5 .10566197
    164  6 .10566197
    164  7 .10566197
    164  8 .10566197
    164  9 .10755715
    164 10 .10566197
    164 11 .05600106
    164 12 .10566197
    165  1  .1105143
    165  2 .10566197
    165  3 .10566197
    165  4 .10566197
    165  5 .10566197
    165  6 .10566197
    165  7 .10566197
    165  8 .10566197
    165  9 .10755715
    165 10 .10566197
    165 11 .05600106
    165 12 .10566197
    166  1  .1105143
    166  2 .10566197
    166  3 .10566197
    166  4 .10566197
    166  5 .10566197
    166  6 .10566197
    166  7 .10566197
    166  8 .10566197
    166  9 .10755715
    166 10 .10566197
    166 11 .05600106
    166 12 .10566197
    167  1  .1105143
    167  2 .10566197
    167  3 .10566197
    167  4 .10566197
    end
    format %tq quarter_date

    I hope this helps to identify what is wrong with the -lasso- use in the loop.

    I look forward to hearing your response. Thank you so much.

    Comment


    • #17
      I see a problem. If you look at the -lasso- output, focusing on the tables with the lambdas, you will see that in nearly all of the iterations, the selected result is the one with 0 variables selected. In other words, for all but a couple of the weeks the model selected is just the constant term--hence the lack of variation in the results. I think the ultimate problem is that your data are just not suitable for this approach. -lasso- works best when there are many predictors, most of which are destined to be left out of the model, but the few that enter the model are fairly strong. In your case, what you have are a bunch of variables that are all of essentially no predictive value. Even where there is a selected variable, the out-of-sample R squared is close to zero, actually negative. I notice that in many of the weeks, several of the variables are dropped because they are constant. And I wonder if the variables that remain are close to constant--that would explain their inability to explain outcome variance.

      Anyway, I don't have a deep understanding of -lasso- and what I have written above is the extent of my insight. But I do believe the essence of it is that you are just asking the data to do more than it is capable of. You need a data set where the variables have more predictive capacity in order to apply this method.

      If somebody following along has a better or different understanding of the situation, I hope they will chime in. I'd like to learn more about this, too.

      Comment


      • #18

        Thank you so much Clyde Schechter

        I want to also share the results when I run the same code but without -myWEEK=`x' in the same line when we run the lasso. In this case, I get sensible results, and the lasso selects several variables. Note that the R2 may not be high but the macro variable is in fact measured as errors or revisions which are very difficult to be predicted.
        Please have a look:

        Code:
        . sort  quarter_date myWEEK // this sort is important 
        
        . 
        . 
        . 
        . gen SUBsample=.
        (1,846 missing values generated)
        
        . replace SUBsample=1 if quarter_date<tq(1999q4)  // a sample to fit the model
        (850 real changes made)
        
        . replace SUBsample=2  if SUBsample==.   // a sample to evaluate the model (out of sample)
        (996 real changes made)
        
        . 
        . 
        . gen prediction=.
        (1,846 missing values generated)
        
        . 
        . set seed 12345 // this is done for the results to be reproducible 
        
        . 
        . levelsof myWEEK, local(levels)
        1 2 3 4 5 6 7 8 9 10 11 12
        
        . 
        . foreach x of local levels {
          2.   
        . lasso linear quarterly_macro var1-var30 if SUBsample == 1 
          3. estimates store LASSOrev 
          4. predict temp if SUBsample==2 & myWEEK==`x', penalized
          5. replace prediction=temp  if myWEEK==`x'
          6. drop temp
          7. }
        note: var11 dropped because it is constant
        note: var22 dropped because it is constant
        note: var21 dropped because it is constant in C.V. subsamples
        10-fold cross-validation with 100 lambdas ...
        Grid value 1:     lambda =  .075676   no. of nonzero coef. =       0
        Folds: 1...5....10   CVF = .4679962
        Grid value 2:     lambda = .0689532   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4680501
        Grid value 3:     lambda = .0628276   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4681945
        Grid value 4:     lambda = .0572461   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4682741
        Grid value 5:     lambda = .0521606   no. of nonzero coef. =       3
        Folds: 1...5....10   CVF = .4678852
        Grid value 6:     lambda = .0475268   no. of nonzero coef. =       4
        Folds: 1...5....10   CVF = .4670921
        Grid value 7:     lambda = .0433046   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .46651
        Grid value 8:     lambda = .0394576   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4660178
        Grid value 9:     lambda = .0359523   no. of nonzero coef. =       7
        Folds: 1...5....10   CVF = .4653309
        Grid value 10:    lambda = .0327584   no. of nonzero coef. =       8
        Folds: 1...5....10   CVF = .4642204
        Grid value 11:    lambda = .0298482   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4628241
        Grid value 12:    lambda = .0271966   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .461378
        Grid value 13:    lambda = .0247805   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4601129
        Grid value 14:    lambda = .0225791   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .45925
        Grid value 15:    lambda = .0205732   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4586102
        Grid value 16:    lambda = .0187455   no. of nonzero coef. =      12
        Folds: 1...5....10   CVF = .4581069
        Grid value 17:    lambda = .0170802   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4577602
        Grid value 18:    lambda = .0155629   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .457408
        Grid value 19:    lambda = .0141803   no. of nonzero coef. =      14
        Folds: 1...5....10   CVF = .4571716
        Grid value 20:    lambda = .0129206   no. of nonzero coef. =      15
        Folds: 1...5....10   CVF = .4569787
        Grid value 21:    lambda = .0117727   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4568816
        Grid value 22:    lambda = .0107269   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4572275
        Grid value 23:    lambda = .0097739   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4578764
        Grid value 24:    lambda = .0089056   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .458571
        Grid value 25:    lambda = .0081145   no. of nonzero coef. =      18
        Folds: 1...5....10   CVF = .4592524
        ... cross-validation complete ... minimum found
        
        Lasso linear model                          No. of obs        =        850
                                                    No. of covariates =         27
        Selection: Cross-validation                 No. of CV folds   =         10
        
        --------------------------------------------------------------------------
                 |                                No. of      Out-of-      CV mean
                 |                               nonzero       sample   prediction
              ID |     Description      lambda     coef.    R-squared        error
        ---------+----------------------------------------------------------------
               1 |    first lambda     .075676         0       0.0031     .4679962
              20 |   lambda before    .0129206        15       0.0205     .4569787
            * 21 | selected lambda    .0117727        17       0.0208     .4568816
              22 |    lambda after    .0107269        17       0.0200     .4572275
              25 |     last lambda    .0081145        18       0.0157     .4592524
        --------------------------------------------------------------------------
        * lambda selected by cross-validation.
        (option xb assumed; linear prediction with penalized coefficients)
        (83 real changes made)
        note: var11 dropped because it is constant
        note: var22 dropped because it is constant
        note: var21 dropped because it is constant in C.V. subsamples
        10-fold cross-validation with 100 lambdas ...
        Grid value 1:     lambda =  .075676   no. of nonzero coef. =       0
        Folds: 1...5....10   CVF = .4675557
        Grid value 2:     lambda = .0689532   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4673491
        Grid value 3:     lambda = .0628276   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4674566
        Grid value 4:     lambda = .0572461   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4674734
        Grid value 5:     lambda = .0521606   no. of nonzero coef. =       3
        Folds: 1...5....10   CVF = .4671639
        Grid value 6:     lambda = .0475268   no. of nonzero coef. =       4
        Folds: 1...5....10   CVF = .466731
        Grid value 7:     lambda = .0433046   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4662746
        Grid value 8:     lambda = .0394576   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4659209
        Grid value 9:     lambda = .0359523   no. of nonzero coef. =       7
        Folds: 1...5....10   CVF = .4652793
        Grid value 10:    lambda = .0327584   no. of nonzero coef. =       8
        Folds: 1...5....10   CVF = .4641166
        Grid value 11:    lambda = .0298482   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .462926
        Grid value 12:    lambda = .0271966   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4616517
        Grid value 13:    lambda = .0247805   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4606399
        Grid value 14:    lambda = .0225791   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4600805
        Grid value 15:    lambda = .0205732   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4599324
        Grid value 16:    lambda = .0187455   no. of nonzero coef. =      12
        Folds: 1...5....10   CVF = .4600541
        Grid value 17:    lambda = .0170802   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4601551
        Grid value 18:    lambda = .0155629   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4603565
        Grid value 19:    lambda = .0141803   no. of nonzero coef. =      14
        Folds: 1...5....10   CVF = .4605374
        Grid value 20:    lambda = .0129206   no. of nonzero coef. =      15
        Folds: 1...5....10   CVF = .460739
        Grid value 21:    lambda = .0117727   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4613779
        ... cross-validation complete ... minimum found
        
        Lasso linear model                          No. of obs        =        850
                                                    No. of covariates =         27
        Selection: Cross-validation                 No. of CV folds   =         10
        
        --------------------------------------------------------------------------
                 |                                No. of      Out-of-      CV mean
                 |                               nonzero       sample   prediction
              ID |     Description      lambda     coef.    R-squared        error
        ---------+----------------------------------------------------------------
               1 |    first lambda     .075676         0       0.0021     .4675557
              14 |   lambda before    .0225791        11       0.0139     .4600805
            * 15 | selected lambda    .0205732        11       0.0142     .4599324
              16 |    lambda after    .0187455        12       0.0140     .4600541
              21 |     last lambda    .0117727        17       0.0111     .4613779
        --------------------------------------------------------------------------
        * lambda selected by cross-validation.
        (option xb assumed; linear prediction with penalized coefficients)
        (83 real changes made)
        note: var11 dropped because it is constant
        note: var22 dropped because it is constant
        note: var21 dropped because it is constant in C.V. subsamples
        note: var28 dropped because it is constant in C.V. subsamples
        10-fold cross-validation with 100 lambdas ...
        Grid value 1:     lambda =  .075676   no. of nonzero coef. =       0
        Folds: 1...5....10   CVF = .4671626
        Grid value 2:     lambda = .0689532   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4671274
        Grid value 3:     lambda = .0628276   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4668262
        Grid value 4:     lambda = .0572461   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4662748
        Grid value 5:     lambda = .0521606   no. of nonzero coef. =       3
        Folds: 1...5....10   CVF = .4655569
        Grid value 6:     lambda = .0475268   no. of nonzero coef. =       4
        Folds: 1...5....10   CVF = .4642978
        Grid value 7:     lambda = .0433046   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .462946
        Grid value 8:     lambda = .0394576   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .461657
        Grid value 9:     lambda = .0359523   no. of nonzero coef. =       7
        Folds: 1...5....10   CVF = .460368
        Grid value 10:    lambda = .0327584   no. of nonzero coef. =       8
        Folds: 1...5....10   CVF = .4589583
        Grid value 11:    lambda = .0298482   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4573456
        Grid value 12:    lambda = .0271966   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4560136
        Grid value 13:    lambda = .0247805   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4550349
        Grid value 14:    lambda = .0225791   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4543975
        Grid value 15:    lambda = .0205732   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4541725
        Grid value 16:    lambda = .0187455   no. of nonzero coef. =      12
        Folds: 1...5....10   CVF = .4538495
        Grid value 17:    lambda = .0170802   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4537191
        Grid value 18:    lambda = .0155629   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4537747
        Grid value 19:    lambda = .0141803   no. of nonzero coef. =      14
        Folds: 1...5....10   CVF = .4538085
        Grid value 20:    lambda = .0129206   no. of nonzero coef. =      15
        Folds: 1...5....10   CVF = .4537054
        Grid value 21:    lambda = .0117727   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4539599
        Grid value 22:    lambda = .0107269   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .454397
        Grid value 23:    lambda = .0097739   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4549359
        Grid value 24:    lambda = .0089056   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4555179
        ... cross-validation complete ... minimum found
        
        Lasso linear model                          No. of obs        =        850
                                                    No. of covariates =         26
        Selection: Cross-validation                 No. of CV folds   =         10
        
        --------------------------------------------------------------------------
                 |                                No. of      Out-of-      CV mean
                 |                               nonzero       sample   prediction
              ID |     Description      lambda     coef.    R-squared        error
        ---------+----------------------------------------------------------------
               1 |    first lambda     .075676         0       0.0013     .4671626
              19 |   lambda before    .0141803        14       0.0273     .4538085
            * 20 | selected lambda    .0129206        15       0.0276     .4537054
              21 |    lambda after    .0117727        17       0.0270     .4539599
              24 |     last lambda    .0089056        17       0.0237     .4555179
        --------------------------------------------------------------------------
        * lambda selected by cross-validation.
        (option xb assumed; linear prediction with penalized coefficients)
        (83 real changes made)
        note: var11 dropped because it is constant
        note: var22 dropped because it is constant
        note: var21 dropped because it is constant in C.V. subsamples
        10-fold cross-validation with 100 lambdas ...
        Grid value 1:     lambda =  .075676   no. of nonzero coef. =       0
        Folds: 1...5....10   CVF = .4678678
        Grid value 2:     lambda = .0689532   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4672716
        Grid value 3:     lambda = .0628276   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4667401
        Grid value 4:     lambda = .0572461   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4663807
        Grid value 5:     lambda = .0521606   no. of nonzero coef. =       3
        Folds: 1...5....10   CVF = .4657634
        Grid value 6:     lambda = .0475268   no. of nonzero coef. =       4
        Folds: 1...5....10   CVF = .4648776
        Grid value 7:     lambda = .0433046   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .464134
        Grid value 8:     lambda = .0394576   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4631294
        Grid value 9:     lambda = .0359523   no. of nonzero coef. =       7
        Folds: 1...5....10   CVF = .4618727
        Grid value 10:    lambda = .0327584   no. of nonzero coef. =       8
        Folds: 1...5....10   CVF = .4603564
        Grid value 11:    lambda = .0298482   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4588729
        Grid value 12:    lambda = .0271966   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4575057
        Grid value 13:    lambda = .0247805   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4564676
        Grid value 14:    lambda = .0225791   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4555316
        Grid value 15:    lambda = .0205732   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4550338
        Grid value 16:    lambda = .0187455   no. of nonzero coef. =      12
        Folds: 1...5....10   CVF = .4547335
        Grid value 17:    lambda = .0170802   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4544967
        Grid value 18:    lambda = .0155629   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4543318
        Grid value 19:    lambda = .0141803   no. of nonzero coef. =      14
        Folds: 1...5....10   CVF = .4542397
        Grid value 20:    lambda = .0129206   no. of nonzero coef. =      15
        Folds: 1...5....10   CVF = .454405
        Grid value 21:    lambda = .0117727   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .454844
        Grid value 22:    lambda = .0107269   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4556178
        Grid value 23:    lambda = .0097739   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4568668
        ... cross-validation complete ... minimum found
        
        Lasso linear model                          No. of obs        =        850
                                                    No. of covariates =         27
        Selection: Cross-validation                 No. of CV folds   =         10
        
        --------------------------------------------------------------------------
                 |                                No. of      Out-of-      CV mean
                 |                               nonzero       sample   prediction
              ID |     Description      lambda     coef.    R-squared        error
        ---------+----------------------------------------------------------------
               1 |    first lambda     .075676         0       0.0028     .4678678
              18 |   lambda before    .0155629        13       0.0262     .4543318
            * 19 | selected lambda    .0141803        14       0.0264     .4542397
              20 |    lambda after    .0129206        15       0.0261      .454405
              23 |     last lambda    .0097739        17       0.0208     .4568668
        --------------------------------------------------------------------------
        * lambda selected by cross-validation.
        (option xb assumed; linear prediction with penalized coefficients)
        (83 real changes made)
        note: var11 dropped because it is constant
        note: var22 dropped because it is constant
        note: var21 dropped because it is constant in C.V. subsamples
        10-fold cross-validation with 100 lambdas ...
        Grid value 1:     lambda =  .075676   no. of nonzero coef. =       0
        Folds: 1...5....10   CVF = .4681504
        Grid value 2:     lambda = .0689532   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4678956
        Grid value 3:     lambda = .0628276   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4674176
        Grid value 4:     lambda = .0572461   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4670662
        Grid value 5:     lambda = .0521606   no. of nonzero coef. =       3
        Folds: 1...5....10   CVF = .4662588
        Grid value 6:     lambda = .0475268   no. of nonzero coef. =       4
        Folds: 1...5....10   CVF = .4653953
        Grid value 7:     lambda = .0433046   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4645442
        Grid value 8:     lambda = .0394576   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4636811
        Grid value 9:     lambda = .0359523   no. of nonzero coef. =       7
        Folds: 1...5....10   CVF = .4624568
        Grid value 10:    lambda = .0327584   no. of nonzero coef. =       8
        Folds: 1...5....10   CVF = .4612471
        Grid value 11:    lambda = .0298482   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4598757
        Grid value 12:    lambda = .0271966   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4588472
        Grid value 13:    lambda = .0247805   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4581462
        Grid value 14:    lambda = .0225791   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4578649
        Grid value 15:    lambda = .0205732   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .457848
        Grid value 16:    lambda = .0187455   no. of nonzero coef. =      12
        Folds: 1...5....10   CVF = .457929
        Grid value 17:    lambda = .0170802   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4581118
        Grid value 18:    lambda = .0155629   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4586234
        Grid value 19:    lambda = .0141803   no. of nonzero coef. =      14
        Folds: 1...5....10   CVF = .4590759
        Grid value 20:    lambda = .0129206   no. of nonzero coef. =      15
        Folds: 1...5....10   CVF = .4593571
        ... cross-validation complete ... minimum found
        
        Lasso linear model                          No. of obs        =        850
                                                    No. of covariates =         27
        Selection: Cross-validation                 No. of CV folds   =         10
        
        --------------------------------------------------------------------------
                 |                                No. of      Out-of-      CV mean
                 |                               nonzero       sample   prediction
              ID |     Description      lambda     coef.    R-squared        error
        ---------+----------------------------------------------------------------
               1 |    first lambda     .075676         0       0.0034     .4681504
              14 |   lambda before    .0225791        11       0.0186     .4578649
            * 15 | selected lambda    .0205732        11       0.0187      .457848
              16 |    lambda after    .0187455        12       0.0185      .457929
              20 |     last lambda    .0129206        15       0.0154     .4593571
        --------------------------------------------------------------------------
        * lambda selected by cross-validation.
        (option xb assumed; linear prediction with penalized coefficients)
        (83 real changes made)
        note: var11 dropped because it is constant
        note: var22 dropped because it is constant
        note: var21 dropped because it is constant in C.V. subsamples
        10-fold cross-validation with 100 lambdas ...
        Grid value 1:     lambda =  .075676   no. of nonzero coef. =       0
        Folds: 1...5....10   CVF = .4687719
        Grid value 2:     lambda = .0689532   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4686223
        Grid value 3:     lambda = .0628276   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .468399
        Grid value 4:     lambda = .0572461   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4682402
        Grid value 5:     lambda = .0521606   no. of nonzero coef. =       3
        Folds: 1...5....10   CVF = .4677396
        Grid value 6:     lambda = .0475268   no. of nonzero coef. =       4
        Folds: 1...5....10   CVF = .4668274
        Grid value 7:     lambda = .0433046   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4657136
        Grid value 8:     lambda = .0394576   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4647239
        Grid value 9:     lambda = .0359523   no. of nonzero coef. =       7
        Folds: 1...5....10   CVF = .4635661
        Grid value 10:    lambda = .0327584   no. of nonzero coef. =       8
        Folds: 1...5....10   CVF = .4616842
        Grid value 11:    lambda = .0298482   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4601508
        Grid value 12:    lambda = .0271966   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4590093
        Grid value 13:    lambda = .0247805   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4581979
        Grid value 14:    lambda = .0225791   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4573072
        Grid value 15:    lambda = .0205732   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4567259
        Grid value 16:    lambda = .0187455   no. of nonzero coef. =      12
        Folds: 1...5....10   CVF = .4560611
        Grid value 17:    lambda = .0170802   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4556289
        Grid value 18:    lambda = .0155629   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4553643
        Grid value 19:    lambda = .0141803   no. of nonzero coef. =      14
        Folds: 1...5....10   CVF = .4552192
        Grid value 20:    lambda = .0129206   no. of nonzero coef. =      15
        Folds: 1...5....10   CVF = .4550419
        Grid value 21:    lambda = .0117727   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4549346
        Grid value 22:    lambda = .0107269   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4549962
        Grid value 23:    lambda = .0097739   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4554479
        Grid value 24:    lambda = .0089056   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4562525
        Grid value 25:    lambda = .0081145   no. of nonzero coef. =      18
        Folds: 1...5....10   CVF = .4571315
        ... cross-validation complete ... minimum found
        
        Lasso linear model                          No. of obs        =        850
                                                    No. of covariates =         27
        Selection: Cross-validation                 No. of CV folds   =         10
        
        --------------------------------------------------------------------------
                 |                                No. of      Out-of-      CV mean
                 |                               nonzero       sample   prediction
              ID |     Description      lambda     coef.    R-squared        error
        ---------+----------------------------------------------------------------
               1 |    first lambda     .075676         0       0.0047     .4687719
              20 |   lambda before    .0129206        15       0.0247     .4550419
            * 21 | selected lambda    .0117727        17       0.0249     .4549346
              22 |    lambda after    .0107269        17       0.0248     .4549962
              25 |     last lambda    .0081145        18       0.0202     .4571315
        --------------------------------------------------------------------------
        * lambda selected by cross-validation.
        (option xb assumed; linear prediction with penalized coefficients)
        (83 real changes made)
        note: var11 dropped because it is constant
        note: var22 dropped because it is constant
        note: var21 dropped because it is constant in C.V. subsamples
        10-fold cross-validation with 100 lambdas ...
        Grid value 1:     lambda =  .075676   no. of nonzero coef. =       0
        Folds: 1...5....10   CVF = .4683256
        Grid value 2:     lambda = .0689532   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4684147
        Grid value 3:     lambda = .0628276   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .468563
        Grid value 4:     lambda = .0572461   no. of nonzero coef. =       1
        Folds: 1...5....10   CVF = .4687462
        Grid value 5:     lambda = .0521606   no. of nonzero coef. =       3
        Folds: 1...5....10   CVF = .4687869
        Grid value 6:     lambda = .0475268   no. of nonzero coef. =       4
        Folds: 1...5....10   CVF = .4688084
        Grid value 7:     lambda = .0433046   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .468757
        Grid value 8:     lambda = .0394576   no. of nonzero coef. =       5
        Folds: 1...5....10   CVF = .4686406
        Grid value 9:     lambda = .0359523   no. of nonzero coef. =       7
        Folds: 1...5....10   CVF = .4681354
        Grid value 10:    lambda = .0327584   no. of nonzero coef. =       8
        Folds: 1...5....10   CVF = .4674565
        Grid value 11:    lambda = .0298482   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4664486
        Grid value 12:    lambda = .0271966   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4655293
        Grid value 13:    lambda = .0247805   no. of nonzero coef. =       9
        Folds: 1...5....10   CVF = .4646018
        Grid value 14:    lambda = .0225791   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4642576
        Grid value 15:    lambda = .0205732   no. of nonzero coef. =      11
        Folds: 1...5....10   CVF = .4643387
        Grid value 16:    lambda = .0187455   no. of nonzero coef. =      12
        Folds: 1...5....10   CVF = .4644699
        Grid value 17:    lambda = .0170802   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4642504
        Grid value 18:    lambda = .0155629   no. of nonzero coef. =      13
        Folds: 1...5....10   CVF = .4640877
        Grid value 19:    lambda = .0141803   no. of nonzero coef. =      14
        Folds: 1...5....10   CVF = .4640245
        Grid value 20:    lambda = .0129206   no. of nonzero coef. =      15
        Folds: 1...5....10   CVF = .4642232
        Grid value 21:    lambda = .0117727   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4649463
        Grid value 22:    lambda = .0107269   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4660712
        Grid value 23:    lambda = .0097739   no. of nonzero coef. =      17
        Folds: 1...5....10   CVF = .4673186
        ... cross-validation complete ... minimum found
        
        Lasso linear model                          No. of obs        =        850
                                                    No. of covariates =         27
        Selection: Cross-validation                 No. of CV folds   =         10
        
        --------------------------------------------------------------------------
                 |                                No. of      Out-of-      CV mean
                 |                               nonzero       sample   prediction
              ID |     Description      lambda     coef.    R-squared        error
        ---------+----------------------------------------------------------------
               1 |    first lambda     .075676         0       0.0038     .4683256
              18 |   lambda before    .0155629        13       0.0053     .4640877
            * 19 | selected lambda    .0141803        14       0.0054     .4640245
              20 |    lambda after    .0129206        15       0.0050     .4642232
              23 |     last lambda    .0097739        17      -0.0016     .4673186
        --------------------------------------------------------------------------
        * lambda selected by cross-validation.
        (option xb assumed; linear prediction with penalized coefficients)
        (83 real changes made)
        note: var11 dropped because it is constant
        note: var22 dropped because it is constant
        note: var21 dropped because it is constant in C.V. subsamples
        note: var28 dropped because it is constant in C.V. subsamples
        10-fold cross-validation with 100 lambdas ...
        
        
        Lasso linear model                          No. of obs        =        850
                                                    No. of covariates =         26
        Selection: Cross-validation                 No. of CV folds   =         10
        
        --------------------------------------------------------------------------
                 |                                No. of      Out-of-      CV mean
                 |                               nonzero       sample   prediction
              ID |     Description      lambda     coef.    R-squared        error
        ---------+----------------------------------------------------------------
               1 |    first lambda     .075676         0       0.0025     .4677543
              20 |   lambda before    .0129206        15       0.0252     .4548254
            * 21 | selected lambda    .0117727        17       0.0258     .4545095
              22 |    lambda after    .0107269        17       0.0258      .454531
              25 |     last lambda    .0081145        18       0.0218     .4563726
        --------------------------------------------------------------------------
        
        
        
        . 
        end of do-file
        
        .
        As you see here from the output, it seems that the -lasso- is being estimated for each week although we have not added this myWEEK=`x` condition in the lasso code line. If Stata estimates the model for the entire SUBsample=1 and not as per each week in SUBsample=1, shouldn't the output be for only one regression?

        NOTE: I had to remove the last few tables in the results only because the forum does not allow me to send all output (Maximum is 30000).

        Comment


        • #19
          As you see here from the output, it seems that the -lasso- is being estimated for each week although we have not added this myWEEK=`x` condition in the lasso code line.
          No, that is not what is happening. You can see that because if you look closely at the "Lasso linear model" output tables, you will see that the sample size is 850 in each case. (By contrast, when you ran it with -if myWEEK==`x'-, the sample size was variable but always in the low 70's.) The reason the results vary by week, even though -lasso- is working with the same sample (i.e. all of SUBsample 1) in every week, is that -lasso- is inherently non-deterministic. If you were to re-do this, setting the random number seed at the top of the loop, rather than outside the loop, then you would see identical results for all weeks.

          The reason the results are more sensible when you don't restrict the sample to a single week is that you are giving -lasso- more data to work with. With more data, there is more information, both in terms of variation in the predictor variables, and more evidence about how those variables relate to the outcome. Consequently the lasso is able to identify more predictors, and those predictors are actually numerous (note that the selected models have somewhere between 10 and 20 variables now).

          Comment


          • #20
            Thanks.
            If you were to re-do this, setting the random number seed at the top of the loop, rather than outside the loop, then you would see identical results for all weeks.
            I assume that you mean the following:
            Code:
            gen prediction=.
            
            levelsof myWEEK, local(levels)
            
            foreach x of local levels {
            set seed 12345 
            lasso linear quarterly_macro var1-var30 if SUBsample == 1 
            estimates store LASSOrev 
            predict temp if SUBsample==2 & myWEEK==`x', penalized
            replace prediction=temp  if myWEEK==`x'
            drop temp
            }
            If so, I have tried it and I think the output is identical as you expected. I think this adjustment to the seed number is more sensible?!


            The reason the results are more sensible when you don't restrict the sample to a single week is that you are giving -lasso- more data to work with.
            If I do not restrict the model to be run on a weekly basis, this means that I run a lasso regression using all week data. Note that each quarterly_macro observation will then have 12 observations for each variable in each quarter.
            Is there any statistical reason to invalidate the results in this case?




            Comment


            • #21
              Well, if you want to use the same lasso results throughout, rather than re-setting the random number seed and re-running lasso, just take the -lasso- outside the loop. Do it once only. Then loop just the -predictions-. (The reason I suggested putting the -set seed- command at the top of the loop in #19 was just to demonstrate my point about what is happening, it was not intended as something to use for production code, though I did not make that clear.)

              If I do not restrict the model to be run on a weekly basis, this means that I run a lasso regression using all week data. Note that each quarterly_macro observation will then have 12 observations for each variable in each quarter.
              Is there any statistical reason to invalidate the results in this case?
              Yes, that is a correct description of what would happen if you take that approach. There is nothing that would "invalidate" the results from a general statistical perspective. The issue is whether those results are an appropriate answer to your research question, or whether they are an answer to the wrong question. So you have to think about it in those terms. What is it you are trying to accomplish? Can it be done in this way or not? Based on the names of your variables, I am guessing that you are working in the general domain of finance or economics. If that's true, even if you were to explain your research question here, I probably would not be able to figure this out for you, as I have no expertise in those domains. If you need help figuring out this issue, you might be best off consulting a colleague in your domain.

              Comment

              Working...
              X