Announcement

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

  • Omit alwaysvars from lassocoef output

    Hello,

    I'm running a lasso with some very high dimensional categorical variables included as alwaysvars. The code is:
    lasso linear `dv' (i.dpc i.year) x*, noconstant sel(cv)
    Afterwards, I run lassocoef to see the selected variables.

    The output of lassocoef includes the hundreds of values of the dpc variable. Is it possible to omit these from the lassocoef output, and only print the selected "othervars"?

    Thanks,
    Mitch
    Mitch Downey, Grad student, UCSD Economics

  • #2
    It should be easy to create a reproducible example to illustrate what you are asking. See FAQ Advice #12 on that.

    Comment


    • #3
      Hi Andrew,

      Thanks for getting back to me. Yes, it is easy, but I don't think it clarifies things all that much.

      Code:
      sysuse auto
      lasso linear price (i.mpg) weight, noconstant sel(cv)
      lassocoef
      That produces the output:
      Code:
      ------------------------
                   |  active  
      -------------+----------
               mpg |
               15  |     x    
               16  |     x    
               17  |     x    
               18  |     x    
               19  |     x    
               20  |     x    
               21  |     x    
               22  |     x    
               23  |     x    
               24  |     x    
               25  |     x    
               26  |     x    
               28  |     x    
               35  |     x    
                   |
            weight |     x    
      ------------------------
      Legend:
        b - base level
        e - empty cell
        o - omitted
        x - estimated
      From that output, I just want to omit the rows corresponding to the different values of mpg. Since those are alwaysvars, I don't need them reported in the table, and omitting them would make it somewhat easier to see which variables were actually selected by the LASSO method.

      Thanks,
      Mitch
      Mitch Downey, Grad student, UCSD Economics

      Comment


      • #4
        Here is a reproducible example. Assume that "rep78" below represents the categorical control variable. You can extract the row names of the matrix r(coef) stored in r() after lassocoef, and subsequently get the names of all other variables. Then, display the matrix elements corresponding to these variables.

        Code:
        sysuse auto, clear
        set seed 05062025
        lasso linear mpg weight i.rep78 turn gear_ratio
        lassocoef, display(coef)
        
        *EXTRACT ROWNAMES EXCLUDING CATVAR
        local names= ustrregexra("`:rownames r(coef)'", "\b[0-9+]\.rep78\b", "")
        
        cap mat drop wanted
        foreach name in `names'{
            mat wanted= nullmat(wanted)\ r(coef)["`name'", 1]
        }
        mat l wanted
        Res.:

        Code:
        . lassocoef, display(coef)
        
        ------------------------
                     |    active
        -------------+----------
              weight | -3.510367
             5.rep78 |   .760635
                turn | -.6059548
          gear_ratio |  .2050545
               _cons |         0
        ------------------------
        Legend:
          b - base level
          e - empty cell
          o - omitted
        
        .
        .
        .
        . *EXTRACT ROWNAMES EXCLUDING CATVAR
        
        .
        . local names= ustrregexra("`:rownames r(coef)'", "\b[0-9+]\.rep78\b", "")
        
        .
        .
        .
        . cap mat drop wanted
        
        .
        . foreach name in `names'{
          2.
        .     mat wanted= nullmat(wanted)\ r(coef)["`name'", 1]
          3.
        . }
        
        .
        . mat l wanted
        
        wanted[4,1]
                        active
            weight   -3.510367
              turn  -.60595476
        gear_ratio   .20505451
             _cons           0
        To suppress the output of lassocoef in your implementation, add the prefix -quietly- to the command:

        Code:
        quietly: lassocoef ...
        Last edited by Andrew Musau; 06 May 2025, 07:37.

        Comment


        • #5
          This is extremely helpful, Andrew, thank you.
          Mitch Downey, Grad student, UCSD Economics

          Comment

          Working...
          X