Announcement

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

  • Generate variables from Least angel regression (Lars) coefficients

    Dear Community,

    I want to run Least Angle Regression (Lars) each month and get the coefficients on each variable for the minimum cp each month. I can run a loop of lars, but I don't know how to generate variables from the output of the LARS (the coefficients of variables for the minimum cp model). Can anyone help me with this issue?

    See the following example:

    . sysuse auto,replace
    (1978 Automobile Data)

    . lars price weight length mpg turn rep78 headroom trunk displacement gear_ratio foreign
    NOTE: Deleting all matrices
    ade[11,10]
    mu[1,1]
    meanx[1,10]
    R2[1,11]
    RSS[1,11]
    r2[1,1]
    rss[1,1]
    cp[1,11]
    normx[1,10]
    beta[11,10]
    sbeta[11,10]
    error[1,1]

    sbeta[11,10]
    c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
    r1 0 0 0 0 0 0 0 0 0 0
    r2 0 0 0 0 0 0 0 29.58997 0 0
    r3 4206.5105 0 0 0 0 0 0 4236.1004 0 0
    r4 8354.7246 0 0 0 0 0 0 7563.3658 0 7321.7572
    r5 9083.8849 0 0 0 0 -646.21787 0 8153.5461 0 8294.1935
    r6 11314.929 0 0 0 1007.6609 -2770.2882 0 10176.517 0 10796.469
    r7 12723.966 0 0 -1356.9973 1030.8162 -3147.7325 0 10571.041 0 11113.29
    r8 14013.567 0 0 -2568.2379 1028.7096 -3478.7636 0 10649.105 -409.21915 11560.397
    r9 19421.302 -4246.1604 0 -3103.4225 1210.2002 -3494.6232 0 10048.91 -797.18484 12114.523
    r10 20594.449 -5323.0166 0 -3209.572 1219.9377 -3603.9899 288.90929 9897.2915 -874.63332 12224.024
    r11 29559.447 -14348.173 -1054.8395 -4185.059 1508.5046 -4471.0833 2560.6707 8864.168 -1215.7602 12746.232

    Algorithm is lars

    Cp, R-squared and Actions along the sequence of models

    +-----------------------------------------------+
    Step Cp R-square Action
    ------+-------------+----------+---------------
    1 77.5851 0.0000
    2 79.3901 0.0013 +displacement
    3 43.1477 0.2658 +weight
    4 17.3644 0.4580 +foreign
    5 15.9537 0.4816 +headroom
    6 9.9905 0.5367 +rep78
    7 10.1221 0.5496 +turn
    8 10.6949 0.5595 +gear_ratio
    9 9.7780 * 0.5796 +length
    10 11.2126 0.5835 +trunk
    11 11.0000 0.5989 +mpg
    +-----------------------------------------------+
    * indicates the smallest value for Cp

    The coefficient values for the minimum Cp

    +-----------------------------+
    Variable Coefficient
    --------------+--------------
    weight 2.9705
    length -22.6365
    turn -84.7424
    rep78 148.2509
    headroom -496.7041
    displacement 13.0825
    gear_ratio -208.9403
    foreign 3169.5719
    +-----------------------------+

    .
    So basically, I want to create variables from the above coefficients. If the regression is OLS, I can use "gen cof_weight=_b[weight]". But this does not work for the LARS.

    Thank you in advance,
    Bob

  • #2
    Welcome to Statalist, Bob.

    For the benefit of other members, lars is a user-written Stata command available from SSC. The output of your demonstration, when presented using CODE delimiters, is more readable.
    Code:
    . lars price weight length mpg turn rep78 headroom trunk displacement gear_ratio foreign
    NOTE: Deleting all matrices
              ade[11,10]
               mu[1,1]
            meanx[1,10]
               R2[1,11]
              RSS[1,11]
               r2[1,1]
              rss[1,1]
               cp[1,11]
            normx[1,10]
             beta[11,10]
            sbeta[11,10]
            error[1,1]
    
    sbeta[11,10]
                 c1          c2          c3          c4          c5          c6          c7
     r1           0           0           0           0           0           0           0
     r2           0           0           0           0           0           0           0
     r3   4206.5105           0           0           0           0           0           0
     r4   8354.7246           0           0           0           0           0           0
     r5   9083.8849           0           0           0           0  -646.21787           0
     r6   11314.929           0           0           0   1007.6609  -2770.2882           0
     r7   12723.966           0           0  -1356.9973   1030.8162  -3147.7325           0
     r8   14013.567           0           0  -2568.2379   1028.7096  -3478.7636           0
     r9   19421.302  -4246.1604           0  -3103.4225   1210.2002  -3494.6232           0
    r10   20594.449  -5323.0166           0   -3209.572   1219.9377  -3603.9899   288.90929
    r11   29559.447  -14348.173  -1054.8395   -4185.059   1508.5046  -4471.0833   2560.6707
    
                 c8          c9         c10
     r1           0           0           0
     r2    29.58997           0           0
     r3   4236.1004           0           0
     r4   7563.3658           0   7321.7572
     r5   8153.5461           0   8294.1935
     r6   10176.517           0   10796.469
     r7   10571.041           0    11113.29
     r8   10649.105  -409.21915   11560.397
     r9    10048.91  -797.18484   12114.523
    r10   9897.2915  -874.63332   12224.024
    r11    8864.168  -1215.7602   12746.232
    
    Algorithm is lars
    
    Cp, R-squared and Actions along the sequence of models
    
    +-----------------------------------------------+
    | Step |      Cp     | R-square |  Action       |
    |------+-------------+----------+---------------|
    |    1 |    77.5851  |  0.0000  |               | 
    |    2 |    79.3901  |  0.0013  | +displacement | 
    |    3 |    43.1477  |  0.2658  | +weight       | 
    |    4 |    17.3644  |  0.4580  | +foreign      | 
    |    5 |    15.9537  |  0.4816  | +headroom     | 
    |    6 |     9.9905  |  0.5367  | +rep78        | 
    |    7 |    10.1221  |  0.5496  | +turn         | 
    |    8 |    10.6949  |  0.5595  | +gear_ratio   | 
    |    9 |     9.7780 *|  0.5796  | +length       | 
    |   10 |    11.2126  |  0.5835  | +trunk        | 
    |   11 |    11.0000  |  0.5989  | +mpg          | 
    +-----------------------------------------------+
    * indicates the smallest value for Cp
    
    The coefficient values for the minimum Cp
    
    +-----------------------------+
    | Variable     |  Coefficient |
    |--------------+--------------|
    | weight       |       2.9705 |
    | length       |     -22.6365 |
    | turn         |     -84.7424 |
    | rep78        |     148.2509 |
    | headroom     |    -496.7041 |
    | displacement |      13.0825 |
    | gear_ratio   |    -208.9403 |
    | foreign      |    3169.5719 |
    +-----------------------------+
    
    . return list
    
    matrices:
                    r(RSS) :  1 x 11
                     r(R2) :  1 x 11
               r(newbetas) :  1 x 10
                     r(cp) :  1 x 11
                  r(sbeta) :  11 x 10
                  r(normx) :  1 x 10
                   r(beta) :  11 x 10
    You'll note that I added the return list command, which shows the matrices returned by lars and documented in the output of help lars, You'll find what you need among them, I believe.

    With that said, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Comment

    Working...
    X