Announcement

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

  • Inputting matrix into Mata with interactions

    Have been working on some out of sample prediction code for a specific case of the mixed command(It will only do in sample predictions for reffects). We have it working but am trying to speed it up.
    So far we have been using predict for getting the fixed effect predictions. However, this is actually several times slower than our reffects mata code so I would like to replace it with specific mata code.

    The problem I am having is getting the matrix X into the mata function. I am extracting the list of variables required from e(b) using colnames and have a string of variable names that can be parsed by the tokens function. The problem I am having is that it is unable to cope with interactions. The only solution I can come up with currently is to parse the string of variable names in stata code and create tempvars for each interaction term. That could get messy very quickly.
    Is there a better way of manually calculating XB for the mixed command using mata faster than the predict function?

  • #2
    In my experience you do not need to create tempvars. You can get interaction terms directly into Mata without formally generating these variables. Here is an example:
    Code:
    . clear
    
    . set obs 10
    number of observations (_N) was 0, now 10
    
    . set seed 1
    
    . gen y=rnormal()
    
    . gen x=rnormal()
    
    . gen d=rbinomial(1,0.5)
    
    . mata st_data(.,("1.d","1.d#c.x"))
                       1              2
         +-------------------------------+
       1 |             0              0  |
       2 |             0              0  |
       3 |             0              0  |
       4 |             0              0  |
       5 |             1   -.0749783516  |
       6 |             1   -.6136219501  |
       7 |             1   -1.341230392  |
       8 |             0              0  |
       9 |             1    1.146460414  |
      10 |             1    1.376888633  |
         +-------------------------------+

    Comment


    • #3
      Originally posted by Blaise Melly View Post
      In my experience you do not need to create tempvars. You can get interaction terms directly into Mata without formally generating these variables. Here is an example:
      Code:
      . clear
      
      . set obs 10
      number of observations (_N) was 0, now 10
      
      . set seed 1
      
      . gen y=rnormal()
      
      . gen x=rnormal()
      
      . gen d=rbinomial(1,0.5)
      
      . mata st_data(.,("1.d","1.d#c.x"))
      1 2
      +-------------------------------+
      1 | 0 0 |
      2 | 0 0 |
      3 | 0 0 |
      4 | 0 0 |
      5 | 1 -.0749783516 |
      6 | 1 -.6136219501 |
      7 | 1 -1.341230392 |
      8 | 0 0 |
      9 | 1 1.146460414 |
      10 | 1 1.376888633 |
      +-------------------------------+
      Turns out my problem was actually related to marksample not doing what I expected(which now makes sense). Typically I discovered this after programming the tempvar workaround. Thanks for your help.

      Comment

      Working...
      X