Thanks. I have done that now:
. sort quarter_date myWEEK
. br quarter_date myWEEK prediction
. dataex quarter_date myWEEK prediction if prediction!=.
----------------------- copy starting from the next line -----------------------
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.
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