-------------
      name:  <unnamed>
       log:  C:\Users\ecomes\Documents\GitHub\lassopack\lassopack_v141incomplete\cs_rlasso.smcl
  log type:  smcl
 opened on:  13 Sep 2020, 00:12:29
r; t=0.02 0:12:29

. about

Stata/SE 16.1 for Windows (64-bit x86-64)
Revision 30 Jun 2020
Copyright 1985-2019 StataCorp LLC

Total physical memory:       16.00 GB
Available physical memory:    6.89 GB

Stata license: 5-user network perpetual
Serial number: 401606228165
  Licensed to: Mark Schaffer
               Heriot-Watt University
r; t=0.00 0:12:29

. which rlasso
.\rlasso.ado
*! rlasso 1.0.11 29jul2020
*! lassopack package 1.4.0
*! authors aa/cbh/ms
r; t=0.00 0:12:29

. which lassoutils
.\lassoutils.ado
*! lassoutils 1.2.03 xxxsept2020
*! lassopack package 1.4.1
*! authors aa/cbh/ms
r; t=0.01 0:12:29

. // original code by CBH, modified to use quad precision etc.; not needed.
. cap noi which lassoShootingCBH
command lassoShootingCBH not found as either built-in or ado-file
r; t=0.00 0:12:29

. cap noi which lassoClusterCBH
command lassoClusterCBH not found as either built-in or ado-file
r; t=0.00 0:12:29

.
. // Initial checks of rlasso are vs. CBH code.
. // Other checks include equivalences that should hold in theory:
. // 1. Partialling-out vs. unpenalized regressors.
. // 2. Standardization "on the fly" (default) vs. pre-standardization of data.
. // 3. Het-robust loadings vs. cluster-robust loadings with singleton clusters.
. // 4. Fixed effects vs. unpenalized dummies.
.
. // Currently uses Kiel-McClain 1995 housing/incinerator example dataset.
. // Includes some badly-scaled variables so useful.
. qui bcuse kielmc, clear
r; t=0.62 0:12:30

. gen lcbd=ln(cbd)
r; t=0.00 0:12:30

. gen lcbdsq=lcbd^2
r; t=0.00 0:12:30

. gen byte one=1
r; t=0.00 0:12:30

. gen n=_n
r; t=0.00 0:12:30

. gen id=ceil(_n/10)
r; t=0.00 0:12:30

.
.
. ******************************************************************************
. // Check vs. CBH code
. // Note that as of lassoutils 1.1.01 08nov2018 we need to set the c0 option.
.
. // homoskedastic
. /*
> lassoShootingCBH lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,     ///
>         het(0) verb(0) tolzero(1e-10) tolups(1e-10) ltol(1e-8) maxiter(10000)
> mat CBHbetaL=r(betaL)'
> mat CBHbetaPL=r(betaPL)'
> scalar lambda=r(lambda)
> local s = colsof(CBHbetaL)
> */
. mat CBHbetaL    = .55033459 , .0958016  , .00259465
r; t=0.00 0:12:30

. mat CBHbetaPL   = .65128713 , .15561146 , .01297576
r; t=0.00 0:12:30

. scalar lambda   = 144.22858784
r; t=0.00 0:12:30

. local s = 3
r; t=0.00 0:12:30

. // Basic
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         lalt corrnum(0) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)               ///
>         maxiter(10000) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5503346      0.6512871
           lintst |       0.0958016      0.1556115
         y81ldist |       0.0025946      0.0129758
            _cons |*       6.1608410      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.42 0:12:30

. mat b=e(b)
r; t=0.01 0:12:30

. mat beta=e(beta)
r; t=0.00 0:12:30

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:30

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:30

. mat beta=beta[1,1..`s']
r; t=0.00 0:12:30

. mat betaOLS=betaOLS[1,1..`s']
r; t=0.00 0:12:30

. assert mreldif(beta,CBHbetaL)<1e-8
r; t=0.00 0:12:30

. assert mreldif(betaOLS,CBHbetaPL)<1e-8
r; t=0.00 0:12:30

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:30

. // Partial-out constant
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         lalt corrnum(0) partial(_cons) tolzero(1e-10) tolpsi(1e-10)             ///
>         tolopt(1e-8) maxiter(10000) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5503346      0.6512871
           lintst |       0.0958016      0.1556115
         y81ldist |       0.0025946      0.0129758
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.1608410      4.7817721
------------------+--------------------------------
r; t=1.40 0:12:31

. mat b=e(b)
r; t=0.00 0:12:31

. mat beta=e(beta)
r; t=0.00 0:12:31

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:31

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:31

. mat beta=beta[1,1..`s']
r; t=0.00 0:12:31

. mat betaOLS=betaOLS[1,1..`s']
r; t=0.00 0:12:31

. assert mreldif(beta,CBHbetaL)<1e-8
r; t=0.00 0:12:31

. assert mreldif(betaOLS,CBHbetaPL)<1e-8
r; t=0.00 0:12:31

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:31

. // Include unpenalized constant by hand
. // Needs high setting for maxpsiiter
. // Also needs looser tolerance
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc one,   ///
>         lalt corrnum(0) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)                       ///
>         maxiter(10000) nocons pnotpen(one) maxpsiiter(10) c0(0.55) psolver(chol)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5503372      0.6512871
           lintst |       0.0958022      0.1556115
         y81ldist |       0.0025946      0.0129758
              one |*       6.1608162      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.33 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. mat beta=beta[1,1..`s']
r; t=0.00 0:12:32

. mat betaOLS=betaOLS[1,1..`s']
r; t=0.00 0:12:32

. assert mreldif(beta,CBHbetaL)<1e-5                      // looser tolerance
r; t=0.00 0:12:32

. assert mreldif(betaOLS,CBHbetaPL)<1e-8
r; t=0.00 0:12:32

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:32

.
. ******************************************************************************
. // Check vs. CBH code
.
. // With controls/pnotpen - note that controls do not appear in CBH X list
. // Needs high setting for maxpsiiter
.
. // Needs looser tolerance
. /*
> lassoShootingCBH lrprice intst lintst y81ldist lintstsq y81nrinc,       ///
>         het(0) verb(0) tolzero(1e-10) tolups(1e-10) ltol(1e-8) maxiter(10000) controls(larea cbd)
> mat CBHbetaL=r(betaL)'
> mat CBHbetaPL=r(betaPL)'
> scalar lambda=r(lambda)
> local s = colsof(CBHbetaL)
> */
. mat CBHbetaL    = .04572585 , .00175637
r; t=0.00 0:12:32

. mat CBHbetaPL   = .20415454 , .01272592
r; t=0.00 0:12:32

. scalar lambda   = 140.55736231
r; t=0.00 0:12:32

. local s = 2
r; t=0.00 0:12:32

. // pnotpen
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         lalt corrnum(0) partial(_cons) tolzero(1e-10) tolpsi(1e-10)             ///
>         tolopt(1e-8) maxiter(10000) maxpsiiter(10) pnotpen(larea cbd)   ///
>         c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.7112231      0.6458100
              cbd |*       0.0000069     -0.0000046
           lintst |       0.0457258      0.2041545
         y81ldist |       0.0017564      0.0127259
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       5.3072922      4.4373393
------------------+--------------------------------
*Not penalized
r; t=0.04 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. // trim
. mat beta=beta[1,3..4]
r; t=0.00 0:12:32

. mat betaOLS=betaOLS[1,3..4]
r; t=0.00 0:12:32

. local s = colsof(CBHbetaL)
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-6
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-6
  4. }
r; t=0.00 0:12:32

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:32

. // partial
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         lalt corrnum(0) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)               ///
>         maxiter(10000) maxpsiiter(10) partial(larea cbd)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
           lintst |       0.0457259      0.2041545
         y81ldist |       0.0017564      0.0127259
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.7112231      0.6458100
              cbd |       0.0000069     -0.0000046
            _cons |       5.3072922      4.4373393
------------------+--------------------------------
r; t=0.03 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. // trim
. mat beta=beta[1,1..2]
r; t=0.00 0:12:32

. mat betaOLS=betaOLS[1,1..2]
r; t=0.00 0:12:32

. local s = colsof(CBHbetaL)
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:32

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:32

.
. ******************************************************************************
. // Check vs CBH code
.
. // heteroskedastic
. /*
> lassoShootingCBH lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,     ///
>         het(1) verb(0) tolzero(1e-10) tolups(1e-10) ltol(1e-8) maxiter(10000)
> mat CBHbetaL=r(betaL)'
> mat CBHbetaPL=r(betaPL)'
> scalar lambda=r(lambda)
> local s = colsof(CBHbetaL)
> */
. mat CBHbetaL    = .52823835 , .09894578 , .0030307
r; t=0.00 0:12:32

. mat CBHbetaPL   = .65128713 , .15561146 , .01297576
r; t=0.00 0:12:32

. scalar lambda   = 144.22858784
r; t=0.00 0:12:32

. local s = 3
r; t=0.00 0:12:32

. // Basic
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         rob lalt corrnum(0) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)   ///
>         maxiter(10000) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5282384      0.6512871
           lintst |       0.0989458      0.1556115
         y81ldist |       0.0030307      0.0129758
            _cons |*       6.2970092      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:32

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:32

. // partial-out cons
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         rob lalt corrnum(0) partial(_cons) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5282384      0.6512871
           lintst |       0.0989458      0.1556115
         y81ldist |       0.0030307      0.0129758
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.2970092      4.7817721
------------------+--------------------------------
r; t=0.03 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. local s = colsof(CBHbetaL)
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:32

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:32

.
. // With controls/pnotpen - note that controls do not appear in CBH X list
. /*
> lassoShootingCBH lrprice intst lintst y81ldist lintstsq y81nrinc,       ///
>         het(1) verb(0) tolzero(1e-10) tolups(1e-10) ltol(1e-8) maxiter(10000) controls(larea cbd)
> mat CBHbetaL=r(betaL)'
> mat CBHbetaPL=r(betaPL)'
> scalar lambda=r(lambda)
> */
. mat CBHbetaL    = .01229867 , .00161341
r; t=0.00 0:12:32

. mat CBHbetaPL   = .20415454 , .01272592
r; t=0.00 0:12:32

. scalar lambda   = 140.55736231
r; t=0.00 0:12:32

. local s = 2
r; t=0.00 0:12:32

. // pnotpen
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         rob lalt corrnum(0) partial(_cons) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) pnotpen(larea cbd) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.7193864      0.6458100
              cbd |*       0.0000095     -0.0000046
           lintst |       0.0122987      0.2041545
         y81ldist |       0.0016134      0.0127259
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       5.5227424      4.4373393
------------------+--------------------------------
*Not penalized
r; t=0.03 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. // trim
. mat beta=beta[1,3..4]
r; t=0.00 0:12:32

. mat betaOLS=betaOLS[1,3..4]
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-6
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-6
  4. }
r; t=0.00 0:12:32

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:32

. // partial
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         rob lalt corrnum(0) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) partial(larea cbd) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
           lintst |       0.0122987      0.2041545
         y81ldist |       0.0016134      0.0127259
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.7193863      0.6458100
              cbd |       0.0000095     -0.0000046
            _cons |       5.5227424      4.4373393
------------------+--------------------------------
r; t=0.03 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. // trim
. mat beta=beta[1,1..2]
r; t=0.00 0:12:32

. mat betaOLS=betaOLS[1,1..2]
r; t=0.00 0:12:32

. local s = colsof(CBHbetaL)
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:32

. assert reldif(lambda,e(lambda0))<1e-8
r; t=0.00 0:12:32

.
. ********************** CLUSTER *********************************
. // Check vs CBH code
. // Update 4 Apr 2018:
. //   CBH cluster code lambda = 2.2*sqrt(nclust)*invnorm(1-(.1/log(nclust))/(2p))
. //   JBES paper and updated rlasso use 2,2*sqrt(n)*invnorm,
. //   i.e., same as standard lasso. Comparisons with CBH lambda commented out.
.
. // clustering (note no lalt required)
. // note center and maxupsiter are required to match CBH code
. /*
> lassoClusterCBH lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,      ///
>         cluster(age) verb(0) tolzero(1e-10) tolups(1e-10) ltol(1e-8) maxiter(10000)
> mat CBHbetaL=r(betaL)'
> mat CBHbetaPL=r(betaPL)'
> scalar lambda=r(lambda)
> */
. mat CBHbetaL    = .46261782 , .11510062
r; t=0.00 0:12:32

. mat CBHbetaPL   = .68488126 , .15090246
r; t=0.00 0:12:32

. scalar lambda   = 50.21173664
r; t=0.00 0:12:32

. local s = 2
r; t=0.00 0:12:32

. // Basic
. // need nclust1 option to replicate CBH
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(age) center corrnum(0) nclust1                                                  ///
>         tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)                                               ///
>         maxiter(10000) maxpsiiter(10) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4626178      0.6848813
           lintst |       0.1151006      0.1509025
            _cons |*       6.6555486      4.6275416
------------------+--------------------------------
*Not penalized
r; t=0.05 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:32

. // assert reldif(lambda,e(lambda0))<1e-8
. // partial-out cons
. // need nclust1 option to replicate CBH
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(age) center corrnum(0) nclust1                                                  ///
>         partial(_cons) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)                ///
>         maxiter(10000) maxpsiiter(10) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4626178      0.6848813
           lintst |       0.1151006      0.1509025
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.6555486      4.6275416
------------------+--------------------------------
r; t=0.06 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:32

. // assert reldif(lambda,e(lambda0))<1e-8
.
. // With controls/pnotpen - note that controls do not appear in CBH X list
. /*
> lassoClusterCBH lrprice intst lintst y81ldist lintstsq y81nrinc,        ///
>         cluster(age) verb(0) tolzero(1e-10) tolups(1e-10) ltol(1e-8) maxiter(10000) controls(larea cbd)
> mat CBHbetaL=r(betaL)'
> mat CBHbetaPL=r(betaPL)'
> scalar lambda=r(lambda)
> */
. mat CBHbetaL    = .00552125 , .00301795
r; t=0.00 0:12:32

. mat CBHbetaPL   = .01283716 , .01236437
r; t=0.00 0:12:32

. scalar lambda   = 48.38313172
r; t=0.00 0:12:32

. local s = 2
r; t=0.00 0:12:32

. // pnotpen
. // need nclust1 option to replicate CBH
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(age) center corrnum(0) nclust1                                                  ///
>         tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000)                ///
>         pnotpen(larea cbd) maxpsiiter(10) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.7003866      0.6450951
              cbd |*       0.0000063     -0.0000065
         y81ldist |       0.0055212      0.0128372
         lintstsq |       0.0030180      0.0123644
            _cons |*       5.5434559      5.2894131
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. // trim
. mat beta=beta[1,3..4]
r; t=0.00 0:12:32

. mat betaOLS=betaOLS[1,3..4]
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-6
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-6
  4. }
r; t=0.00 0:12:32

. // assert reldif(lambda,e(lambda0))<1e-8
. // partial
. // need nclust1 option to replicate CBH
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(age) center corrnum(0) nclust1                                                  ///
>         tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000)                ///
>         partial(larea cbd) maxpsiiter(10) c0(0.55)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
         y81ldist |       0.0055212      0.0128372
         lintstsq |       0.0030180      0.0123644
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.7003866      0.6450951
              cbd |       0.0000063     -0.0000065
            _cons |       5.5434559      5.2894131
------------------+--------------------------------
r; t=0.04 0:12:32

. mat b=e(b)
r; t=0.00 0:12:32

. mat beta=e(beta)
r; t=0.00 0:12:32

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:32

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:32

. // trim
. mat beta=beta[1,1..2]
r; t=0.00 0:12:32

. mat betaOLS=betaOLS[1,1..2]
r; t=0.00 0:12:32

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:32

. // assert reldif(lambda,e(lambda0))<1e-8
.
.
. ********************** CLUSTER+FE *********************************
. // Check vs. CBH code.
.
. xtset age
       panel variable:  age (unbalanced)
r; t=0.00 0:12:32

. // clustering (note no lalt required) + fixed effects
. // note center and maxpsiiter are required to match CBH code
. /*
> lassoClusterCBH lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,      ///
>         cluster(age) fix(age) verb(0) tolzero(1e-10) tolups(1e-10) ltol(1e-8) maxiter(10000)
> mat CBHbetaL=r(betaL)'
> mat CBHbetaPL=r(betaPL)'
> scalar lambda=r(lambda)
> */
. mat CBHbetaL    = .38441642 , .00767435
r; t=0.00 0:12:32

. mat CBHbetaPL   = .53696874 , .01285201
r; t=0.00 0:12:32

. scalar lambda   = 50.21173664
r; t=0.00 0:12:32

. local s = 2
r; t=0.00 0:12:32

. // Basic
. // need nclust1 option to replicate CBH
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(age) fe center corrnum(0) nclust1                                               ///
>         tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)                                               ///
>         maxiter(10000) maxpsiiter(10) c0(0.55)
(sorting by xtset panelvar age)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.3844164      0.5369687
         y81ldist |       0.0076744      0.0128520
------------------+--------------------------------
r; t=0.80 0:12:33

. mat b=e(b)
r; t=0.00 0:12:33

. mat beta=e(beta)
r; t=0.00 0:12:33

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:33

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:33

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:33

. // assert reldif(lambda,e(lambda0))<1e-8
.
. // With controls/pnotpen - note that controls do not appear in CBH X list
. /*
> lassoClusterCBH lrprice intst lintst y81ldist lintstsq y81nrinc,        ///
>         cluster(age) fix(age) verb(0) tolzero(1e-10) tolups(1e-10) ltol(1e-8) maxiter(10000) controls(larea cbd)
> mat CBHbetaL=r(betaL)'
> mat CBHbetaPL=r(betaPL)'
> scalar lambda=r(lambda)
> */
. mat CBHbetaL    = .00308359
r; t=0.00 0:12:33

. mat CBHbetaPL   = .01266582
r; t=0.00 0:12:33

. scalar lambda   = 48.38313172
r; t=0.00 0:12:33

. local s = 1
r; t=0.00 0:12:33

. // pnotpen
. // need nclust1 option to replicate CBH
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(age) fe center corrnum(0) nclust1                                               ///
>         tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000)                ///
>         pnotpen(larea cbd) maxpsiiter(10) c0(0.55)
(sorting by xtset panelvar age)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.5719870      0.5368213
              cbd |*      -0.0000016     -0.0000007
         y81ldist |       0.0030836      0.0126658
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:33

. mat b=e(b)
r; t=0.00 0:12:33

. mat beta=e(beta)
r; t=0.00 0:12:33

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:33

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:33

. // trim
. mat beta=beta[1,3..3]
r; t=0.00 0:12:33

. mat betaOLS=betaOLS[1,3..3]
r; t=0.00 0:12:33

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-6
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-6
  4. }
r; t=0.00 0:12:33

. // assert reldif(lambda,e(lambda0))<1e-8
. // partial
. // need nclust1 option to replicate CBH
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(age) fe center corrnum(0) nclust1                                               ///
>         tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000)                ///
>         partial(larea cbd) maxpsiiter(10) c0(0.55)
(sorting by xtset panelvar age)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
         y81ldist |       0.0030836      0.0126658
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.5719870      0.5368213
              cbd |      -0.0000016     -0.0000007
------------------+--------------------------------
r; t=0.04 0:12:33

. mat b=e(b)
r; t=0.00 0:12:33

. mat beta=e(beta)
r; t=0.00 0:12:33

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:33

. assert mreldif(b,beta)<1e-8
r; t=0.00 0:12:33

. // trim
. mat beta=beta[1,1..1]
r; t=0.00 0:12:33

. mat betaOLS=betaOLS[1,1..1]
r; t=0.00 0:12:33

. forvalues i=1/`s' {
  2.         assert reldif(CBHbetaL[1,`i'],beta[1,`i'])<1e-8
  3.         assert reldif(CBHbetaPL[1,`i'],betaOLS[1,`i'])<1e-8
  4. }
r; t=0.00 0:12:33

. // assert reldif(lambda,e(lambda0))<1e-8
.
. ***************************************************************
. // Equivalence check
.
. // Robust vs. singleton clusters - should match
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         rob tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) maxpsiiter(10)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:33

. savedresults save rob e()
r; t=0.00 0:12:33

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(n) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) maxpsiiter(10)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:33

. savedresults comp rob e(), exclude(macro: cmdline robust cluster)  tol(1e-8)
r; t=0.00 0:12:33

.
. // center option
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         rob center tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) maxpsiiter(10)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:33

. savedresults save rob e()
r; t=0.00 0:12:33

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         cluster(n) center tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) maxpsiiter(10)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:33

. savedresults comp rob e(), exclude(macro: cmdline robust cluster)  tol(1e-8)
r; t=0.00 0:12:33

.
. // partial and pnotpen
. // with constant
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         partial(lintst y81ldist)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5116156      0.6512871
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
           lintst |       0.1733954      0.1556115
         y81ldist |       0.0145826      0.0129758
            _cons |       5.6673104      4.7817721
------------------+--------------------------------
r; t=0.03 0:12:33

. savedresults save partial e()
r; t=0.00 0:12:33

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         pnotpen(lintst y81ldist)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5116156      0.6512871
           lintst |*       0.1733954      0.1556115
         y81ldist |*       0.0145826      0.0129758
            _cons |*       5.6673104      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:33

. savedresults comp partial e(), exclude(                                                         ///
>                 macro: cmdline pnotpen partial varXmodel                                        ///
>                 scalar: niter pminus pnotpen_ct partial_ct r2                           ///
>                 matrix: betaAll betaAllOLS Psi ePsi sPsi                                        /// order/components differ
>                 )  tol(1e-8)
r; t=0.00 0:12:33

. // with constant + prestd
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         partial(lintst y81ldist) prestd

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5116156      0.6512871
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
           lintst |       0.1733954      0.1556115
         y81ldist |       0.0145826      0.0129758
            _cons |       5.6673104      4.7817721
------------------+--------------------------------
r; t=0.03 0:12:33

. savedresults save partial e()
r; t=0.00 0:12:33

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         pnotpen(lintst y81ldist) prestd

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5116156      0.6512871
           lintst |*       0.1733954      0.1556115
         y81ldist |*       0.0145826      0.0129758
            _cons |*       5.6673104      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.04 0:12:33

. savedresults comp partial e(), exclude(                                                         ///
>                 macro: cmdline pnotpen partial varXmodel                                        ///
>                 scalar: niter pminus pnotpen_ct partial_ct r2                           ///
>                 matrix: betaAll betaAllOLS Psi ePsi sPsi                                        /// order/components differ
>                 )  tol(1e-8)
r; t=0.00 0:12:33

. // with constant + sqrt
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         partial(lintst y81ldist) sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5093104      0.6512871
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
           lintst |       0.1736889      0.1556115
         y81ldist |       0.0146091      0.0129758
            _cons |       5.6819256      4.7817721
------------------+--------------------------------
r; t=0.03 0:12:33

. savedresults save partial e()
r; t=0.00 0:12:33

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         pnotpen(lintst y81ldist) sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5093104      0.6512871
           lintst |*       0.1736889      0.1556115
         y81ldist |*       0.0146091      0.0129758
            _cons |*       5.6819256      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:33

. savedresults comp partial e(), exclude(                                                         ///
>                 macro: cmdline pnotpen partial varXmodel                                        ///
>                 scalar: niter pminus pnotpen_ct partial_ct r2                           ///
>                 matrix: betaAll betaAllOLS Psi ePsi sPsi                                        /// order/components differ
>                 )  tol(1e-8)
r; t=0.00 0:12:33

. // nocons - need an easier minimization
. rlasso lrprice nbh rooms baths y81 nearinc y81nrinc,                            ///
>         partial(nearinc y81nrinc) nocons

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
              nbh |       0.0150455      0.0688260
            rooms |       1.5564775      1.5416549
            baths |       0.0378460      0.0916800
              y81 |       0.4460697      0.6927288
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |       1.4407318      1.2871243
         y81nrinc |      -0.4910026     -0.7296275
------------------+--------------------------------
r; t=0.04 0:12:33

. savedresults save partial e()
r; t=0.00 0:12:33

. rlasso lrprice nbh rooms baths y81 nearinc y81nrinc,                            ///
>         pnotpen(nearinc y81nrinc) nocons

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
              nbh |       0.0150455      0.0688260
            rooms |       1.5564775      1.5416549
            baths |       0.0378460      0.0916800
              y81 |       0.4460697      0.6927288
          nearinc |*       1.4407318      1.2871243
         y81nrinc |*      -0.4910026     -0.7296275
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:33

. savedresults comp partial e(), exclude(                                                         ///
>                 macro: cmdline pnotpen partial varXmodel                                        ///
>                 scalar: niter pminus pnotpen_ct partial_ct r2                           ///
>                 matrix: betaAll betaAllOLS Psi ePsi sPsi                                        /// order/components differ
>                 )  tol(1e-8)
r; t=0.00 0:12:33

. // nocons + prestd
. rlasso lrprice nbh rooms baths y81 nearinc y81nrinc,                            ///
>         partial(nearinc y81nrinc) nocons prestd

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
              nbh |       0.0150455      0.0688260
            rooms |       1.5564775      1.5416549
            baths |       0.0378460      0.0916800
              y81 |       0.4460697      0.6927288
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |       1.4407318      1.2871243
         y81nrinc |      -0.4910026     -0.7296275
------------------+--------------------------------
r; t=0.04 0:12:33

. savedresults save partial e()
r; t=0.00 0:12:33

. rlasso lrprice nbh rooms baths y81 nearinc y81nrinc,                            ///
>         pnotpen(nearinc y81nrinc) nocons prestd

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
              nbh |       0.0150455      0.0688260
            rooms |       1.5564775      1.5416549
            baths |       0.0378460      0.0916800
              y81 |       0.4460697      0.6927288
          nearinc |*       1.4407318      1.2871243
         y81nrinc |*      -0.4910026     -0.7296275
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:33

. savedresults comp partial e(), exclude(                                                         ///
>                 macro: cmdline pnotpen partial varXmodel                                        ///
>                 scalar: niter pminus pnotpen_ct partial_ct r2                           ///
>                 matrix: betaAll betaAllOLS Psi ePsi sPsi                                        /// order/components differ
>                 )  tol(1e-8)
r; t=0.00 0:12:33

. // nocons + sqrt
. rlasso lrprice nbh rooms baths y81 nearinc y81nrinc,                            ///
>         partial(nearinc y81nrinc) nocons sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
              nbh |       0.0137579      0.0688260
            rooms |       1.5568323      1.5416549
            baths |       0.0365571      0.0916800
              y81 |       0.4401644      0.6927288
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |       1.4444094      1.2871243
         y81nrinc |      -0.4852896     -0.7296275
------------------+--------------------------------
r; t=0.10 0:12:33

. savedresults save partial e()
r; t=0.00 0:12:33

. rlasso lrprice nbh rooms baths y81 nearinc y81nrinc,                            ///
>         pnotpen(nearinc y81nrinc) nocons sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
              nbh |       0.0137579      0.0688260
            rooms |       1.5568323      1.5416549
            baths |       0.0365571      0.0916800
              y81 |       0.4401644      0.6927288
          nearinc |*       1.4444094      1.2871243
         y81nrinc |*      -0.4852896     -0.7296275
------------------+--------------------------------
*Not penalized
r; t=0.15 0:12:34

. savedresults comp partial e(), exclude(                                                         ///
>                 macro: cmdline pnotpen partial varXmodel                                        ///
>                 scalar: niter pminus pnotpen_ct partial_ct r2                           ///
>                 matrix: betaAll betaAllOLS Psi ePsi sPsi                                        /// order/components differ
>                 )  tol(1e-8)
r; t=0.00 0:12:34

.
. // psolver option
. rlasso lrprice nbh rooms baths y81 nearinc y81nrinc,                            ///
>         partial(nearinc y81nrinc)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            rooms |       0.0292232      0.0653731
            baths |       0.2635702      0.2964328
              y81 |       0.0735169      0.1718676
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |      -0.1888857     -0.0938656
         y81nrinc |       0.0620594     -0.0393664
            _cons |      10.4685177     10.0942764
------------------+--------------------------------
r; t=0.03 0:12:34

. savedresults save partial e()
r; t=0.00 0:12:34

. // default solver is qrxx = QR+quadcross
. foreach solver in svd svdxx qr lu luxx chol {
  2.         rlasso lrprice nbh rooms baths y81 nearinc y81nrinc,                    ///
>                 partial(nearinc y81nrinc) psolver(`solver')
  3.         savedresults comp partial e(), exclude(                                                 ///
>                 macro: cmdline                                                                                          ///
>                         )  tol(1e-8)
  4. }

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            rooms |       0.0292232      0.0653731
            baths |       0.2635702      0.2964328
              y81 |       0.0735169      0.1718676
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |      -0.1888857     -0.0938656
         y81nrinc |       0.0620594     -0.0393664
            _cons |      10.4685177     10.0942764
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            rooms |       0.0292232      0.0653731
            baths |       0.2635702      0.2964328
              y81 |       0.0735169      0.1718676
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |      -0.1888857     -0.0938656
         y81nrinc |       0.0620594     -0.0393664
            _cons |      10.4685177     10.0942764
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            rooms |       0.0292232      0.0653731
            baths |       0.2635702      0.2964328
              y81 |       0.0735169      0.1718676
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |      -0.1888857     -0.0938656
         y81nrinc |       0.0620594     -0.0393664
            _cons |      10.4685177     10.0942764
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            rooms |       0.0292232      0.0653731
            baths |       0.2635702      0.2964328
              y81 |       0.0735169      0.1718676
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |      -0.1888857     -0.0938656
         y81nrinc |       0.0620594     -0.0393664
            _cons |      10.4685177     10.0942764
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            rooms |       0.0292232      0.0653731
            baths |       0.2635702      0.2964328
              y81 |       0.0735169      0.1718676
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |      -0.1888857     -0.0938656
         y81nrinc |       0.0620594     -0.0393664
            _cons |      10.4685177     10.0942764
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            rooms |       0.0292232      0.0653731
            baths |       0.2635702      0.2964328
              y81 |       0.0735169      0.1718676
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
          nearinc |      -0.1888857     -0.0938656
         y81nrinc |       0.0620594     -0.0393664
            _cons |      10.4685177     10.0942764
------------------+--------------------------------
r; t=0.22 0:12:34

.
. ********************** FE ONLY *********************************
. // Equivalence check
.
. // fe vs. explicit dummies
. xtset age
       panel variable:  age (unbalanced)
r; t=0.00 0:12:34

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         fe tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) maxpsiiter(10)
(sorting by xtset panelvar age)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4188396      0.5369687
         y81ldist |       0.0057321      0.0128520
------------------+--------------------------------
r; t=0.02 0:12:34

. mat b=e(b)
r; t=0.00 0:12:34

. mat beta=e(beta)
r; t=0.00 0:12:34

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:34

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc i.age, ///
>         partial(i.age) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8) maxiter(10000) maxpsiiter(10)
Warning: reduced rank encountered when partialling out (may be caused by omitted vars).

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4188396      0.5369687
         y81ldist |       0.0057321      0.0128520
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
                  |
              age |
               0  |       (omitted)      (omitted)
               1  |      -0.1074253     -0.0757639
               2  |      -0.1620694     -0.1650689
               3  |      -0.2466746     -0.2043496
               4  |      -0.0639881     -0.0205436
               5  |      -0.1138763     -0.0620101
               6  |      -0.1444857     -0.1641423
               7  |      -0.1698061     -0.1580507
               8  |      -0.0562016     -0.0259286
               9  |      -0.0324962      0.0379956
              10  |      -0.1192847     -0.0697881
              11  |      -0.2754333     -0.2125293
              12  |      -0.1213177     -0.0726461
              13  |      -0.2318380     -0.1785931
              14  |      -0.2532146     -0.1606708
              15  |      -0.3913400     -0.3146402
              16  |      -0.2509969     -0.2372362
              17  |      -0.1955477     -0.2203558
              18  |      -0.3228346     -0.2247184
              19  |      -0.2938447     -0.2590578
              20  |      -0.1768026     -0.1417986
              21  |      -0.5189102     -0.4425419
              22  |      -0.3357346     -0.2426813
              23  |      -0.2430005     -0.2075176
              24  |      -0.3315197     -0.2929506
              25  |      -0.6068101     -0.5375448
              26  |      -0.2531357     -0.2009325
              28  |      -0.5178013     -0.4167807
              29  |      -0.4020205     -0.3838838
              30  |      -0.2277585     -0.1231172
              31  |      -0.5358766     -0.5437240
              32  |      -0.3932134     -0.2811164
              33  |      -0.5157354     -0.4156201
              36  |      -0.4310962     -0.4028292
              38  |      -0.5276451     -0.4835454
              39  |      -0.4023266     -0.3749707
              41  |      -0.5531938     -0.4814310
              42  |      -0.3480954     -0.2659741
              48  |      -0.5651149     -0.4815839
              51  |      -0.6866603     -0.6321052
              54  |      -0.3140009     -0.1628040
              56  |      -0.7393784     -0.6890353
              58  |      -0.3905766     -0.2762806
              59  |      -0.6218065     -0.6176855
              65  |      -0.5446228     -0.5759021
              68  |      -0.6144933     -0.5480469
              70  |      -0.5421796     -0.5077154
              71  |      -0.7987981     -0.7774028
              78  |      -0.7194524     -0.6192860
              81  |      -0.6661622     -0.6576958
              83  |      -0.9562909     -0.9290052
             104  |      -0.7892082     -0.7602831
             121  |      -0.5842908     -0.5680719
             128  |      -0.9758364     -0.9358631
             131  |      -0.3348417     -0.4219250
             148  |      -0.9369465     -0.8412708
             153  |      -0.2163358     -0.2330860
             158  |       0.1272742      0.1433503
             178  |      -0.2742254     -0.1996289
             188  |      -0.6151410     -0.5871642
             189  |       0.2967434      0.2454967
                  |
            _cons |       8.2575448      7.3008346
------------------+--------------------------------
r; t=0.10 0:12:34

. mat btemp=(el(e(b),1,1), el(e(b),1,2))
r; t=0.00 0:12:34

. assert mreldif(b,btemp)<1e-8
r; t=0.00 0:12:34

. mat btemp=(el(e(beta),1,1), el(e(beta),1,2))
r; t=0.00 0:12:34

. assert mreldif(beta,btemp)<1e-8
r; t=0.00 0:12:34

. mat btemp=(el(e(betaOLS),1,1), el(e(betaOLS),1,2))
r; t=0.00 0:12:34

. assert mreldif(betaOLS,btemp)<1e-8
r; t=0.00 0:12:34

.
. // Controls vs pnotpen
. xtset age
       panel variable:  age (unbalanced)
r; t=0.00 0:12:34

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         fe partial(larea cbd) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)
(sorting by xtset panelvar age)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
         y81ldist |       0.0038551      0.0126658
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.5691556      0.5368213
              cbd |      -0.0000015     -0.0000007
------------------+--------------------------------
r; t=0.04 0:12:34

. mat b=e(b)
r; t=0.00 0:12:34

. mat beta=e(beta)
r; t=0.00 0:12:34

. mat betaOLS=e(betaOLS)
r; t=0.00 0:12:34

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc,       ///
>         fe pnotpen(larea cbd) tolzero(1e-10) tolpsi(1e-10) tolopt(1e-8)
(sorting by xtset panelvar age)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.5691556      0.5368213
              cbd |*      -0.0000015     -0.0000007
         y81ldist |       0.0038551      0.0126658
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:34

. mat btemp=e(b)
r; t=0.00 0:12:34

. assert reldif(b[1,1],btemp[1,3])<1e-8
r; t=0.00 0:12:34

. assert reldif(b[1,2],btemp[1,1])<1e-8
r; t=0.00 0:12:34

. assert reldif(b[1,3],btemp[1,2])<1e-8
r; t=0.00 0:12:34

. mat btemp=e(beta)
r; t=0.00 0:12:34

. assert reldif(beta[1,1],btemp[1,3])<1e-8
r; t=0.00 0:12:34

. assert reldif(beta[1,2],btemp[1,1])<1e-8
r; t=0.00 0:12:34

. assert reldif(beta[1,3],btemp[1,2])<1e-8
r; t=0.00 0:12:34

. mat btemp=e(betaOLS)
r; t=0.00 0:12:34

. assert reldif(betaOLS[1,1],btemp[1,3])<1e-8
r; t=0.00 0:12:34

. assert reldif(betaOLS[1,2],btemp[1,1])<1e-8
r; t=0.00 0:12:34

. assert reldif(betaOLS[1,3],btemp[1,2])<1e-8
r; t=0.00 0:12:34

.
. // noftools option
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, fe
(sorting by xtset panelvar age)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4188396      0.5369687
         y81ldist |       0.0057321      0.0128520
------------------+--------------------------------
r; t=0.01 0:12:34

. savedresults save ftools e()
r; t=0.00 0:12:34

. cap noi assert "`e(noftools)'"==""  // will be error if ftools not installed
r; t=0.00 0:12:34

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, fe noftools
(sorting by xtset panelvar age)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4188396      0.5369687
         y81ldist |       0.0057321      0.0128520
------------------+--------------------------------
r; t=0.02 0:12:34

. assert "`e(noftools)'"=="noftools"
r; t=0.00 0:12:34

. savedresults comp ftools e(), exclude(macro: cmdline) tol(1e-10)
r; t=0.00 0:12:34

.
. ********************** Standardization **************************
.
. foreach opt in " " "sqrt" {
  2.         rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, rob `opt'
  3.         savedresults save nostd e()
  4.         rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, prestd rob `opt'
  5.         savedresults comp nostd e(), exclude(macro: cmdline scalar: niter matrix: Psi) tol(1e-8)
  6.
.         rlasso lrprice larea lintst y81ldist lintstsq y81nrinc, rob `opt' pnotpen(larea lintst)
  7.         savedresults save nostd e()
  8.         rlasso lrprice larea lintst y81ldist lintstsq y81nrinc, prestd rob `opt' pnotpen(larea lintst)
  9.         savedresults comp nostd e(), exclude(macro: cmdline scalar: niter matrix: Psi) tol(1e-8)
 10.
.         rlasso lrprice larea lintst y81ldist lintstsq y81nrinc, rob `opt' partial(larea lintst)
 11.         savedresults save nostd e()
 12.         rlasso lrprice larea lintst y81ldist lintstsq y81nrinc, prestd rob `opt' partial(larea lintst)
 13.         savedresults comp nostd e(), exclude(macro: cmdline scalar: niter matrix: Psi) tol(1e-8)
 14.
.         // note that some coefs have been removed to make it easier for sqrt-lasso
.         xtset id
 15.         rlasso lrprice larea lintst y81ldist, rob `opt' fe
 16.         savedresults save nostd e()
 17.         rlasso lrprice larea lintst y81ldist, prestd rob `opt' fe
 18.         savedresults comp nostd e(), exclude(macro: cmdline scalar: niter matrix: Psi) tol(1e-8)
 19.
.         xtset id
 20.         rlasso lrprice larea lintst y81ldist, rob `opt' fe pnotpen(larea)
 21.         savedresults save nostd e()
 22.         rlasso lrprice larea lintst y81ldist, prestd rob `opt' fe pnotpen(larea)
 23.         savedresults comp nostd e(), exclude(macro: cmdline scalar: niter matrix: Psi) tol(1e-8)
 24.
.         xtset id
 25.         rlasso lrprice larea lintst y81ldist, rob `opt' fe partial(larea)
 26.         savedresults save nostd e()
 27.         rlasso lrprice larea lintst y81ldist, prestd rob `opt' fe partial(larea)
 28.         savedresults comp nostd e(), exclude(macro: cmdline scalar: niter matrix: Psi) tol(1e-8)
 29. }

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.6743944      0.6512871
           lintst |*       0.1523724      0.1556115
         y81ldist |       0.0040506      0.0129758
            _cons |*       4.6756870      4.7817721
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.6743944      0.6512871
           lintst |*       0.1523724      0.1556115
         y81ldist |       0.0040506      0.0129758
            _cons |*       4.6756870      4.7817721
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
         y81ldist |       0.0040506      0.0129758
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.6743944      0.6512871
           lintst |       0.1523724      0.1556115
            _cons |       4.6756870      4.7817721
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
         y81ldist |       0.0040506      0.0129758
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.6743944      0.6512871
           lintst |       0.1523724      0.1556115
            _cons |       4.6756870      4.7817721
------------------+--------------------------------
       panel variable:  id (unbalanced)
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4339910      0.5869142
           lintst |       0.0586900      0.1640386
------------------+--------------------------------
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4339910      0.5869142
           lintst |       0.0586900      0.1640386
------------------+--------------------------------
       panel variable:  id (unbalanced)
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.6049295      0.5869142
           lintst |       0.0487458      0.1640386
------------------+--------------------------------
*Not penalized
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.6049295      0.5869142
           lintst |       0.0487458      0.1640386
------------------+--------------------------------
*Not penalized
       panel variable:  id (unbalanced)
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
           lintst |       0.0487458      0.1640386
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.6049295      0.5869142
------------------+--------------------------------
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
           lintst |       0.0487458      0.1640386
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.6049295      0.5869142
------------------+--------------------------------

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5454463      0.6512871
           lintst |       0.1062146      0.1556115
         y81ldist |       0.0042061      0.0129758
            _cons |*       6.0922598      4.7817721
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5454463      0.6512871
           lintst |       0.1062146      0.1556115
         y81ldist |       0.0042061      0.0129758
            _cons |*       6.0922598      4.7817721
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.6747441      0.6512871
           lintst |*       0.1523234      0.1556115
         y81ldist |       0.0039155      0.0129758
            _cons |*       4.6740813      4.7817721
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.6747441      0.6512871
           lintst |*       0.1523234      0.1556115
         y81ldist |       0.0039155      0.0129758
            _cons |*       4.6740813      4.7817721
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
         y81ldist |       0.0039155      0.0129758
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.6747441      0.6512871
           lintst |       0.1523234      0.1556115
            _cons |       4.6740813      4.7817721
------------------+--------------------------------

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
         y81ldist |       0.0039155      0.0129758
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.6747441      0.6512871
           lintst |       0.1523234      0.1556115
            _cons |       4.6740813      4.7817721
------------------+--------------------------------
       panel variable:  id (unbalanced)
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4279098      0.5869142
           lintst |       0.0545007      0.1640386
------------------+--------------------------------
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4279098      0.5869142
           lintst |       0.0545007      0.1640386
------------------+--------------------------------
       panel variable:  id (unbalanced)
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.6052398      0.5869142
           lintst |       0.0467594      0.1640386
------------------+--------------------------------
*Not penalized
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |*       0.6052398      0.5869142
           lintst |       0.0467594      0.1640386
------------------+--------------------------------
*Not penalized
       panel variable:  id (unbalanced)
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
           lintst |       0.0467594      0.1640386
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.6052398      0.5869142
------------------+--------------------------------
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
           lintst |       0.0467594      0.1640386
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            larea |       0.6052398      0.5869142
------------------+--------------------------------
r; t=0.76 0:12:35

.
. ********************** Misc options **************************
.
. // Confirm these don't crash it and that post-lasso is the same.
. // Lasso est may differ because of slightly different lambda.
. mat betaOLS     = .65128713 , .15561146 , .01297576 , 4.7817721
r; t=0.00 0:12:35

. foreach opt in  tolopt(1e-8) tolpsi(1e-3) tolzero(1e-3) ///
>                                 maxiter(1000) maxpsiiter(5)                             ///
>                                 lassopsi corrn(3) corrn(0)                              ///
>                                 c(1.05) gamma(0.05) gammad(1)                   ///
>                                 {
  2.         di "opt=`opt'"
  3.         rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, `opt' rob
  4.         assert mreldif(betaOLS,e(betaOLS))<1e-8
  5. }
opt=tolopt(1e-8)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
opt=tolpsi(1e-3)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
opt=tolzero(1e-3)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
opt=maxiter(1000)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
opt=maxpsiiter(5)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
opt=lassopsi

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5528697      0.6512871
           lintst |       0.1076152      0.1556115
         y81ldist |       0.0043384      0.0129758
            _cons |*       6.0220098      4.7817721
------------------+--------------------------------
*Not penalized
opt=corrn(3)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
opt=corrn(0)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
opt=c(1.05)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5541429      0.6512871
           lintst |       0.1108752      0.1556115
         y81ldist |       0.0051244      0.0129758
            _cons |*       5.9780180      4.7817721
------------------+--------------------------------
*Not penalized
opt=gamma(0.05)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5608241      0.6512871
           lintst |       0.1139520      0.1556115
         y81ldist |       0.0056643      0.0129758
            _cons |*       5.8957444      4.7817721
------------------+--------------------------------
*Not penalized
opt=gammad(1)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5688986      0.6512871
           lintst |       0.1176704      0.1556115
         y81ldist |       0.0063169      0.0129758
            _cons |*       5.7963143      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.17 0:12:35

.
. // xdep option - standard lasso
. // NB: update of values for lassoutils 1.1.01 8nov2018
. // benchmark - homoskedastic, no xdep
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5677921      0.6512871
           lintst |       0.1061444      0.1556115
         y81ldist |       0.0043898      0.0129758
            _cons |*       5.9223612      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:35

. mat betaOLS = e(betaOLS)
r; t=0.00 0:12:35

. // xdep, homoskedastic
. // uses multiplier bootstrap so set seed for replicability
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, xdep seed(1)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5711901      0.6512871
           lintst |       0.1081575      0.1556115
         y81ldist |       0.0047392      0.0129758
            _cons |*       5.8759434      4.7817721
------------------+--------------------------------
*Not penalized
r; t=1.12 0:12:36

. assert mreldif(betaOLS,e(betaOLS))<1e-8
r; t=0.00 0:12:36

. assert reldif(e(lambda0),114.432846013)<1e-8
r; t=0.00 0:12:36

. assert reldif(el(e(beta),1,1),0.571190061895)<1e-8
r; t=0.00 0:12:36

. // benchmark - heteroskedastic, no xdep
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, rob

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5495169      0.6512871
           lintst |       0.1087449      0.1556115
         y81ldist |       0.0047505      0.0129758
            _cons |*       6.0349821      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:36

. mat betaOLS = e(betaOLS)
r; t=0.00 0:12:36

. // xdep, heteroskedastic
. // uses multiplier bootstrap so set seed for replicability
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, xdep rob seed(1)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5549289      0.6512871
           lintst |       0.1112372      0.1556115
         y81ldist |       0.0051879      0.0129758
            _cons |*       5.9683382      4.7817721
------------------+--------------------------------
*Not penalized
r; t=1.14 0:12:37

. assert mreldif(betaOLS,e(betaOLS))<1e-8
r; t=0.00 0:12:37

. assert reldif(e(lambda0),112.94387827)<1e-8
r; t=0.00 0:12:37

. assert reldif(el(e(beta),1,1),0.554928934936)<1e-8
r; t=0.00 0:12:37

. // benchmark - cluster, no xdep
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, cluster(id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5679360      0.6512871
           lintst |       0.0994303      0.1556115
         y81ldist |       0.0028173      0.0129758
            _cons |*       5.9917497      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:37

. mat betaOLS = e(betaOLS)
r; t=0.00 0:12:37

. // xdep, cluster
. // uses multiplier bootstrap so set seed for replicability
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, xdep cluster(id) seed(1)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5715880      0.6512871
           lintst |       0.1018919      0.1556115
         y81ldist |       0.0032624      0.0129758
            _cons |*       5.9387351      4.7817721
------------------+--------------------------------
*Not penalized
r; t=4.83 0:12:42

. assert mreldif(betaOLS,e(betaOLS))<1e-8
r; t=0.00 0:12:42

. assert reldif(e(lambda0),108.22341837697)<1e-8
r; t=0.00 0:12:42

. assert reldif(el(e(beta),1,1),0.571588016862)<1e-8
r; t=0.00 0:12:42

.
. // xdep option - sqrt-lasso
. // NB: introduced with update of lassoutils 1.1.01 8nov2018
. // benchmark - homoskedastic, no xdep
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5641933      0.6512871
           lintst |       0.1040122      0.1556115
         y81ldist |       0.0040198      0.0129758
            _cons |*       5.9715237      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:42

. mat betaOLS = e(betaOLS)
r; t=0.00 0:12:42

. // xdep, homoskedastic
. // uses multiplier bootstrap so set seed for replicability
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, xdep seed(1) sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5682010      0.6512871
           lintst |       0.1063866      0.1556115
         y81ldist |       0.0044319      0.0129758
            _cons |*       5.9167762      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.64 0:12:43

. assert mreldif(betaOLS,e(betaOLS))<1e-8
r; t=0.00 0:12:43

. assert reldif(e(lambda0),57.107312563841)<1e-8
r; t=0.00 0:12:43

. assert reldif(el(e(beta),1,1),0.568200965628)<1e-8
r; t=0.00 0:12:43

. // benchmark - heteroskedastic, no xdep
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, rob sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5454463      0.6512871
           lintst |       0.1062146      0.1556115
         y81ldist |       0.0042061      0.0129758
            _cons |*       6.0922598      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.04 0:12:43

. mat betaOLS = e(betaOLS)
r; t=0.00 0:12:43

. // xdep, heteroskedastic
. // uses multiplier bootstrap so set seed for replicability
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, xdep rob seed(1) sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5540276      0.6512871
           lintst |       0.1102196      0.1556115
         y81ldist |       0.0049171      0.0129758
            _cons |*       5.9860081      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.67 0:12:43

. assert mreldif(betaOLS,e(betaOLS))<1e-8
r; t=0.00 0:12:43

. assert reldif(e(lambda0),55.179713611581)<1e-8
r; t=0.00 0:12:43

. assert reldif(el(e(beta),1,1),0.554027632301)<1e-8
r; t=0.00 0:12:43

. // benchmark - cluster, no xdep
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, cluster(id) sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5633576      0.6512871
           lintst |       0.0963444      0.1556115
         y81ldist |       0.0022593      0.0129758
            _cons |*       6.0582127      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.06 0:12:44

. mat betaOLS = e(betaOLS)
r; t=0.00 0:12:44

. // xdep, cluster
. // uses multiplier bootstrap so set seed for replicability
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, xdep cluster(id) seed(1) sqrt

------------------+--------------------------------
         Selected |      Sqrt-lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5713229      0.6512871
           lintst |       0.1017132      0.1556115
         y81ldist |       0.0032301      0.0129758
            _cons |*       5.9425831      4.7817721
------------------+--------------------------------
*Not penalized
r; t=2.50 0:12:46

. assert mreldif(betaOLS,e(betaOLS))<1e-8
r; t=0.00 0:12:46

. assert reldif(e(lambda0),51.922234705643)<1e-8
r; t=0.00 0:12:46

. assert reldif(el(e(beta),1,1),0.571322946179)<1e-8
r; t=0.00 0:12:46

.
. // supscore option
. // uses multiplier bootstrap so set seed for replicability
. // update for lassoutils 1.1.01 8nov2018 - now sqrt(n)*L instead of n*L
. // set seed for null
. set seed 1
r; t=0.00 0:12:46

. gen double ynull=rnormal()
r; t=0.00 0:12:46

. // homoskedastic
. rlasso ynull cbd intst lintst y81ldist lintstsq y81nrinc, testonly seed(1)

Sup-score test H0: beta=0
CCK sup-score statistic   1.73 p-value=  0.242
CCK 5% critical value     2.90 (asympt bound)
r; t=0.06 0:12:46

. assert reldif(e(supscore),1.7293626410)<1e-8
r; t=0.00 0:12:46

. assert reldif(e(supscore_p),0.242)<1e-8
r; t=0.00 0:12:46

. assert reldif(e(supscore_cv),2.9020830008)<1e-8
r; t=0.00 0:12:46

. // critical value for gamma=10%
. rlasso ynull cbd intst lintst y81ldist lintstsq y81nrinc, testonly ssgamma(0.1) seed(1)

Sup-score test H0: beta=0
CCK sup-score statistic   1.73 p-value=  0.242
CCK 10% critical value    2.63 (asympt bound)
r; t=0.06 0:12:46

. assert reldif(e(supscore_cv),2.63337777980)<1e-8
r; t=0.00 0:12:46

. // heteroskedastic
. rlasso ynull cbd intst lintst y81ldist lintstsq y81nrinc, testonly rob seed(1)

Sup-score test H0: beta=0
CCK sup-score statistic   1.74 p-value=  0.238
CCK 5% critical value     2.90 (asympt bound)
r; t=0.06 0:12:46

. assert reldif(e(supscore),1.736085668840)<1e-8
r; t=0.00 0:12:46

. assert reldif(e(supscore_p),0.238)<1e-8
r; t=0.00 0:12:46

. assert reldif(e(supscore_cv),2.9020830008)<1e-8
r; t=0.00 0:12:46

. // clustered
. rlasso ynull cbd intst lintst y81ldist lintstsq y81nrinc, testonly cluster(id) seed(1)

Sup-score test H0: beta=0
CCK sup-score statistic   1.83 p-value=  0.218
CCK 5% critical value     2.90 (asympt bound)
r; t=0.24 0:12:46

. assert reldif(e(supscore),1.833764450629)<1e-8
r; t=0.00 0:12:46

. assert reldif(e(supscore_p),0.218)<1e-8
r; t=0.00 0:12:46

. assert reldif(e(supscore_cv),2.9020830008)<1e-8
r; t=0.00 0:12:46

.
. ********************** Prediction *********************
.
. // loop through options
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5677921      0.6512871
           lintst |       0.1061444      0.1556115
         y81ldist |       0.0043898      0.0129758
            _cons |*       5.9223612      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:46

. foreach option in "xb" "xb lasso" "xb ols" "resid" "resid lasso" "resid ols" {
  2.         cap drop newvar
  3.         di "option=`option'"
  4.         predict double newvar, `option'
  5. }
option=xb
option=xb lasso
option=xb ols
option=resid
option=resid lasso
option=resid ols
r; t=0.04 0:12:47

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, fe
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4601538      0.5880385
           lintst |       0.0382092      0.1005835
         y81nrinc |      -0.0777246     -0.2035076
------------------+--------------------------------
r; t=0.01 0:12:47

. foreach option in "ue" "e" {
  2.         cap drop newvar
  3.         di "option=`option'"
  4.         predict double newvar, `option'
  5. }
option=ue
option=e
r; t=0.01 0:12:47

. // resids should be mean zero
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5677921      0.6512871
           lintst |       0.1061444      0.1556115
         y81ldist |       0.0043898      0.0129758
            _cons |*       5.9223612      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:47

. foreach option in " " "lasso" "ols" {
  2.         cap drop resid
  3.         predict double resid, resid `option'
  4.         qui sum resid, meanonly
  5.         assert r(mean)<1e-8
  6. }
r; t=0.00 0:12:47

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, fe
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4601538      0.5880385
           lintst |       0.0382092      0.1005835
         y81nrinc |      -0.0777246     -0.2035076
------------------+--------------------------------
r; t=0.02 0:12:47

. foreach option in "u" "e" "ue" {
  2.         cap drop resid
  3.         predict double resid, `option'
  4.         qui sum resid, meanonly
  5.         assert r(mean)<1e-8
  6. }
r; t=0.01 0:12:47

. // mean of predicted yhat = mean of y
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5677921      0.6512871
           lintst |       0.1061444      0.1556115
         y81ldist |       0.0043898      0.0129758
            _cons |*       5.9223612      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:47

. qui sum lrprice, meanonly
r; t=0.00 0:12:47

. local ymean `r(mean)'
r; t=0.00 0:12:47

. foreach option in " " "lasso" "ols" {
  2.         cap drop yhat
  3.         predict double yhat, xb
  4.         qui sum yhat, meanonly
  5.         assert r(mean)-`ymean'<1e-8
  6. }
r; t=0.00 0:12:47

. // confirm post-lasso OLS matches regress
. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5677921      0.6512871
           lintst |       0.1061444      0.1556115
         y81ldist |       0.0043898      0.0129758
            _cons |*       5.9223612      4.7817721
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:47

. local selected `e(selected)'
r; t=0.00 0:12:47

. cap drop xb_rlasso
r; t=0.00 0:12:47

. cap drop resid_rlasso
r; t=0.00 0:12:47

. predict double xb_rlasso, xb ols
r; t=0.00 0:12:47

. predict double resid_rlasso, resid ols
r; t=0.00 0:12:47

. regress lrprice `selected'

      Source |       SS           df       MS      Number of obs   =       321
-------------+----------------------------------   F(3, 317)       =    148.72
       Model |  28.1487632         3  9.38292105   Prob > F        =    0.0000
    Residual |  20.0003795       317   .06309268   R-squared       =    0.5846
-------------+----------------------------------   Adj R-squared   =    0.5807
       Total |  48.1491427       320  .150466071   Root MSE        =    .25118

-------------+----------------------------------------------------------------
      lrprice |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
-------------+----------------------------------------------------------------
       larea |    .6512871    .0437109     14.90    0.000      .5652869     .7372874
      lintst |    .1556115    .0189057      8.23    0.000       .118415     .1928079
    y81ldist |    .0129758    .0029138      4.45    0.000       .007243     .0187085
       _cons |    4.781772    .3263734     14.65    0.000       4.13964     5.423904
-------------+----------------------------------------------------------------
r; t=0.87 0:12:47

. cap drop xb_regress
r; t=0.01 0:12:47

. cap drop resid_regress
r; t=0.00 0:12:47

. predict double xb_regress, xb
r; t=0.00 0:12:47

. predict double resid_regress, resid
r; t=0.00 0:12:47

. assert reldif(xb_rlasso,xb_regress) < 1e-6
r; t=0.00 0:12:47

. assert reldif(resid_rlasso,resid_regress) < 1e-6
r; t=0.00 0:12:47

. // confirm post-lasso LSDV matches xtreg,fe
. foreach opt in xb u e ue xbu {
  2.         cap drop `opt'hat_rlasso
  3.         cap drop `opt'hat_xtreg
  4. }
r; t=0.00 0:12:47

. rlasso lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc, fe
(sorting by xtset panelvar id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4601538      0.5880385
           lintst |       0.0382092      0.1005835
         y81nrinc |      -0.0777246     -0.2035076
------------------+--------------------------------
r; t=0.01 0:12:48

. local selected `e(selected)'
r; t=0.00 0:12:48

. foreach opt in xb u e ue xbu {
  2.         predict double `opt'hat_rlasso, ols `opt'
  3. }
r; t=0.01 0:12:48

. xtreg lrprice `selected', fe

Fixed-effects (within) regression               Number of obs     =        321
Group variable: id                              Number of groups  =         33

R-sq:                                           Obs per group:
     within  = 0.4229                                         min =          1
     between = 0.6121                                         avg =        9.7
     overall = 0.5238                                         max =         10

                                                F(3,285)          =      69.63
corr(u_i, Xb)  = 0.1492                         Prob > F          =     0.0000

-------------+----------------------------------------------------------------
      lrprice |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
-------------+----------------------------------------------------------------
       larea |    .5880385      .04597     12.79    0.000      .4975547     .6785224
      lintst |    .1005835    .0426493      2.36    0.019      .0166359      .184531
    y81nrinc |   -.2035076    .0695852     -2.92    0.004     -.3404738    -.0665415
       _cons |    5.865687    .5076245     11.56    0.000      4.866519     6.864856
-------------+----------------------------------------------------------------
     sigma_u |  .17625241
     sigma_e |  .23924531
         rho |  .35179814   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------
F test that all u_i=0: F(32, 285) = 2.69                     Prob > F = 0.0000
r; t=0.04 0:12:48

. foreach opt in xb u e ue xbu {
  2.         predict double `opt'hat_xtreg, `opt'
  3. }
r; t=0.01 0:12:48

. foreach opt in xb u e ue xbu {
  2.         di "checking option `opt'
  3.         assert reldif(`opt'hat_rlasso, `opt'hat_xtreg) < 1e-6
  4. }
checking option xb
checking option u
checking option e
checking option ue
checking option xbu
r; t=0.00 0:12:48

.
.
. **************** Weights *********************
.
. cap drop one
r; t=0.00 0:12:48

. cap drop wt
r; t=0.00 0:12:48

. gen double one=1
r; t=0.00 0:12:48

. // use a non-integer weight
. global wtvar lcbdsq
r; t=0.00 0:12:48

. gen double wt=$wtvar
r; t=0.00 0:12:48

. // assumes no missings for any regressors or dep var
. sum wt

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
          wt |        321     89.5797    14.18291   47.71708   109.4765
r; t=0.00 0:12:48

. replace wt = wt * 1/r(mean)
(321 real changes made)
r; t=0.00 0:12:48

.
. * Basic estimation with constant and no partialling etc.
. * w_c_ vars are centered (demeaned using weighted means)
. * and then weighted by the sqrt of the weighting var.
.
. cap drop w_*
r; t=0.00 0:12:48

. * weighted centering (so no intercept)
. foreach var of varlist lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc {
  2.         qui sum `var' [aw=wt], meanonly
  3.         gen double w_c_`var' = (`var'-r(mean))*sqrt(wt)
  4. }
r; t=0.00 0:12:48

.
. global vlist lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc
r; t=0.00 0:12:48

. global w_c_vlist w_c_lrprice w_c_larea w_c_cbd w_c_intst w_c_lintst w_c_y81ldist w_c_lintstsq w_c_y81nrinc
r; t=0.00 0:12:48

.
. // disallowed options
. cap noi rlasso $vlist [aw=$wtvar], nocons
incompatible options - weights + noconstant
r; t=0.00 0:12:48

. assert _rc==198
r; t=0.00 0:12:48

.
. foreach opt in "" "rob" {
  2.         // cons automatically partialled out
.         rlasso $vlist [aw=$wtvar], `opt'
  3.         mat b=e(beta)
  4.         mat bOLS=e(betaOLS)
  5.         rlasso $vlist [aw=$wtvar], prestd `opt'
  6.         assert mreldif(e(beta),b)<1e-8
  7.         assert mreldif(e(betaOLS),bOLS)<1e-8
  8.         // no constant to compare
.         mat b=b[1,1..colsof(b)-1]
  9.         mat bOLS=bOLS[1,1..colsof(bOLS)-1]
 10.         // use dm and nocons
.         rlasso $w_c_vlist, dm nocons `opt'
 11.         assert mreldif(e(beta),b)<1e-8
 12.         assert mreldif(e(betaOLS),bOLS)<1e-8
 13. }
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5819738      0.6666220
           lintst |       0.0886430      0.1433958
         y81ldist |       0.0041251      0.0125833
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       5.9920944      4.7856295
------------------+--------------------------------
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5819738      0.6666220
           lintst |       0.0886430      0.1433958
         y81ldist |       0.0041251      0.0125833
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       5.9920944      4.7856295
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
        w_c_larea |       0.5819738      0.6666220
       w_c_lintst |       0.0886430      0.1433958
     w_c_y81ldist |       0.0041251      0.0125833
------------------+--------------------------------
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5670517      0.6666220
           lintst |       0.0950719      0.1433958
         y81ldist |       0.0045284      0.0125833
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.0422607      4.7856295
------------------+--------------------------------
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5670517      0.6666220
           lintst |       0.0950719      0.1433958
         y81ldist |       0.0045284      0.0125833
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.0422607      4.7856295
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
        w_c_larea |       0.5670517      0.6666220
       w_c_lintst |       0.0950719      0.1433958
     w_c_y81ldist |       0.0045284      0.0125833
------------------+--------------------------------
r; t=0.18 0:12:48

.
. * Estimation with constant and partial/notpen
.
. cap drop w_*
r; t=0.00 0:12:48

. cap drop c_*
r; t=0.00 0:12:48

. * weighted partialling out of y81nrinc
. foreach var of varlist lrprice larea cbd intst lintst y81ldist lintstsq {
  2.         qui reg `var' y81nrinc [aw=wt]
  3.         predict double c_`var', resid
  4.         gen double w_c_`var' = c_`var'*sqrt(wt)
  5. }
r; t=0.06 0:12:48

.
. global vlist lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc
r; t=0.00 0:12:48

. global w_c_vlist w_c_lrprice w_c_larea w_c_cbd w_c_intst w_c_lintst w_c_y81ldist w_c_lintstsq
r; t=0.00 0:12:48

.
. foreach opt in "" "rob" {
  2.         rlasso $vlist [aw=$wtvar], partial(y81nrinc) `rob'
  3.         mat b=e(beta)
  4.         mat bOLS=e(betaOLS)
  5.         rlasso $vlist [aw=$wtvar], partial(y81nrinc) prestd `rob'
  6.         assert mreldif(e(beta),b)<1e-8
  7.         assert mreldif(e(betaOLS),bOLS)<1e-8
  8.         rlasso $vlist [aw=$wtvar], pnotpen(y81nrinc) `rob'
  9.         assert mreldif(e(beta),b)<1e-8
 10.         assert mreldif(e(betaOLS),bOLS)<1e-8
 11.         rlasso $vlist [aw=$wtvar], pnotpen(y81nrinc) prestd `rob'
 12.         assert mreldif(e(beta),b)<1e-8
 13.         assert mreldif(e(betaOLS),bOLS)<1e-8
 14.         // no constant or partialled-out var to compare
.         mat b=b[1,1..colsof(b)-2]
 15.         mat bOLS=bOLS[1,1..colsof(bOLS)-2]
 16.         // use dm and nocons
.         rlasso $w_c_vlist, dm nocons `rob'
 17.         assert mreldif(e(beta),b)<1e-8
 18.         assert mreldif(e(betaOLS),bOLS)<1e-8
 19. }
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5749949      0.6634407
           lintst |       0.0789506      0.1297048
         y81ldist |       0.0069238      0.0141991
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.1015180     -0.0802589
            _cons |       6.1362885      4.9422728
------------------+--------------------------------
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5749949      0.6634407
           lintst |       0.0789506      0.1297048
         y81ldist |       0.0069238      0.0141991
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.1015180     -0.0802589
            _cons |       6.1362885      4.9422728
------------------+--------------------------------
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5749949      0.6634407
           lintst |       0.0789506      0.1297048
         y81ldist |       0.0069238      0.0141991
         y81nrinc |*      -0.1015180     -0.0802589
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.1362885      4.9422728
------------------+--------------------------------
*Not penalized
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5749949      0.6634407
           lintst |       0.0789506      0.1297048
         y81ldist |       0.0069238      0.0141991
         y81nrinc |*      -0.1015180     -0.0802589
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.1362885      4.9422728
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
        w_c_larea |       0.5749949      0.6634407
       w_c_lintst |       0.0789506      0.1297048
     w_c_y81ldist |       0.0069238      0.0141991
------------------+--------------------------------
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5749949      0.6634407
           lintst |       0.0789506      0.1297048
         y81ldist |       0.0069238      0.0141991
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.1015180     -0.0802589
            _cons |       6.1362885      4.9422728
------------------+--------------------------------
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5749949      0.6634407
           lintst |       0.0789506      0.1297048
         y81ldist |       0.0069238      0.0141991
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.1015180     -0.0802589
            _cons |       6.1362885      4.9422728
------------------+--------------------------------
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5749949      0.6634407
           lintst |       0.0789506      0.1297048
         y81ldist |       0.0069238      0.0141991
         y81nrinc |*      -0.1015180     -0.0802589
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.1362885      4.9422728
------------------+--------------------------------
*Not penalized
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.5749949      0.6634407
           lintst |       0.0789506      0.1297048
         y81ldist |       0.0069238      0.0141991
         y81nrinc |*      -0.1015180     -0.0802589
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
            _cons |       6.1362885      4.9422728
------------------+--------------------------------
*Not penalized

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
        w_c_larea |       0.5749949      0.6634407
       w_c_lintst |       0.0789506      0.1297048
     w_c_y81ldist |       0.0069238      0.0141991
------------------+--------------------------------
r; t=0.31 0:12:48

.
.
. * Fixed effects + partialling-out
. * id var created earlier
. xtset id
       panel variable:  id (unbalanced)
r; t=0.00 0:12:48

.
. cap drop w_*
r; t=0.00 0:12:48

. cap drop c_*
r; t=0.00 0:12:48

. * weighted partialling out of y81nrinc and FEs
. foreach var of varlist lrprice larea cbd intst lintst y81ldist lintstsq {
  2.         qui reg `var' y81nrinc i.id [aw=wt]
  3.         predict double c_`var', resid
  4.         gen double w_c_`var' = c_`var'*sqrt(wt)
  5. }
r; t=0.08 0:12:48

.
. global vlist lrprice larea cbd intst lintst y81ldist lintstsq y81nrinc
r; t=0.00 0:12:48

. global w_c_vlist w_c_lrprice w_c_larea w_c_cbd w_c_intst w_c_lintst w_c_y81ldist w_c_lintstsq
r; t=0.00 0:12:48

.
. // disallowed options
. cap noi rlasso $vlist [aw=$wtvar], partial(y81nrinc) fe noftools
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)
error - fe option with weights requires ftools package to be installed
see help ftools for details
r; t=0.00 0:12:48

. assert _rc==198
r; t=0.00 0:12:48

.
. foreach opt in "" "rob" {
  2.         rlasso $vlist [aw=$wtvar], partial(y81nrinc) fe `rob'
  3.         mat b=e(beta)
  4.         mat bOLS=e(betaOLS)
  5.         rlasso $vlist [aw=$wtvar], partial(y81nrinc) prestd fe `rob'
  6.         assert mreldif(e(beta),b)<1e-8
  7.         assert mreldif(e(betaOLS),bOLS)<1e-8
  8.         rlasso $vlist [aw=$wtvar], pnotpen(y81nrinc) fe `rob'
  9.         assert mreldif(e(beta),b)<1e-8
 10.         assert mreldif(e(betaOLS),bOLS)<1e-8
 11.         rlasso $vlist [aw=$wtvar], pnotpen(y81nrinc) prestd fe `rob'
 12.         assert mreldif(e(beta),b)<1e-8
 13.         assert mreldif(e(betaOLS),bOLS)<1e-8
 14.         // partial-out FEs by hand
.         rlasso $vlist i.id [aw=$wtvar], partial(y81nrinc i.id) `rob'
 15.         mat bfe=e(beta)
 16.         mat bfe=bfe[1,1..2]
 17.         mat bfeols=e(betaOLS)
 18.         mat bfeols=bfeols[1,1..2]
 19.         assert mreldif(bfe,b)<1e-8
 20.         assert mreldif(bfeols,bOLS)<1e-8
 21.         // no partialled-out var to compare
.         mat b=b[1,1..colsof(b)-1]
 22.         mat bOLS=bOLS[1,1..colsof(bOLS)-1]
 23.         rlasso $w_c_vlist, dm nocons `rob'
 24.         assert mreldif(e(beta),b)<1e-8
 25.         assert mreldif(e(betaOLS),bOLS)<1e-8
 26. }
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.2799452     -0.2709871
------------------+--------------------------------
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.2799452     -0.2709871
------------------+--------------------------------
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
         y81nrinc |*      -0.2799452     -0.2709871
------------------+--------------------------------
*Not penalized
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
         y81nrinc |*      -0.2799452     -0.2709871
------------------+--------------------------------
*Not penalized
(sum of wgt is     2.8755e+04)
Warning: reduced rank encountered when partialling out (may be caused by omitted vars).

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.2799452     -0.2709871
                  |
               id |
               1  |       (omitted)      (omitted)
               2  |       0.1054704      0.1172776
               3  |       0.2652116      0.2387346
               4  |       0.0778708      0.0551744
               5  |       0.1063566      0.0847455
               6  |       0.0980111      0.0839800
               7  |       0.3868828      0.3560441
               8  |       0.4192168      0.4024239
               9  |       0.5067665      0.4601601
              10  |       0.4283910      0.3815130
              11  |       0.3917681      0.3638107
              12  |       0.4184360      0.4112733
              13  |       0.4165676      0.3923341
              14  |       0.4511133      0.4113331
              15  |       0.3494806      0.3234671
              16  |       0.3919927      0.3755104
              17  |       0.1841384      0.1574185
              18  |       0.5963523      0.5431613
              19  |       0.2817940      0.2928926
              20  |       0.5056557      0.4968058
              21  |       0.5124859      0.4939018
              22  |       0.6594473      0.5787118
              23  |       0.7817130      0.6953542
              24  |       0.4947035      0.4656154
              25  |       0.5789679      0.4993976
              26  |       0.3569438      0.3187176
              27  |       0.4250706      0.3835142
              28  |       0.5403327      0.4771397
              29  |       0.6580916      0.5884807
              30  |       0.5760600      0.5330149
              31  |       0.5106629      0.4961018
              32  |       0.4849989      0.4698870
              33  |       1.0104936      0.9624022
                  |
            _cons |       7.3248470      6.2780896
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
        w_c_larea |       0.4696058      0.6113602
------------------+--------------------------------
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.2799452     -0.2709871
------------------+--------------------------------
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.2799452     -0.2709871
------------------+--------------------------------
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
         y81nrinc |*      -0.2799452     -0.2709871
------------------+--------------------------------
*Not penalized
(sorting by xtset panelvar id)
(sum of wgt is     2.8755e+04)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
         y81nrinc |*      -0.2799452     -0.2709871
------------------+--------------------------------
*Not penalized
(sum of wgt is     2.8755e+04)
Warning: reduced rank encountered when partialling out (may be caused by omitted vars).

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            larea |       0.4696058      0.6113602
------------------+--------------------------------
   Partialled-out*|
------------------+--------------------------------
         y81nrinc |      -0.2799452     -0.2709871
                  |
               id |
               1  |       (omitted)      (omitted)
               2  |       0.1054704      0.1172776
               3  |       0.2652116      0.2387346
               4  |       0.0778708      0.0551744
               5  |       0.1063566      0.0847455
               6  |       0.0980111      0.0839800
               7  |       0.3868828      0.3560441
               8  |       0.4192168      0.4024239
               9  |       0.5067665      0.4601601
              10  |       0.4283910      0.3815130
              11  |       0.3917681      0.3638107
              12  |       0.4184360      0.4112733
              13  |       0.4165676      0.3923341
              14  |       0.4511133      0.4113331
              15  |       0.3494806      0.3234671
              16  |       0.3919927      0.3755104
              17  |       0.1841384      0.1574185
              18  |       0.5963523      0.5431613
              19  |       0.2817940      0.2928926
              20  |       0.5056557      0.4968058
              21  |       0.5124859      0.4939018
              22  |       0.6594473      0.5787118
              23  |       0.7817130      0.6953542
              24  |       0.4947035      0.4656154
              25  |       0.5789679      0.4993976
              26  |       0.3569438      0.3187176
              27  |       0.4250706      0.3835142
              28  |       0.5403327      0.4771397
              29  |       0.6580916      0.5884807
              30  |       0.5760600      0.5330149
              31  |       0.5106629      0.4961018
              32  |       0.4849989      0.4698870
              33  |       1.0104936      0.9624022
                  |
            _cons |       7.3248470      6.2780896
------------------+--------------------------------

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
        w_c_larea |       0.4696058      0.6113602
------------------+--------------------------------
r; t=0.39 0:12:49

.
. **************** Misc syntax/options *****************************
.
. // Support for inrange(.) and similar [if] expressions:
. rlasso $vlist if inrange(age,50,70)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
            _cons |*      10.7981860     10.7981860
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:49

.
. **************** Panel data with time series ops *****************
.
. use "http://fmwww.bc.edu/ec-p/data/macro/abdata.dta", clear
(Layard & Nickell, Unemployment in Britain, Economica 53, 1986 from Ox dist)
r; t=0.72 0:12:49

.
. // FE and noftools options
. rlasso ys l(0/3).k l(0/3).n, fe

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
                k |
              --. |       0.1055359      0.1284247
              L3. |      -0.0152348     -0.0618353
                  |
                n |       0.0896224      0.1114129
------------------+--------------------------------
r; t=0.02 0:12:49

. savedresults save ftools e()
r; t=0.00 0:12:49

. cap noi assert "`e(noftools)'"==""  // will be error if ftools not installed
r; t=0.00 0:12:49

. rlasso ys l(0/3).k l(0/3).n, fe noftools

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
                k |
              --. |       0.1055359      0.1284247
              L3. |      -0.0152348     -0.0618353
                  |
                n |       0.0896224      0.1114129
------------------+--------------------------------
r; t=0.05 0:12:49

. assert "`e(noftools)'"=="noftools"
r; t=0.00 0:12:49

. savedresults comp ftools e(), exclude(macro: cmdline)
r; t=0.00 0:12:49

.
. // two-way cluster-robust; order shouldn't matter
. rlasso ys l(0/3).k l(0/3).n, fe cluster(id year)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
                k |       0.0594519      0.1288796
                n |       0.0478063      0.1147564
------------------+--------------------------------
r; t=0.05 0:12:49

. savedresults save twoway e()
r; t=0.00 0:12:49

. rlasso ys l(0/3).k l(0/3).n, fe cluster(year id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
                k |       0.0594519      0.1288796
                n |       0.0478063      0.1147564
------------------+--------------------------------
r; t=0.05 0:12:49

. savedresults comp twoway e(), exclude(                                                          ///
>         macro: cmdline loptions clustvar clustvar1 clustvar2                    ///
>         scalar: N_clust1 N_clust2                                                                               ///
>                 )  tol(1e-8)
r; t=0.00 0:12:49

.
. // balanced panel
. keep if year>=1978 & year<=1982
(331 observations deleted)
r; t=0.00 0:12:49

. // cluster-robust with a balanced panel is equivalent to
. // HAC with bw=all lags and kernel=truncated (and same rlasso gamma)
. // can use gamma(0.1) but HAC with maq theoretically appealing
. rlasso n w k ys                                                                                 ///
>                 , rob bw(4) kernel(tru) maq

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
                k |       0.7145334      0.8128797
            _cons |*       1.3713302      1.4099982
------------------+--------------------------------
*Not penalized
r; t=0.02 0:12:50

. savedresults save hac e()
r; t=0.00 0:12:50

. rlasso n w k ys                                                                         ///
>                 , cluster(id)

------------------+--------------------------------
         Selected |           Lasso   Post-est OLS
------------------+--------------------------------
                k |       0.7145334      0.8128797
            _cons |*       1.3713302      1.4099982
------------------+--------------------------------
*Not penalized
r; t=0.01 0:12:50

. savedresults comp hac e(), exclude(                                                                     ///
>         macro: cmdline robust clustvar kernel                           ///
>         scalar: N_clust N_clust1 N_clust2 bw niter                                                      ///
>                 )  tol(1e-8)
r; t=0.00 0:12:50

.
.
. // ******************* COMPLETE *********************** //
.
. log close
      name:  <unnamed>
       log:  C:\Users\ecomes\Documents\GitHub\lassopack\lassopack_v141incomplete\cs_rlasso.smcl
  log type:  smcl
 closed on:  13 Sep 2020, 00:12:50
-------------