Announcement

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

  • Sharing a list of covariates with stata and mata

    I'm using stata to get intial values for an optimization problem and then mata to do the optimization. A snipped of some crude code that works looks like

    reg y x1 x2 x3 x4
    mata:
    /* I need to create the y and X matrix again*/
    y=st_data(.,"y")
    X=st_data(.,"x1 x2 x3 x4 ")


    I would like to be able to change the list of covariates (adding or dropping some) without having to do so TWICE (to minimize errors). Is there a good way to share the list of covariates?

  • #2
    With your example, local macros seem to work as usual.
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . keep in 1/10
    (64 observations deleted)
    
    . local y price
    
    . local x weight length
    
    . list `y' `x'
    
         +--------------------------+
         |  price   weight   length |
         |--------------------------|
      1. |  4,099    2,930      186 |
      2. |  4,749    3,350      173 |
      3. |  3,799    2,640      168 |
      4. |  4,816    3,250      196 |
      5. |  7,827    4,080      222 |
         |--------------------------|
      6. |  5,788    3,670      218 |
      7. |  4,453    2,230      170 |
      8. |  5,189    3,280      200 |
      9. | 10,372    3,880      207 |
     10. |  4,082    3,400      200 |
         +--------------------------+
    
    . regress `y' `x'
    
          Source |       SS           df       MS      Number of obs   =        10
    -------------+----------------------------------   F(2, 7)         =      3.58
           Model |  19382884.6         2  9691442.28   Prob > F        =    0.0849
        Residual |  18940057.8         7  2705722.55   R-squared       =    0.5058
    -------------+----------------------------------   Adj R-squared   =    0.3646
           Total |  38322942.4         9  4258104.71   Root MSE        =    1644.9
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          weight |    2.60011   1.915132     1.36   0.217    -1.928457    7.128678
          length |   .9434239   55.32872     0.02   0.987    -129.8882    131.7751
           _cons |  -3170.585   6266.466    -0.51   0.628    -17988.42    11647.25
    ------------------------------------------------------------------------------
    
    . mata:
    ------------------------------------------------- mata (type end to exit) ----------------------
    : /* I need to create the y and X matrix again*/
    : y=st_data(.,"`y'")
    
    : X=st_data(.,"`x'")
    
    : y
                1
         +---------+
       1 |   4099  |
       2 |   4749  |
       3 |   3799  |
       4 |   4816  |
       5 |   7827  |
       6 |   5788  |
       7 |   4453  |
       8 |   5189  |
       9 |  10372  |
      10 |   4082  |
         +---------+
    
    : X
               1      2
         +---------------+
       1 |  2930    186  |
       2 |  3350    173  |
       3 |  2640    168  |
       4 |  3250    196  |
       5 |  4080    222  |
       6 |  3670    218  |
       7 |  2230    170  |
       8 |  3280    200  |
       9 |  3880    207  |
      10 |  3400    200  |
         +---------------+
    
    : end
    ------------------------------------------------------------------------------------------------
    
    .

    Comment


    • #3
      Perfect. Thank you.

      Comment

      Working...
      X