Announcement

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

  • Column names and stata-mata transfers

    Hi all,

    I have a program that in order to run I have to remove all 0 values from a matrix and then do some basic linear algebra.
    The returned value should have column names still, but I'm unable to both use mata to drop the values and to keep the column names form the original stata matrix.

    Example:
    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . reg price mpg i.rep78
    
          Source |       SS           df       MS      Number of obs   =        69
    -------------+----------------------------------   F(5, 63)        =      4.39
           Model |   149020603         5  29804120.7   Prob > F        =    0.0017
        Residual |   427776355        63  6790100.88   R-squared       =    0.2584
    -------------+----------------------------------   Adj R-squared   =    0.1995
           Total |   576796959        68  8482308.22   Root MSE        =    2605.8
    
    ------------------------------------------------------------------------------
           price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             mpg |  -280.2615   61.57666    -4.55   0.000    -403.3126   -157.2103
                 |
           rep78 |
              2  |   877.6347   2063.285     0.43   0.672     -3245.51     5000.78
              3  |   1425.657   1905.438     0.75   0.457    -2382.057    5233.371
              4  |   1693.841   1942.669     0.87   0.387    -2188.274    5575.956
              5  |   3131.982   2041.049     1.53   0.130    -946.7282    7210.693
                 |
           _cons |   10449.99   2251.041     4.64   0.000     5951.646    14948.34
    ------------------------------------------------------------------------------
    
    . mata: B = st_matrix("e(b)")
    
    . mata: B
                      1              2              3              4              5              6              7
        +----------------------------------------------------------------------------------------------------------+
      1 |  -280.2614852              0    877.6347152    1425.657006     1693.84099    3131.982179    10449.99119  |
        +----------------------------------------------------------------------------------------------------------+
    
    . mata: collabel = st_matrixcolstripe("e(b)")
    
    . mata: collabel
                  1          2
        +-----------------------+
      1 |                  mpg  |
      2 |             1b.rep78  |
      3 |              2.rep78  |
      4 |              3.rep78  |
      5 |              4.rep78  |
      6 |              5.rep78  |
      7 |                _cons  |
        +-----------------------+
    
    . mata: B_sub = select(B, B[1,.]:!=0)
    
    . mata: B_sub
                      1              2              3              4              5              6
        +-------------------------------------------------------------------------------------------+
      1 |  -280.2614852    877.6347152    1425.657006     1693.84099    3131.982179    10449.99119  |
        +-------------------------------------------------------------------------------------------+
    I can't use the column stripe (collabel) now on B_sub, because the dimensions differ...

    Any help would be most welcome, I have been struggling with this for some time now.

  • #2
    I am not sure if I understand fully the problem. The matrices collabel and B are different objects and thus independent in mata. To restrict the collabel you could code using selectindex():
    Code:
    mata: collabel = st_matrixcolstripe("e(b)")
    mata: idx = selectindex(B:!=0)
    mata: B_sub = B[idx]
    mata: collabel_sub = collabel[idx,.]
    . mata B_sub
                      1              2              3              4              5              6
        +-------------------------------------------------------------------------------------------+
      1 |  -280.2614852    877.6347152    1425.657006     1693.84099    3131.982179    10449.99119  |
        +-------------------------------------------------------------------------------------------+
    
    
    
    . mata collabel_sub
                 1         2
        +---------------------+
      1 |                mpg  |
      2 |            2.rep78  |
      3 |            3.rep78  |
      4 |            4.rep78  |
      5 |            5.rep78  |
      6 |              _cons  |
        +---------------------+

    Comment


    • #3
      I think you understood the problem very well and it looks like your solution is SPOT ON.

      I'll try it soon, I think it's exactly what I wanted. Thank you so much.

      Comment

      Working...
      X